|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import assert from 'node:assert'; |
| 14 | +import sinon from 'sinon'; |
| 15 | +import esmock from 'esmock'; |
| 16 | +import { ResponseError } from '../../src/utils/http.js'; |
| 17 | + |
| 18 | +describe('handleProductFetchRequest', () => { |
| 19 | + let handleProductFetchRequest; |
| 20 | + let fetchProductStub; |
| 21 | + let errorResponseStub; |
| 22 | + let ctx; |
| 23 | + let config; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + fetchProductStub = sinon.stub(); |
| 27 | + errorResponseStub = sinon.stub(); |
| 28 | + |
| 29 | + const moduleUnderTest = await esmock('../../src/catalog/fetch.js', { |
| 30 | + '../../src/utils/r2.js': { fetchProduct: fetchProductStub }, |
| 31 | + '../../src/utils/http.js': { errorResponse: errorResponseStub }, |
| 32 | + }); |
| 33 | + |
| 34 | + ({ handleProductFetchRequest } = moduleUnderTest); |
| 35 | + |
| 36 | + ctx = { |
| 37 | + url: new URL('https://example.com/products/12345'), |
| 38 | + log: { |
| 39 | + error: sinon.stub(), |
| 40 | + }, |
| 41 | + }; |
| 42 | + config = {}; |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(async () => { |
| 46 | + await esmock.purge(handleProductFetchRequest); |
| 47 | + sinon.restore(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return the product response when fetchProduct succeeds', async () => { |
| 51 | + const sku = '12345'; |
| 52 | + const product = { id: sku, name: 'Test Product' }; |
| 53 | + fetchProductStub.resolves(product); |
| 54 | + |
| 55 | + const response = await handleProductFetchRequest(ctx, config); |
| 56 | + |
| 57 | + assert.equal(response.headers.get('Content-Type'), 'application/json'); |
| 58 | + const responseBody = await response.text(); |
| 59 | + assert.equal(responseBody, JSON.stringify(product)); |
| 60 | + sinon.assert.calledWith(fetchProductStub, ctx, config, sku); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return e.response when fetchProduct throws an error with a response property', async () => { |
| 64 | + const errorResponse = new Response('Not Found', { status: 404 }); |
| 65 | + const error = new ResponseError('Product not found', errorResponse); |
| 66 | + fetchProductStub.rejects(error); |
| 67 | + |
| 68 | + const response = await handleProductFetchRequest(ctx, config); |
| 69 | + |
| 70 | + assert.strictEqual(response, errorResponse); |
| 71 | + sinon.assert.notCalled(ctx.log.error); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should log error and return 500 response when fetchProduct throws an error without a response property', async () => { |
| 75 | + const error = new Error('Internal Server Error'); |
| 76 | + fetchProductStub.rejects(error); |
| 77 | + const errorResp = new Response('Internal Server Error', { status: 500 }); |
| 78 | + errorResponseStub.returns(errorResp); |
| 79 | + |
| 80 | + const response = await handleProductFetchRequest(ctx, config); |
| 81 | + |
| 82 | + assert.equal(response.status, 500); |
| 83 | + const responseBody = await response.text(); |
| 84 | + assert.equal(responseBody, 'Internal Server Error'); |
| 85 | + sinon.assert.calledOnce(ctx.log.error); |
| 86 | + sinon.assert.calledWith(ctx.log.error, error); |
| 87 | + sinon.assert.calledWith(errorResponseStub, 500, 'internal server error'); |
| 88 | + }); |
| 89 | +}); |
0 commit comments