diff --git a/package.json b/package.json index c582c19b..4dc314d5 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "node": "^10.17 || ^12" }, "scripts": { - "lint": "eslint . && prettier --check .", - "lint-and-fix": "eslint . --fix && prettier --write .", + "lint": "eslint . && prettier --check . --ignore-unknown", + "lint-and-fix": "eslint . --fix && prettier --write . --ignore-unknown", "test": "jest", "test-with-coverage": "jest --coverage --verbose" }, diff --git a/server/plugins/renderer/renderer.module.js b/server/plugins/renderer/renderer.module.js index c4d019be..0d8413e0 100644 --- a/server/plugins/renderer/renderer.module.js +++ b/server/plugins/renderer/renderer.module.js @@ -135,7 +135,7 @@ internals.getResponse = async (request) => { const contentType = response.headers.get('content-type') || ''; const isResponseJson = contentType.toLowerCase().includes('application/json'); - const bcAppData = isResponseJson ? await response.json() : await response.text(); + const bcAppData = isResponseJson ? await response.json() : await response.buffer(); // cache response cache.put( diff --git a/server/plugins/renderer/renderer.module.spec.js b/server/plugins/renderer/renderer.module.spec.js index 56f83447..eb093a18 100644 --- a/server/plugins/renderer/renderer.module.spec.js +++ b/server/plugins/renderer/renderer.module.spec.js @@ -1,5 +1,6 @@ const fetchMock = require('node-fetch'); const path = require('path'); +const fs = require('fs'); const Server = require('../../index'); const ThemeConfig = require('../../../lib/theme-config'); @@ -12,6 +13,8 @@ const themeConfigManager = ThemeConfig.getInstance( // eslint-disable-next-line node/no-unpublished-require,global-require jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox()); +fetchMock.config.sendAsJson = false; + describe('Renderer Plugin', () => { const storeUrl = 'https://store-abc123.mybigcommerce.com'; const normalStoreUrl = 'http://s123456789.mybigcommerce.com'; @@ -173,7 +176,7 @@ describe('Renderer Plugin', () => { expect(localServerResponse.statusCode).toEqual(401); }); - describe('when the storefront server response is Success and content-type is not JSON', () => { + describe('when the storefront server response is Success and content-type is "text/html"', () => { const browserRequest = { method: 'get', url: '/checkout.php', @@ -263,4 +266,52 @@ describe('Renderer Plugin', () => { ); }); }); + + describe('when the storefront server response is Success and content-type is "image"', () => { + const browserRequest = { + method: 'get', + url: '/content/cat_and_dog.jpeg', + }; + const testImage = fs.readFileSync('./test/assets/cat_and_dog.jpeg'); + const storefrontServerResponse = { + status: 200, + headers: new fetchMock.Headers({ + 'content-type': 'image/jpeg', + }), + body: testImage, + }; + let localServerResponse; + + beforeEach(async () => { + fetchMock.mock('*', storefrontServerResponse); + + localServerResponse = await server.inject(browserRequest); + }); + + it('should send a request to the storefront server with correct url', async () => { + expect(fetchMock.lastUrl()).toEqual(`${storeUrl}${browserRequest.url}`); + }); + + it('should pass request method from browser request to the storefront server request', async () => { + expect(fetchMock.lastOptions().method).toEqual(browserRequest.method); + }); + + it('should return a correct status code', async () => { + expect(localServerResponse.statusCode).toEqual(200); + }); + + it('should return correct headers', async () => { + expect(localServerResponse.headers).toMatchObject({ + 'content-type': storefrontServerResponse.headers.get('content-type'), + // Beware, if our local server parsed the storefront server response wrongly - + // content-length will be different + 'content-length': 6858, + }); + }); + + it('should return a correct response body', async () => { + expect(localServerResponse.rawPayload).toBeInstanceOf(Buffer); + expect(localServerResponse.rawPayload).toEqual(testImage); + }); + }); }); diff --git a/test/assets/cat_and_dog.jpeg b/test/assets/cat_and_dog.jpeg new file mode 100644 index 00000000..1af49a77 Binary files /dev/null and b/test/assets/cat_and_dog.jpeg differ