Skip to content

Commit

Permalink
test: more test coverage for middleware i18n and for skipping normali…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
orinokai committed Jun 14, 2024
1 parent 21df1aa commit 1e2ee6e
Showing 1 changed file with 80 additions and 4 deletions.
84 changes: 80 additions & 4 deletions tests/integration/edge-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,92 @@ describe('page router', () => {
res.end()
})
ctx.cleanup?.push(() => origin.stop())

const response = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/fr/json`,
url: `/json`,
})
expect(response.status).toBe(200)
const body = await response.json()

expect(body.requestUrlPathname).toBe('/json')
expect(body.nextUrlPathname).toBe('/json')
expect(body.nextUrlLocale).toBe('en')

const responseEn = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/en/json`,
})
expect(responseEn.status).toBe(200)
const bodyEn = await responseEn.json()

expect(bodyEn.requestUrlPathname).toBe('/json')
expect(bodyEn.nextUrlPathname).toBe('/json')
expect(bodyEn.nextUrlLocale).toBe('en')

const responseFr = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/fr/json`,
})
expect(responseFr.status).toBe(200)
const bodyFr = await responseFr.json()

expect(bodyFr.requestUrlPathname).toBe('/fr/json')
expect(bodyFr.nextUrlPathname).toBe('/json')
expect(bodyFr.nextUrlLocale).toBe('fr')
})

test.only<FixtureTestContext>('should preserve locale in request.nextUrl with skipMiddlewareUrlNormalize', async (ctx) => {
await createFixture('middleware-i18n-skip-normalize', 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: `/json`,
})
expect(response.status).toBe(200)
const body = await response.json()
const bodyUrl = new URL(body.url)
expect(bodyUrl.pathname).toBe('/fr/json')
expect(body.locale).toBe('fr')

expect(body.requestUrlPathname).toBe('/json')
expect(body.nextUrlPathname).toBe('/json')
expect(body.nextUrlLocale).toBe('en')

const responseEn = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/en/json`,
})
expect(responseEn.status).toBe(200)
const bodyEn = await responseEn.json()

expect(bodyEn.requestUrlPathname).toBe('/en/json')
expect(bodyEn.nextUrlPathname).toBe('/json')
expect(bodyEn.nextUrlLocale).toBe('en')

const responseFr = await invokeEdgeFunction(ctx, {
functions: ['___netlify-edge-handler-middleware'],
origin,
url: `/fr/json`,
})
expect(responseFr.status).toBe(200)
const bodyFr = await responseFr.json()

expect(bodyFr.requestUrlPathname).toBe('/fr/json')
expect(bodyFr.nextUrlPathname).toBe('/json')
expect(bodyFr.nextUrlLocale).toBe('fr')
})
})

0 comments on commit 1e2ee6e

Please sign in to comment.