From b7aefca859184737e315e8bb3c49890a25d20e99 Mon Sep 17 00:00:00 2001 From: Max Moroz <95914987+bc-max@users.noreply.github.com> Date: Fri, 4 Nov 2022 13:17:07 +0200 Subject: [PATCH] fix(storefront): strf-10193 Bad request for custom front matter: (#1019) * fix(storefront): strf-10193 Bad request for custom front matter: - template is get locally; - test fix; --- server/plugins/renderer/renderer.module.js | 28 ++--- .../renderer/renderer.module.option2.spec.js | 116 ------------------ .../plugins/renderer/renderer.module.spec.js | 12 -- 3 files changed, 7 insertions(+), 149 deletions(-) delete mode 100644 server/plugins/renderer/renderer.module.option2.spec.js diff --git a/server/plugins/renderer/renderer.module.js b/server/plugins/renderer/renderer.module.js index b1abff76..4d11c2b7 100644 --- a/server/plugins/renderer/renderer.module.js +++ b/server/plugins/renderer/renderer.module.js @@ -21,7 +21,6 @@ const { } = require('../../../lib/utils/frontmatter'); const networkUtils = new NetworkUtils(); -const { DEFAULT_CUSTOM_LAYOUTS_CONFIG } = require('../../../constants'); const internals = { options: {}, @@ -79,14 +78,12 @@ internals.getResponse = async (request) => { const withoutPageQuery = new URL(fullUrl.toString()); withoutPageQuery.searchParams.delete('page'); - const { customLayouts } = internals.options; - const httpOpts = { url: withoutPageQuery.toString(), headers: internals.buildReqHeaders({ request, stencilOptions: { get_template_file: true, get_data_only: true }, - extraHeaders: { host: storeUrlObj.host, stencil_custom_templates: customLayouts }, + extraHeaders: { host: storeUrlObj.host }, }), accessToken: internals.options.accessToken, data: request.payload, @@ -119,7 +116,6 @@ internals.getResponse = async (request) => { // clear when making a non-get request because smth may be changed cache.clear(); } - const response = await networkUtils.sendApiRequest(httpOpts); internals.processResHeaders(response.headers); @@ -137,6 +133,7 @@ internals.getResponse = async (request) => { ? JSON.parse(await readFromStream(response.data)) : response.data; + internals.updateTemplatePath(request.path, bcAppData); // cache response cache.put( requestSignature, @@ -156,13 +153,10 @@ internals.getResponse = async (request) => { * @param request * @param response * @param responseArgs - * @param customLayouts * @returns {*} */ internals.parseResponse = async (bcAppData, request, response, responseArgs) => { const { httpOpts, storeUrlObj } = responseArgs; - // eslint-disable-next-line camelcase - const { stencil_custom_templates } = httpOpts.headers; if (typeof bcAppData !== 'object' || !('pencil_response' in bcAppData)) { delete response.headers['x-frame-options']; @@ -185,7 +179,6 @@ internals.parseResponse = async (bcAppData, request, response, responseArgs) => stencilConfig: internals.getResourceConfig(bcAppData, request, configuration), extraHeaders: { host: storeUrlObj.host, - stencil_custom_templates, }, }); httpOpts.responseType = 'json'; // In the second request we always expect json @@ -344,9 +337,9 @@ internals.redirect = async (response, request) => { * * @param {string} requestPath * @param {Object} data - * @returns {string} + * @returns {void} */ -internals.getTemplatePath = (requestPath, data) => { +internals.updateTemplatePath = (requestPath, data) => { const customLayouts = internals.options.customLayouts || {}; const pageType = data.page_type; let templatePath; @@ -369,10 +362,10 @@ internals.getTemplatePath = (requestPath, data) => { if (templatePath) { templatePath = path.join('pages/custom', pageType, templatePath.replace(/\.html$/, '')); + // eslint-disable-next-line no-param-reassign + data.template_file = templatePath; } } - - return templatePath || data.template_file; }; function getAcceptLanguageHeader(request) { @@ -413,7 +406,7 @@ internals.getPencilResponse = (data, request, response, configuration, renderedR return new PencilResponse( { - template_file: internals.getTemplatePath(request.path, data), + template_file: data.template_file, templates: data.templates, remote: data.remote, remote_data: data.remote_data, @@ -451,14 +444,9 @@ internals.buildReqHeaders = ({ ? JSON.parse(request.headers['stencil-options']) : {}; - const templates = - JSON.stringify(extraHeaders.stencil_custom_templates) || - JSON.stringify(DEFAULT_CUSTOM_LAYOUTS_CONFIG); - const headers = { 'stencil-options': JSON.stringify({ ...stencilOptions, ...currentOptions }), 'accept-encoding': 'identity', - 'stencil-custom-templates': templates, }; const config = request.headers['stencil-config']; @@ -466,8 +454,6 @@ internals.buildReqHeaders = ({ if ((!config || config === '{}') && stencilConfig) { headers['stencil-config'] = JSON.stringify(stencilConfig); } - // eslint-disable-next-line no-param-reassign - delete extraHeaders.stencil_custom_templates; return { ...request.headers, ...headers, ...extraHeaders }; }; diff --git a/server/plugins/renderer/renderer.module.option2.spec.js b/server/plugins/renderer/renderer.module.option2.spec.js deleted file mode 100644 index b3bc3af8..00000000 --- a/server/plugins/renderer/renderer.module.option2.spec.js +++ /dev/null @@ -1,116 +0,0 @@ -const axios = require('axios'); -const MockAdapter = require('axios-mock-adapter'); -const path = require('path'); - -const Server = require('../../index'); -const ThemeConfig = require('../../../lib/theme-config'); -const { PACKAGE_INFO } = require('../../../constants'); - -const themeConfigManager = ThemeConfig.getInstance( - path.join(process.cwd(), 'test/_mocks/themes/valid'), -); - -const axiosMock = new MockAdapter(axios); - -/** - * We separated this suit from what we have in renderer.module.spec as it uses different server options. - * Two servers in one file conflicted and tests failed. - */ -describe('Renderer Plugin', () => { - const storeUrl = 'https://store-abc123.mybigcommerce.com'; - const normalStoreUrl = 'http://s123456789.mybigcommerce.com'; - const serverOptions = { - dotStencilFile: { - storeUrl, - normalStoreUrl, - port: 4000, - username: 'testUser', - token: '6832b1c755bb9de13aa8990216a69a7623043fd7', - }, - useCache: false, - themePath: themeConfigManager.themePath, - }; - let server; - - beforeAll(async () => { - // Prevent littering the console - jest.spyOn(console, 'log').mockImplementation(jest.fn()); - jest.spyOn(console, 'error').mockImplementation(jest.fn()); - jest.spyOn(console, 'info').mockImplementation(jest.fn()); - - server = await Server.create(serverOptions); - - // Don't log errors during the test - server.ext('onPostHandler', (request, h) => { - if (request.response.isBoom) { - return h.response().code(500); - } - return h.continue; - }); - }); - - afterEach(() => { - jest.resetAllMocks(); - axiosMock.reset(); - }); - - afterAll(async () => { - await server.stop(); - }); - - it('should send a request to the storefront server with added custom layouts to headers when the storefront server response is Redirect', async () => { - const browserRequest = { - method: 'post', - url: '/login.php?action=check_login', - payload: 'login_email=user%40gmail.com&login_pass=12345678&authenticity_token=abcdefg', - headers: { - 'content-type': 'application/x-www-form-urlencoded', - }, - }; - - const redirectLocationPath = '/account.php?action=order_status#first'; - const storefrontResponseHeaders = { - location: `${normalStoreUrl}${redirectLocationPath}`, - 'set-cookie': [ - 'SHOP_SESSION_TOKEN=aaaaaaaaaaaaaa; expires=Mon, 12-Oct-2020 17:40:04 GMT; path=/; Secure; HttpOnly; SameSite=none', - 'fornax_anonymousId=bbbbbbbbbbbbbb; expires=Wed, 05-Oct-2022 17:40:04 GMT; path=/; Secure; SameSite=none', - 'RECENTLY_VIEWED_PRODUCTS=cccccccc; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; Secure; SameSite=none', - 'SHOP_TOKEN=dddddddddddddddddddddd; expires=Mon, 12-Oct-2020 17:40:04 GMT; path=/; Secure; HttpOnly; SameSite=none', - ], - }; - axiosMock.onPost().reply(301, undefined, storefrontResponseHeaders); - await server.inject(browserRequest); - - expect(axiosMock.history.post[0].headers).toMatchObject({ - 'Content-Type': 'application/x-www-form-urlencoded', - host: new URL(storeUrl).host, - 'stencil-cli': PACKAGE_INFO.version, - 'stencil-options': '{"get_template_file":true,"get_data_only":true}', - 'stencil-version': PACKAGE_INFO.config.stencil_version, - 'accept-encoding': 'identity', - 'stencil-custom-templates': '{"brand":{},"category":{},"page":{},"product":{}}', - }); - }); - - it('should send a request to the storefront server with added custom layouts to headers when the storefront server response is Success and content-type is "text/html"', async () => { - const browserRequest = { - method: 'get', - url: '/checkout.php', - headers: { - cookie: 'bcactive=yes; lastVisitedCategory=23; STORE_VISITOR=1', - }, - }; - axiosMock.onGet().reply(200, {}); - await server.inject(browserRequest); - - expect(axiosMock.history.get[0].headers).toMatchObject({ - cookie: browserRequest.headers.cookie, - host: new URL(storeUrl).host, - 'stencil-cli': PACKAGE_INFO.version, - 'stencil-options': '{"get_template_file":true,"get_data_only":true}', - 'stencil-version': PACKAGE_INFO.config.stencil_version, - 'accept-encoding': 'identity', - 'stencil-custom-templates': '{"brand":{},"category":{},"page":{},"product":{}}', - }); - }); -}); diff --git a/server/plugins/renderer/renderer.module.spec.js b/server/plugins/renderer/renderer.module.spec.js index ebb1e9af..603ebf52 100644 --- a/server/plugins/renderer/renderer.module.spec.js +++ b/server/plugins/renderer/renderer.module.spec.js @@ -24,14 +24,6 @@ describe('Renderer Plugin', () => { port: 4000, username: 'testUser', token: '6832b1c755bb9de13aa8990216a69a7623043fd7', - customLayouts: { - brand: { - a: '/abc/', - }, - category: {}, - page: {}, - product: {}, - }, }, useCache: false, themePath: themeConfigManager.themePath, @@ -167,8 +159,6 @@ describe('Renderer Plugin', () => { 'stencil-options': '{"get_template_file":true,"get_data_only":true}', 'stencil-version': PACKAGE_INFO.config.stencil_version, 'accept-encoding': 'identity', - 'stencil-custom-templates': - '{"brand":{"a":"/abc/"},"category":{},"page":{},"product":{}}', }); }); @@ -264,8 +254,6 @@ describe('Renderer Plugin', () => { 'stencil-options': '{"get_template_file":true,"get_data_only":true}', 'stencil-version': PACKAGE_INFO.config.stencil_version, 'accept-encoding': 'identity', - 'stencil-custom-templates': - '{"brand":{"a":"/abc/"},"category":{},"page":{},"product":{}}', }); });