diff --git a/src/utils/proxy.js b/src/utils/proxy.js index c2cfe5ccfe8..c49b5b26da9 100644 --- a/src/utils/proxy.js +++ b/src/utils/proxy.js @@ -510,6 +510,7 @@ const startProxy = async function ({ jwtSecret: settings.jwtSecret, jwtRoleClaim: settings.jwtRolePath, configPath, + geoCountry, }) const onRequestWithOptions = onRequest.bind(undefined, { diff --git a/src/utils/rules-proxy.js b/src/utils/rules-proxy.js index cb9ac8013d8..ee362c4574f 100644 --- a/src/utils/rules-proxy.js +++ b/src/utils/rules-proxy.js @@ -33,11 +33,7 @@ const getLanguage = function (headers) { return 'en' } -const getCountry = function () { - return 'us' -} - -const createRewriter = async function ({ configPath, distDir, jwtRoleClaim, jwtSecret, projectDir }) { +const createRewriter = async function ({ configPath, distDir, geoCountry, jwtRoleClaim, jwtSecret, projectDir }) { let matcher = null const redirectsFiles = [...new Set([path.resolve(distDir, '_redirects'), path.resolve(projectDir, '_redirects')])] let redirects = await parseRedirects({ redirectsFiles, configPath }) @@ -80,7 +76,7 @@ const createRewriter = async function ({ configPath, distDir, jwtRoleClaim, jwtS const cookieValues = cookie.parse(req.headers.cookie || '') const headers = { 'x-language': cookieValues.nf_lang || getLanguage(req.headers), - 'x-country': cookieValues.nf_country || getCountry(), + 'x-country': cookieValues.nf_country || geoCountry || 'us', ...req.headers, } diff --git a/tests/integration/100.command.dev.test.js b/tests/integration/100.command.dev.test.js index 40aae71f42a..ea780ac34d3 100644 --- a/tests/integration/100.command.dev.test.js +++ b/tests/integration/100.command.dev.test.js @@ -451,6 +451,40 @@ test('redirect with country cookie', async (t) => { }) }) +test('redirect with country flag', async (t) => { + await withSiteBuilder('site-with-country-flag', async (builder) => { + builder + .withContentFiles([ + { + path: 'index.html', + content: 'index', + }, + { + path: 'index-es.html', + content: 'index in spanish', + }, + ]) + .withRedirectsFile({ + redirects: [{ from: `/`, to: `/index-es.html`, status: '200!', condition: 'Country=ES' }], + }) + + await builder.buildAsync() + + // NOTE: default fallback for country is 'US' if no flag is provided + await withDevServer({ cwd: builder.directory }, async (server) => { + const response = await got(`${server.url}/`) + t.is(response.statusCode, 200) + t.is(response.body, 'index') + }) + + await withDevServer({ cwd: builder.directory, args: ['--country=ES'] }, async (server) => { + const response = await got(`${server.url}/`) + t.is(response.statusCode, 200) + t.is(response.body, 'index in spanish') + }) + }) +}) + test(`doesn't hang when sending a application/json POST request to function server`, async (t) => { await withSiteBuilder('site-with-functions', async (builder) => { const functionsPort = 6666