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
115 changes: 115 additions & 0 deletions __tests__/ipinfo-plus-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { Request, Response, NextFunction } from "express";
import { ipinfoPlus, originatingIPSelector } from "../src/index";
import { IPinfoPlus } from "node-ipinfo/dist/src/common";

// Mock the node-ipinfo module
const mockLookupIp = jest.fn();
jest.mock("node-ipinfo", () => ({
IPinfoPlusWrapper: jest.fn().mockImplementation(() => ({
lookupIp: mockLookupIp
}))
}));

describe("ipinfoPlusMiddleware", () => {
const mockToken = "test_token";
let mockReq: Partial<Request> & { ipinfo?: IPinfoPlus };
let mockRes: Partial<Response>;
let next: NextFunction;

beforeEach(() => {
// Reset mocks before each test
jest.clearAllMocks();

// Set up default mock response
mockLookupIp.mockResolvedValue({
ip: "1.2.3.4",
city: "New York",
country: "US",
hostname: "example.com",
org: "Example Org"
});

// Setup mock request/response
mockReq = {
ip: "1.2.3.4",
headers: { "x-forwarded-for": "5.6.7.8, 10.0.0.1" },
header: jest.fn((name: string) => {
if (name.toLowerCase() === "set-cookie") {
return ["mock-cookie-1", "mock-cookie-2"];
}
if (name.toLowerCase() === "x-forwarded-for") {
return "5.6.7.8, 10.0.0.1";
}
return undefined;
}) as jest.MockedFunction<
((name: "set-cookie") => string[] | undefined) &
((name: string) => string | undefined)
>
};
mockRes = {};
next = jest.fn();
});

it("should use defaultIPSelector when no custom selector is provided", async () => {
const middleware = ipinfoPlus({ token: mockToken });

await middleware(mockReq, mockRes, next);

expect(mockLookupIp).toHaveBeenCalledWith("1.2.3.4");
expect(mockReq.ipinfo).toEqual({
ip: "1.2.3.4",
city: "New York",
country: "US",
hostname: "example.com",
org: "Example Org"
});
expect(next).toHaveBeenCalled();
});

it("should use originatingIPSelector when specified", async () => {
mockLookupIp.mockResolvedValue({
ip: "5.6.7.8",
city: "San Francisco",
country: "US",
hostname: "proxy.example.com",
org: "Proxy Org"
});

const middleware = ipinfoPlus({
token: mockToken,
ipSelector: originatingIPSelector
});

await middleware(mockReq, mockRes, next);

expect(mockLookupIp).toHaveBeenCalledWith("5.6.7.8");
expect(mockReq.ipinfo?.ip).toBe("5.6.7.8");
});

it("should use custom ipSelector function when provided", async () => {
const customSelector = jest.fn().mockReturnValue("9.10.11.12");

const middleware = ipinfoPlus({
token: mockToken,
ipSelector: customSelector
});

await middleware(mockReq, mockRes, next);

expect(customSelector).toHaveBeenCalledWith(mockReq);
expect(mockLookupIp).toHaveBeenCalledWith("9.10.11.12");
});

it("should throw IPinfo API errors", async () => {
const errorMessage = "API rate limit exceeded";
mockLookupIp.mockRejectedValueOnce(new Error(errorMessage));
const middleware = ipinfoPlus({ token: mockToken });

await expect(middleware(mockReq, mockRes, next)).rejects.toThrow(
errorMessage
);

expect(mockReq.ipinfo).toBeUndefined();
expect(next).not.toHaveBeenCalled();
});
});
33 changes: 30 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import {
IPinfoWrapper,
IPinfo,
IPinfoLiteWrapper,
IPinfoCoreWrapper
IPinfoCoreWrapper,
IPinfoPlusWrapper
} from "node-ipinfo";
import defaultIPSelector from "./ip-selector/default-ip-selector";
import originatingIPSelector from "./ip-selector/originating-ip-selector";
import { IPinfoLite, IPinfoCore, IPBogon } from "node-ipinfo/dist/src/common";
import {
IPinfoLite,
IPinfoCore,
IPinfoPlus,
IPBogon
} from "node-ipinfo/dist/src/common";

type MiddlewareOptions = {
token?: string;
Expand Down Expand Up @@ -75,10 +81,31 @@ const ipinfoCoreMiddleware = ({
};
};

const ipinfoPlusMiddleware = ({
token = "",
cache,
timeout,
ipSelector
}: MiddlewareOptions = {}) => {
const ipinfo = new IPinfoPlusWrapper(token, cache, timeout);
if (ipSelector == null || typeof ipSelector !== "function") {
ipSelector = defaultIPSelector;
}
return async (req: any, _: any, next: any) => {
const ip = ipSelector?.(req) ?? defaultIPSelector(req);
if (ip) {
const ipInfo: IPinfoPlus | IPBogon = await ipinfo.lookupIp(ip);
req.ipinfo = ipInfo;
}
next();
};
};

export default ipinfoMiddleware;
export {
defaultIPSelector,
originatingIPSelector,
ipinfoLiteMiddleware as ipinfoLite,
ipinfoCoreMiddleware as ipinfoCore
ipinfoCoreMiddleware as ipinfoCore,
ipinfoPlusMiddleware as ipinfoPlus
};