Skip to content

Commit 10bc7ce

Browse files
refactor(request): align jsdocs with getRequestHost (#1316)
1 parent 5da2989 commit 10bc7ce

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

docs/2.utils/1.request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Get the request hostname.
139139

140140
If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
141141

142-
If no host header is found, it will default to "localhost".
142+
If no host header is found, it will return an empty string.
143143

144144
**Example:**
145145

src/utils/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ export function assertMethod(
319319
*
320320
* If `xForwardedHost` is `true`, it will use the `x-forwarded-host` header if it exists.
321321
*
322-
* If no host header is found, it will default to "localhost".
322+
* If no host header is found, it will return an empty string.
323323
*
324324
* @example
325325
* app.get("/", (event) => {

test/utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
assertMethod,
66
getQuery,
77
getRequestURL,
8+
getRequestHost,
89
getRequestIP,
910
getRequestFingerprint,
1011
handleCacheHeaders,
@@ -83,6 +84,31 @@ describeMatrix("utils", (t, { it, describe, expect }) => {
8384
});
8485
});
8586

87+
describe("getRequestHost", () => {
88+
it("returns host header value", async () => {
89+
t.app.get("/", (event) => getRequestHost(event));
90+
const res = await t.fetch("/");
91+
// In test environments, host header is set by the HTTP client
92+
expect(await res.text()).toBeTruthy();
93+
});
94+
95+
it("uses x-forwarded-host when enabled", async () => {
96+
t.app.get("/", (event) => getRequestHost(event, { xForwardedHost: true }));
97+
const res = await t.fetch("/", {
98+
headers: { "x-forwarded-host": "proxy.example.com" },
99+
});
100+
expect(await res.text()).toBe("proxy.example.com");
101+
});
102+
103+
it("uses first value from x-forwarded-host with multiple entries", async () => {
104+
t.app.get("/", (event) => getRequestHost(event, { xForwardedHost: true }));
105+
const res = await t.fetch("/", {
106+
headers: { "x-forwarded-host": "first.com, second.com" },
107+
});
108+
expect(await res.text()).toBe("first.com");
109+
});
110+
});
111+
86112
describe("getRequestURL", () => {
87113
const tests = [
88114
"http://localhost/foo?bar=baz",

0 commit comments

Comments
 (0)