Skip to content

Commit

Permalink
fix(dev): allow passing mock country for geo-based redirects (#5093)
Browse files Browse the repository at this point in the history
* fix(dev): allow passing mock country for geo-based redirects

* refactor: remove getCountry function
  • Loading branch information
tinfoil-knight committed Sep 29, 2022
1 parent 138681d commit 09009f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/utils/proxy.js
Expand Up @@ -510,6 +510,7 @@ const startProxy = async function ({
jwtSecret: settings.jwtSecret,
jwtRoleClaim: settings.jwtRolePath,
configPath,
geoCountry,
})

const onRequestWithOptions = onRequest.bind(undefined, {
Expand Down
8 changes: 2 additions & 6 deletions src/utils/rules-proxy.js
Expand Up @@ -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 })
Expand Down Expand Up @@ -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,
}

Expand Down
34 changes: 34 additions & 0 deletions tests/integration/100.command.dev.test.js
Expand Up @@ -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: '<html>index</html>',
},
{
path: 'index-es.html',
content: '<html>index in spanish</html>',
},
])
.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, '<html>index</html>')
})

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, '<html>index in spanish</html>')
})
})
})

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
Expand Down

1 comment on commit 09009f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 223 MB

Please sign in to comment.