Skip to content

Commit d510483

Browse files
committed
test: add tests for getRequestURL
1 parent b7126b8 commit d510483

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

test/utils.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
eventHandler,
1111
getMethod,
1212
getQuery,
13+
getRequestURL,
1314
} from "../src";
1415

1516
describe("", () => {
@@ -93,6 +94,46 @@ describe("", () => {
9394
});
9495
});
9596

97+
describe("getRequestURL", () => {
98+
const tests = [
99+
{ path: "/foo", url: "http://127.0.0.1/foo" },
100+
{ path: "/test", host: "example.com", url: "http://example.com/test" },
101+
{
102+
path: "/test",
103+
headers: [["x-forwarded-proto", "https"]],
104+
url: "https://127.0.0.1:80/test",
105+
},
106+
{
107+
path: "/test",
108+
headers: [["x-forwarded-host", "example.com"]],
109+
url: "http://example.com/test",
110+
},
111+
];
112+
for (const test of tests) {
113+
it("getRequestURL: " + JSON.stringify(test), async () => {
114+
app.use(
115+
"/",
116+
eventHandler((event) => {
117+
const url = getRequestURL(event);
118+
// @ts-ignore
119+
url.port = 80;
120+
return url;
121+
})
122+
);
123+
const req = request.get(test.path);
124+
if (test.host) {
125+
req.set("Host", test.host);
126+
}
127+
if (test.headers) {
128+
for (const header of test.headers) {
129+
req.set(header[0], header[1]);
130+
}
131+
}
132+
expect((await req).text).toBe(JSON.stringify(test.url));
133+
});
134+
}
135+
});
136+
96137
describe("assertMethod", () => {
97138
it("only allow head and post", async () => {
98139
app.use(

0 commit comments

Comments
 (0)