Skip to content

Commit

Permalink
test(getRequestFingerprint): set explicit ip via headers
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 4, 2024
1 parent 2989326 commit b375975
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,16 @@ describeMatrix("utils", (t, { it, describe, expect }) => {
});

it("returns the same hash every time for same request", async () => {
t.app.use((event) => getRequestFingerprint(event, { hash: false }));
t.app.use((event) =>
getRequestFingerprint(event, { hash: false, xForwardedFor: true }),
);
for (let i = 0; i < 3; i++) {
const res = await t.fetch("/");
expect(await res.text()).toBe(t.target === "web" ? "" : "::1");
const res = await t.fetch("/", {
headers: {
"x-forwarded-for": "client-ip",
},
});
expect(await res.text()).toBe("client-ip");
}
});

Expand Down Expand Up @@ -230,18 +236,21 @@ describeMatrix("utils", (t, { it, describe, expect }) => {

it("uses user agent when available", async () => {
t.app.use((event) =>
getRequestFingerprint(event, { hash: false, userAgent: true }),
getRequestFingerprint(event, {
hash: false,
userAgent: true,
xForwardedFor: true,
}),
);

const res = await t.fetch("/", {
headers: {
"user-agent": "test-user-agent",
"x-forwarded-for": "client-ip",
},
});

expect(await res.text()).toBe(
t.target === "web" ? "test-user-agent" : "::1|test-user-agent",
);
expect(await res.text()).toBe("client-ip|test-user-agent");
});

it("uses x-forwarded-for ip when header set", async () => {
Expand Down

0 comments on commit b375975

Please sign in to comment.