Skip to content

Commit 4f30eca

Browse files
authored
test: add unit tests for getRequestURL function
1 parent 7786a7c commit 4f30eca

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

test/utils/getRequestURL.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { getRequestURL } from "../../src/utils.js";
2+
3+
describe("getRequestURL", () => {
4+
it("should use getURL() when available (pseudo API)", () => {
5+
const req = { getURL: () => "/pseudo-url" };
6+
expect(getRequestURL(req)).toBe("/pseudo-url");
7+
});
8+
9+
it("should return req.url when no special API is present", () => {
10+
const req = { url: "/basic" };
11+
expect(getRequestURL(req)).toBe("/basic");
12+
});
13+
14+
describe("Fastify (req.originalUrl present)", () => {
15+
it("should return req.originalUrl when req.url was not modified", () => {
16+
// Fastify decodes req.url, so originalUrl preserves encoding
17+
const req = {
18+
url: "/path with spaces",
19+
originalUrl: "/path%20with%20spaces",
20+
};
21+
expect(getRequestURL(req)).toBe("/path%20with%20spaces");
22+
});
23+
24+
it("should return encoded req.url when middleware (e.g. connect-history-api-fallback) modified it", () => {
25+
// Middleware changed req.url to a different path
26+
const req = { url: "/index.html", originalUrl: "/path%20with%20spaces" };
27+
expect(getRequestURL(req)).toBe("/index.html");
28+
});
29+
30+
it("should encode req.url when middleware sets a path with special chars", () => {
31+
const req = { url: "/new path", originalUrl: "/old%20path" };
32+
expect(getRequestURL(req)).toBe("/new%20path");
33+
});
34+
35+
it("should preserve query string from modified req.url", () => {
36+
const req = {
37+
url: "/index.html?redirect=/other",
38+
originalUrl: "/some%20page",
39+
};
40+
expect(getRequestURL(req)).toBe("/index.html?redirect=/other");
41+
});
42+
});
43+
});

0 commit comments

Comments
 (0)