-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do redirect to locale and trailingSlash only if the route exist
- Loading branch information
Showing
3 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,54 @@ describe("CLI: serve", () => { | |
globalThis.mockConstants = undefined; | ||
}); | ||
|
||
it("should return 404 page without redirect to the locale if the page doesn't exist", async () => { | ||
const response = await testRequest( | ||
new Request("http://localhost:1234/not-found-page"), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should return 404 page without redirect to the trailingSlash if the page doesn't exist", async () => { | ||
globalThis.mockConstants = { | ||
...globalThis.mockConstants, | ||
CONFIG: { | ||
trailingSlash: true, | ||
}, | ||
}; | ||
const response = await testRequest( | ||
new Request("http://localhost:1234/es/not-found-page"), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should return 404 page without redirect to the locale and trailingSlash if the page doesn't exist", async () => { | ||
globalThis.mockConstants = { | ||
...globalThis.mockConstants, | ||
CONFIG: { | ||
trailingSlash: true, | ||
}, | ||
}; | ||
const response = await testRequest( | ||
new Request("http://localhost:1234/not-found-page"), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should return 404 page", async () => { | ||
const response = await testRequest( | ||
new Request("http://localhost:1234/es/not-found-page"), | ||
|
@@ -49,6 +97,30 @@ describe("CLI: serve", () => { | |
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should redirect to the correct locale", async () => { | ||
const response = await testRequest( | ||
new Request(`http://localhost:1234/somepage`), | ||
); | ||
expect(response.status).toBe(301); | ||
expect(response.headers.get("Location")).toBe("/es/somepage"); | ||
}); | ||
|
||
it("should redirect with trailingSlash", async () => { | ||
globalThis.mockConstants = { | ||
...globalThis.mockConstants, | ||
CONFIG: { | ||
trailingSlash: true, | ||
}, | ||
}; | ||
const response = await testRequest( | ||
new Request(`http://localhost:1234/es/somepage`), | ||
); | ||
expect(response.status).toBe(301); | ||
expect(response.headers.get("Location")).toBe( | ||
"http://localhost:1234/es/somepage/", | ||
); | ||
}); | ||
|
||
it("should return a page with layout and i18n", async () => { | ||
const response = await testRequest( | ||
new Request(`http://localhost:1234/es/somepage`), | ||
|
@@ -87,4 +159,30 @@ describe("CLI: serve", () => { | |
expect(response.status).toBe(200); | ||
expect(json).toEqual({ name: "Brisa", email: "[email protected]" }); | ||
}); | ||
|
||
it("should return 404 page if the api route does not exist", async () => { | ||
const response = await testRequest( | ||
new Request(`http:///localhost:1234/es/api/not-found`), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
|
||
it("should return 404 page if the api route exist but the method does not", async () => { | ||
const response = await testRequest( | ||
new Request(`http:///localhost:1234/es/api/example`, { | ||
method: "PUT", | ||
}), | ||
); | ||
const html = await response.text(); | ||
|
||
expect(response.status).toBe(404); | ||
expect(html).toContain('<title id="title">Page not found</title>'); | ||
expect(html).not.toContain('<title id="title">CUSTOM LAYOUT</title>'); | ||
expect(html).toContain("<h1>Page not found 404</h1>"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters