Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Added tests for IP detection.

Mike Cao3d343991dc3d
diff --git a/src/declaration.d.ts b/src/declaration.d.ts
index 2f494dc28..986adf272 100644
--- a/src/declaration.d.ts
+++ b/src/declaration.d.ts
@@ -2,3 +2,4 @@ declare module 'cors';
declare module 'debug';
declare module 'chartjs-adapter-date-fns';
declare module 'md5';
+declare module 'request-ip';
diff --git a/src/lib/__tests__/detect.test.ts b/src/lib/__tests__/detect.test.ts
new file mode 100644
index 000000000..89b9c6c45
--- /dev/null
+++ b/src/lib/__tests__/detect.test.ts
@@ -0,0 +1,21 @@
+import * as detect from '../detect';
+
+const IP = '127.0.0.1';
+
+test('getIpAddress: Custom header', () => {
+ process.env.CLIENT_IP_HEADER = 'x-custom-ip-header';
+
+ expect(detect.getIpAddress({ headers: { 'x-custom-ip-header': IP } } as any)).toEqual(IP);
+});
+
+test('getIpAddress: CloudFlare header', () => {
+ expect(detect.getIpAddress({ headers: { 'cf-connecting-ip': IP } } as any)).toEqual(IP);
+});
+
+test('getIpAddress: Standard header', () => {
+ expect(detect.getIpAddress({ headers: { 'x-forwarded-for': IP } } as any)).toEqual(IP);
+});
+
+test('getIpAddress: No header', () => {
+ expect(detect.getIpAddress({ headers: {} } as any)).toEqual(null);
+});
diff --git a/src/lib/detect.ts b/src/lib/detect.ts
index cdef36704..af5c491f1 100644
--- a/src/lib/detect.ts
+++ b/src/lib/detect.ts
@@ -16,7 +16,7 @@ import { NextApiRequestCollect } from 'pages/api/send';
let lookup;
-export function getIpAddress(req) {
+export function getIpAddress(req: NextApiRequestCollect) {
// Custom header
if (req.headers[process.env.CLIENT_IP_HEADER]) {
return req.headers[process.env.CLIENT_IP_HEADER];
@@ -29,35 +29,35 @@ export function getIpAddress(req) {
return getClientIp(req);
}
-export function getDevice(screen, os) {
+export function getDevice(screen: string, os: string) {
if (!screen) return;
const [width] = screen.split('x');
if (DESKTOP_OS.includes(os)) {
- if (os === 'Chrome OS' || width < DESKTOP_SCREEN_WIDTH) {
+ if (os === 'Chrome OS' || +width < DESKTOP_SCREEN_WIDTH) {
return 'laptop';
}
return 'desktop';
} else if (MOBILE_OS.includes(os)) {
- if (os === 'Amazon OS' || width > MOBILE_SCREEN_WIDTH) {
+ if (os === 'Amazon OS' || +width > MOBILE_SCREEN_WIDTH) {
return 'tablet';
}
return 'mobile';
}
- if (width >= DESKTOP_SCREEN_WIDTH) {
+ if (+width >= DESKTOP_SCREEN_WIDTH) {
return 'desktop';
- } else if (width >= LAPTOP_SCREEN_WIDTH) {
+ } else if (+width >= LAPTOP_SCREEN_WIDTH) {
return 'laptop';
- } else if (width >= MOBILE_SCREEN_WIDTH) {
+ } else if (+width >= MOBILE_SCREEN_WIDTH) {
return 'tablet';
} else {
return 'mobile';
}
}
-function getRegionCode(country, region) {
+function getRegionCode(country: string, region: string) {
if (!country || !region) {
return undefined;
}
@@ -65,7 +65,7 @@ function getRegionCode(country, region) {
return region.includes('-') ? region : `${country}-${region}`;
}
-export async function getLocation(ip, req) {
+export async function getLocation(ip: string, req: NextApiRequestCollect) {
// Ignore local ips
if (await isLocalhost(ip)) {
return;