Skip to content

Commit 1e2ee6e

Browse files
committed
test: more test coverage for middleware i18n and for skipping normalization
1 parent 21df1aa commit 1e2ee6e

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

tests/integration/edge-handler.test.ts

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,16 +513,92 @@ describe('page router', () => {
513513
res.end()
514514
})
515515
ctx.cleanup?.push(() => origin.stop())
516+
516517
const response = await invokeEdgeFunction(ctx, {
517518
functions: ['___netlify-edge-handler-middleware'],
518519
origin,
519-
url: `/fr/json`,
520+
url: `/json`,
520521
})
521522
expect(response.status).toBe(200)
523+
const body = await response.json()
524+
525+
expect(body.requestUrlPathname).toBe('/json')
526+
expect(body.nextUrlPathname).toBe('/json')
527+
expect(body.nextUrlLocale).toBe('en')
528+
529+
const responseEn = await invokeEdgeFunction(ctx, {
530+
functions: ['___netlify-edge-handler-middleware'],
531+
origin,
532+
url: `/en/json`,
533+
})
534+
expect(responseEn.status).toBe(200)
535+
const bodyEn = await responseEn.json()
536+
537+
expect(bodyEn.requestUrlPathname).toBe('/json')
538+
expect(bodyEn.nextUrlPathname).toBe('/json')
539+
expect(bodyEn.nextUrlLocale).toBe('en')
522540

541+
const responseFr = await invokeEdgeFunction(ctx, {
542+
functions: ['___netlify-edge-handler-middleware'],
543+
origin,
544+
url: `/fr/json`,
545+
})
546+
expect(responseFr.status).toBe(200)
547+
const bodyFr = await responseFr.json()
548+
549+
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
550+
expect(bodyFr.nextUrlPathname).toBe('/json')
551+
expect(bodyFr.nextUrlLocale).toBe('fr')
552+
})
553+
554+
test.only<FixtureTestContext>('should preserve locale in request.nextUrl with skipMiddlewareUrlNormalize', async (ctx) => {
555+
await createFixture('middleware-i18n-skip-normalize', ctx)
556+
await runPlugin(ctx)
557+
const origin = await LocalServer.run(async (req, res) => {
558+
res.write(
559+
JSON.stringify({
560+
url: req.url,
561+
headers: req.headers,
562+
}),
563+
)
564+
res.end()
565+
})
566+
ctx.cleanup?.push(() => origin.stop())
567+
568+
const response = await invokeEdgeFunction(ctx, {
569+
functions: ['___netlify-edge-handler-middleware'],
570+
origin,
571+
url: `/json`,
572+
})
573+
expect(response.status).toBe(200)
523574
const body = await response.json()
524-
const bodyUrl = new URL(body.url)
525-
expect(bodyUrl.pathname).toBe('/fr/json')
526-
expect(body.locale).toBe('fr')
575+
576+
expect(body.requestUrlPathname).toBe('/json')
577+
expect(body.nextUrlPathname).toBe('/json')
578+
expect(body.nextUrlLocale).toBe('en')
579+
580+
const responseEn = await invokeEdgeFunction(ctx, {
581+
functions: ['___netlify-edge-handler-middleware'],
582+
origin,
583+
url: `/en/json`,
584+
})
585+
expect(responseEn.status).toBe(200)
586+
const bodyEn = await responseEn.json()
587+
588+
expect(bodyEn.requestUrlPathname).toBe('/en/json')
589+
expect(bodyEn.nextUrlPathname).toBe('/json')
590+
expect(bodyEn.nextUrlLocale).toBe('en')
591+
592+
const responseFr = await invokeEdgeFunction(ctx, {
593+
functions: ['___netlify-edge-handler-middleware'],
594+
origin,
595+
url: `/fr/json`,
596+
})
597+
expect(responseFr.status).toBe(200)
598+
const bodyFr = await responseFr.json()
599+
600+
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
601+
expect(bodyFr.nextUrlPathname).toBe('/json')
602+
expect(bodyFr.nextUrlLocale).toBe('fr')
527603
})
528604
})

0 commit comments

Comments
 (0)