Skip to content

Commit

Permalink
test: add tests for getRequestURL
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 15, 2023
1 parent b7126b8 commit d510483
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
eventHandler,
getMethod,
getQuery,
getRequestURL,
} from "../src";

describe("", () => {
Expand Down Expand Up @@ -93,6 +94,46 @@ describe("", () => {
});
});

describe("getRequestURL", () => {
const tests = [
{ path: "/foo", url: "http://127.0.0.1/foo" },
{ path: "/test", host: "example.com", url: "http://example.com/test" },
{
path: "/test",
headers: [["x-forwarded-proto", "https"]],
url: "https://127.0.0.1:80/test",
},
{
path: "/test",
headers: [["x-forwarded-host", "example.com"]],
url: "http://example.com/test",
},
];
for (const test of tests) {
it("getRequestURL: " + JSON.stringify(test), async () => {
app.use(
"/",
eventHandler((event) => {
const url = getRequestURL(event);
// @ts-ignore
url.port = 80;
return url;
})
);
const req = request.get(test.path);
if (test.host) {
req.set("Host", test.host);
}
if (test.headers) {
for (const header of test.headers) {
req.set(header[0], header[1]);
}
}
expect((await req).text).toBe(JSON.stringify(test.url));
});
}
});

describe("assertMethod", () => {
it("only allow head and post", async () => {
app.use(
Expand Down

0 comments on commit d510483

Please sign in to comment.