Skip to content

Commit

Permalink
test: add test case for middleware redirect to same path just changin…
Browse files Browse the repository at this point in the history
…g locale to defalt
  • Loading branch information
pieh committed Oct 1, 2024
1 parent a3240f3 commit 88fc614
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/fixtures/middleware-i18n/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export async function middleware(request) {
return Response.redirect(new URL('/new-home#fragment', url))
}

if (url.locale !== 'en' && url.pathname === '/redirect-to-same-page-but-default-locale') {
url.locale = 'en'
return Response.redirect(url)
}

if (url.pathname.includes('/json')) {
return NextResponse.json({
requestUrlPathname: new URL(request.url).pathname,
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/edge-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,30 @@ describe('page router', () => {
expect(response.status).toBe(302)
})

test<FixtureTestContext>('should support redirects to default locale without changing path', async (ctx) => {
await createFixture('middleware-i18n', ctx)
await runPlugin(ctx)
const origin = await LocalServer.run(async (req, res) => {
res.write(
JSON.stringify({
url: req.url,
headers: req.headers,
}),
)
res.end()
})
ctx.cleanup?.push(() => origin.stop())
const response = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/fr/redirect-to-same-page-but-default-locale`,
redirect: 'manual',
})
const url = new URL(response.headers.get('location') ?? '', 'http://n/')
expect(url.pathname).toBe('/redirect-to-same-page-but-default-locale')
expect(response.status).toBe(302)
})

test<FixtureTestContext>('should preserve locale in request.nextUrl', async (ctx) => {
await createFixture('middleware-i18n', ctx)
await runPlugin(ctx)
Expand Down

0 comments on commit 88fc614

Please sign in to comment.