Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): allow passing mock country for geo-based redirects #5093

Merged
merged 3 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
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: 4 additions & 4 deletions src/utils/rules-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const getLanguage = function (headers) {
return 'en'
}

const getCountry = function () {
return 'us'
const getCountry = function (geoCountry) {
return geoCountry || 'us'
}
Copy link
Contributor Author

@tinfoil-knight tinfoil-knight Sep 26, 2022

Choose a reason for hiding this comment

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

Discussed w/ @kitop on Slack. Not sure why this fallback to 'us' exists but we decided to preserve it for backwards compatibility.


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 +80,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 || getCountry(geoCountry),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just simplify that instead of having a function for that?

Suggested change
'x-country': cookieValues.nf_country || getCountry(geoCountry),
'x-country': cookieValues.nf_country || geoCountry || 'us',

Copy link
Contributor Author

@tinfoil-knight tinfoil-knight Sep 27, 2022

Choose a reason for hiding this comment

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

@lukasholzer Made the suggested change.

...req.headers,
}

Expand Down
34 changes: 34 additions & 0 deletions tests/integration/100.command.dev.test.js
Original file line number Diff line number Diff line change
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