diff --git a/packages/core/__tests__/utils/nuxt/proxyUtils.spec.ts b/packages/core/__tests__/utils/nuxt/proxyUtils.spec.ts index d635806cd9..b595f32b4a 100644 --- a/packages/core/__tests__/utils/nuxt/proxyUtils.spec.ts +++ b/packages/core/__tests__/utils/nuxt/proxyUtils.spec.ts @@ -73,6 +73,56 @@ describe('[CORE - utils] _proxyUtils', () => { }); }); + it('adds a X-Forwarded-Host header', () => { + const integrationConfig = utils.getIntegrationConfig( + { + $config: { + middlewareUrl: 'http://localhost.com' + }, + req: { + headers: { + 'x-forwarded-host': 'myforward.vsf' + } + } + } as any, + {} + ); + + expect(integrationConfig).toEqual({ + axios: { + baseURL: expect.any(String), + headers: expect.objectContaining({ + Host: 'myforward.vsf' + }) + } + }); + }); + + it('adds a Host header', () => { + const integrationConfig = utils.getIntegrationConfig( + { + $config: { + middlewareUrl: 'http://localhost.com' + }, + req: { + headers: { + host: 'mywebsite.local' + } + } + } as any, + {} + ); + + expect(integrationConfig).toEqual({ + axios: { + baseURL: expect.any(String), + headers: expect.objectContaining({ + Host: 'mywebsite.local' + }) + } + }); + }); + /** * baseURL configuration cases matrix */ @@ -85,7 +135,7 @@ describe('[CORE - utils] _proxyUtils', () => { ]; const testMsg = '[baseUrl must be configured properly for] server: $server, middlewareUrl: $middlewareUrl, ssrMiddlewareUrl: $ssrMiddlewareUrl, expected: $expected'; - test.each(urlSetupCases)(testMsg, ({ server, middlewareUrl, ssrMiddlewareUrl, expected }) => { + it.each(urlSetupCases)(testMsg, ({ server, middlewareUrl, ssrMiddlewareUrl, expected }) => { process.server = server; const integrationConfig = utils.getIntegrationConfig( diff --git a/packages/docs/changelog/6869.js b/packages/docs/changelog/6869.js new file mode 100644 index 0000000000..edb223902a --- /dev/null +++ b/packages/docs/changelog/6869.js @@ -0,0 +1,7 @@ +module.exports = { + description: 'feat!: plugins pass X-Forwarded-Host header with fallback to the Host header in server to server communication #6869.', + link: 'https://github.com/vuestorefront/vue-storefront/pull/6869', + isBreaking: false, + author: 'Filip Jędrasik', + linkToGitHubAccount: 'https://github.com/Fifciu' +}; diff --git a/packages/middleware/__tests__/unit/helpers/getAgnosticStatusCode.spec.ts b/packages/middleware/__tests__/unit/helpers/getAgnosticStatusCode.spec.ts index 3ba0b19023..28d97f3df3 100644 --- a/packages/middleware/__tests__/unit/helpers/getAgnosticStatusCode.spec.ts +++ b/packages/middleware/__tests__/unit/helpers/getAgnosticStatusCode.spec.ts @@ -2,7 +2,7 @@ import getAgnosticStatusCode, { ApolloError, AxiosError, UnknownError } from '.. import bigObject from '../test-data/getAgnosticStatusCode'; const expectedStatusCode = 400; -const defaultCode = 200; +const defaultCode = 500; const networkErrorCode = 500; describe('[middleware-helpers] getAgnosticStatusCode', () => { @@ -30,7 +30,7 @@ describe('[middleware-helpers] getAgnosticStatusCode', () => { }); it('retrieves the status code from big object', () => { - const testData = {...bigObject, ...{statusCode: expectedStatusCode}} as unknown as UnknownError; + const testData = { ...bigObject, ...{ statusCode: expectedStatusCode } } as unknown as UnknownError; const statusCode = getAgnosticStatusCode(testData); expect(statusCode).toBe(expectedStatusCode); diff --git a/packages/middleware/src/createServer.ts b/packages/middleware/src/createServer.ts index 98fbdb2fab..24ff414d31 100644 --- a/packages/middleware/src/createServer.ts +++ b/packages/middleware/src/createServer.ts @@ -75,8 +75,9 @@ async function createServer(config: MiddlewareConfig): Promise { const platformResponse = await apiFunction(...req.body); res.send(platformResponse); } catch (error) { + consola.error(error); res.status(getAgnosticStatusCode(error)); - res.send(error); + res.send('ServerError: Response not successful. Please, check server logs for more details.'); } }); diff --git a/packages/middleware/src/helpers/getAgnosticStatusCode.ts b/packages/middleware/src/helpers/getAgnosticStatusCode.ts index a122d91ed2..18e3eace6a 100644 --- a/packages/middleware/src/helpers/getAgnosticStatusCode.ts +++ b/packages/middleware/src/helpers/getAgnosticStatusCode.ts @@ -62,7 +62,7 @@ function getAgnosticStatusCode(error: AxiosError | ApolloError | UnknownError): return getAxiosStatusCode(error as AxiosError) || getApolloStatusCode(error as ApolloError) || obtainStatusCode(error as UnknownError) || - 200; + 500; } export default getAgnosticStatusCode;