Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/lib/identifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ import { identifiers } from "./identifiers";
vi.mock(import("next/headers"), () => ({ headers: vi.fn() }));

describe("identifiers", () => {
it("should return IP from cf-connecting-ip header", async () => {
vi.mocked(headers).mockResolvedValue(
new Headers([["cf-connecting-ip", "172.16.0.1"]]),
);

const result = await identifiers();

expect(result.ip).toBe("172.16.0.1");
});

it("should prefer cf-connecting-ip over x-forwarded-for and x-real-ip", async () => {
vi.mocked(headers).mockResolvedValue(
new Headers([
["cf-connecting-ip", "172.16.0.1"],
["x-forwarded-for", "192.168.1.1"],
["x-real-ip", "10.0.0.1"],
]),
);

const result = await identifiers();

expect(result.ip).toBe("172.16.0.1");
});

it("should return IP from x-forwarded-for header", async () => {
vi.mocked(headers).mockResolvedValue(
new Headers([["x-forwarded-for", "192.168.1.1"]]),
Expand Down
1 change: 1 addition & 0 deletions app/lib/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function identifiers() {
const headersList = await headers();
return {
ip:
headersList.get("cf-connecting-ip") ??
headersList.get("x-forwarded-for")?.split(",")[0] ??
headersList.get("x-real-ip") ??
undefined,
Expand Down