Skip to content

Commit 4680973

Browse files
committed
Handle invalid cases default locales and add more middleware tests
1 parent 20ae0bf commit 4680973

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

Diff for: packages/next-intl/src/middleware/utils.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ export function getKnownLocaleFromPathname<Locales extends AllLocales>(
8888
locales: Locales
8989
): Locales[number] | undefined {
9090
const pathLocaleCandidate = getLocaleFromPathname(pathname);
91-
const pathLocale = locales.includes(pathLocaleCandidate)
91+
const pathLocale = locales.find(
92+
(locale) => locale.toLowerCase() === pathLocaleCandidate.toLowerCase()
93+
)
9294
? pathLocaleCandidate
9395
: undefined;
9496
return pathLocale;

Diff for: packages/next-intl/test/middleware/middleware.test.tsx

+75-5
Original file line numberDiff line numberDiff line change
@@ -537,23 +537,63 @@ describe('prefix-based routing', () => {
537537
);
538538
});
539539

540-
it('redirects an invalid, upper-cased request for a localized route to the case-sensitive format', () => {
540+
it('redirects uppercase locale requests to case-sensitive defaults at the root', () => {
541+
middlewareWithPathnames(createMockRequest('/EN', 'en'));
542+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
543+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
544+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
545+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
546+
'http://localhost:3000/en/'
547+
);
548+
});
549+
550+
it('redirects uppercase locale requests to case-sensitive defaults for nested paths', () => {
551+
middlewareWithPathnames(createMockRequest('/EN/about', 'en'));
552+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
553+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
554+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
555+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
556+
'http://localhost:3000/en/about'
557+
);
558+
});
559+
560+
it('redirects uppercase locale requests for non-default locales at the root', () => {
541561
middlewareWithPathnames(createMockRequest('/DE-AT', 'de-AT'));
542562
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
543563
expect(MockedNextResponse.next).not.toHaveBeenCalled();
544564
expect(MockedNextResponse.redirect).toHaveBeenCalled();
545565
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
546-
'http://localhost:3000/de-AT'
566+
'http://localhost:3000/de-AT/'
547567
);
548568
});
549569

550-
it('redirects an invalid, lower-cased request for a localized route to the case-sensitive format', () => {
570+
it('redirects uppercase locale requests for non-default locales and nested paths', () => {
571+
middlewareWithPathnames(createMockRequest('/DE-AT/ueber', 'de-AT'));
572+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
573+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
574+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
575+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
576+
'http://localhost:3000/de-AT/ueber'
577+
);
578+
});
579+
580+
it('redirects lowercase locale requests for non-default locales to case-sensitive format at the root', () => {
551581
middlewareWithPathnames(createMockRequest('/de-at', 'de-AT'));
552582
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
553583
expect(MockedNextResponse.next).not.toHaveBeenCalled();
554584
expect(MockedNextResponse.redirect).toHaveBeenCalled();
555585
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
556-
'http://localhost:3000/de-AT'
586+
'http://localhost:3000/de-AT/'
587+
);
588+
});
589+
590+
it('redirects lowercase locale requests for non-default locales to case-sensitive format for nested paths', () => {
591+
middlewareWithPathnames(createMockRequest('/de-at/ueber', 'de-AT'));
592+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
593+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
594+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
595+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
596+
'http://localhost:3000/de-AT/ueber'
557597
);
558598
});
559599

@@ -976,7 +1016,7 @@ describe('prefix-based routing', () => {
9761016
describe('localePrefix: never', () => {
9771017
const middleware = createIntlMiddleware({
9781018
defaultLocale: 'en',
979-
locales: ['en', 'de'],
1019+
locales: ['en', 'de', 'de-AT'],
9801020
localePrefix: 'never'
9811021
});
9821022

@@ -1074,6 +1114,36 @@ describe('prefix-based routing', () => {
10741114
);
10751115
});
10761116

1117+
it('redirects requests with uppercase default locale in a nested path', () => {
1118+
middleware(createMockRequest('/EN/list'));
1119+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
1120+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
1121+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
1122+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
1123+
'http://localhost:3000/list'
1124+
);
1125+
});
1126+
1127+
it('redirects requests with uppercase non-default locale in a nested path', () => {
1128+
middleware(createMockRequest('/DE-AT/list'));
1129+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
1130+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
1131+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
1132+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
1133+
'http://localhost:3000/list'
1134+
);
1135+
});
1136+
1137+
it('redirects requests with lowercase non-default locale in a nested path', () => {
1138+
middleware(createMockRequest('/de-at/list'));
1139+
expect(MockedNextResponse.rewrite).not.toHaveBeenCalled();
1140+
expect(MockedNextResponse.next).not.toHaveBeenCalled();
1141+
expect(MockedNextResponse.redirect).toHaveBeenCalled();
1142+
expect(MockedNextResponse.redirect.mock.calls[0][0].toString()).toBe(
1143+
'http://localhost:3000/list'
1144+
);
1145+
});
1146+
10771147
it('rewrites requests for the root if a cookie exists with a non-default locale', () => {
10781148
middleware(createMockRequest('/', 'en', 'http://localhost:3000', 'de'));
10791149
expect(MockedNextResponse.next).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)