|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { onRequestPost } from './collect' |
| 3 | + |
| 4 | +const VALID_ID = 'G-1Z7TF66B8X' |
| 5 | + |
| 6 | +type Captured = { url: URL; init: RequestInit } |
| 7 | + |
| 8 | +// Drives onRequestPost with a stubbed global fetch, awaiting the fire-and-forget |
| 9 | +// upstream call (captured through waitUntil) so it has settled before assertions. |
| 10 | +async function invoke(opts: { |
| 11 | + query: string |
| 12 | + body: string |
| 13 | + env?: { MODREX_GA_MEASUREMENT_ID?: string } |
| 14 | + connectingIp?: string |
| 15 | +}) { |
| 16 | + const captured: Captured[] = [] |
| 17 | + const fetchMock = vi.fn(async (input: string | URL, init: RequestInit) => { |
| 18 | + captured.push({ url: new URL(String(input)), init }) |
| 19 | + return new Response(null, { status: 204 }) |
| 20 | + }) |
| 21 | + vi.stubGlobal('fetch', fetchMock) |
| 22 | + |
| 23 | + const headers = new Headers() |
| 24 | + if (opts.connectingIp) headers.set('CF-Connecting-IP', opts.connectingIp) |
| 25 | + const request = new Request(`https://modrex.net/api/collect${opts.query}`, { |
| 26 | + method: 'POST', |
| 27 | + headers, |
| 28 | + body: opts.body, |
| 29 | + }) |
| 30 | + |
| 31 | + const pending: Promise<unknown>[] = [] |
| 32 | + const res = await onRequestPost({ |
| 33 | + request, |
| 34 | + env: opts.env ?? {}, |
| 35 | + waitUntil: (p) => pending.push(p), |
| 36 | + }) |
| 37 | + await Promise.all(pending) |
| 38 | + return { res, captured, fetchMock } |
| 39 | +} |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + vi.unstubAllGlobals() |
| 43 | +}) |
| 44 | + |
| 45 | +describe('onRequestPost validation', () => { |
| 46 | + it('returns 400 when measurement_id or api_secret is missing', async () => { |
| 47 | + const a = await invoke({ query: '?api_secret=s', body: '{}' }) |
| 48 | + expect(a.res.status).toBe(400) |
| 49 | + |
| 50 | + const b = await invoke({ query: `?measurement_id=${VALID_ID}`, body: '{}' }) |
| 51 | + expect(b.res.status).toBe(400) |
| 52 | + }) |
| 53 | + |
| 54 | + it('returns 403 when the id does not match the pinned env id', async () => { |
| 55 | + const { res, fetchMock } = await invoke({ |
| 56 | + query: `?measurement_id=G-WRONG123&api_secret=s`, |
| 57 | + body: '{}', |
| 58 | + env: { MODREX_GA_MEASUREMENT_ID: VALID_ID }, |
| 59 | + }) |
| 60 | + expect(res.status).toBe(403) |
| 61 | + expect(fetchMock).not.toHaveBeenCalled() |
| 62 | + }) |
| 63 | + |
| 64 | + it('accepts the exact pinned id', async () => { |
| 65 | + const { res, captured } = await invoke({ |
| 66 | + query: `?measurement_id=${VALID_ID}&api_secret=s`, |
| 67 | + body: '{}', |
| 68 | + env: { MODREX_GA_MEASUREMENT_ID: VALID_ID }, |
| 69 | + }) |
| 70 | + expect(res.status).toBe(204) |
| 71 | + expect(captured).toHaveLength(1) |
| 72 | + }) |
| 73 | + |
| 74 | + it('falls back to a GA4 id shape check when no env id is pinned', async () => { |
| 75 | + const ok = await invoke({ query: `?measurement_id=${VALID_ID}&api_secret=s`, body: '{}' }) |
| 76 | + expect(ok.res.status).toBe(204) |
| 77 | + |
| 78 | + const bad = await invoke({ query: `?measurement_id=not-a-ga-id&api_secret=s`, body: '{}' }) |
| 79 | + expect(bad.res.status).toBe(403) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +describe('onRequestPost forwarding', () => { |
| 84 | + it('forwards to GA4 mp/collect carrying both credentials', async () => { |
| 85 | + const { captured } = await invoke({ |
| 86 | + query: `?measurement_id=${VALID_ID}&api_secret=secret`, |
| 87 | + body: '{}', |
| 88 | + }) |
| 89 | + const { url, init } = captured[0] |
| 90 | + expect(url.origin + url.pathname).toBe('https://www.google-analytics.com/mp/collect') |
| 91 | + expect(url.searchParams.get('measurement_id')).toBe(VALID_ID) |
| 92 | + expect(url.searchParams.get('api_secret')).toBe('secret') |
| 93 | + expect(init.method).toBe('POST') |
| 94 | + }) |
| 95 | + |
| 96 | + it('injects the real client IP as ip_override into a JSON body', async () => { |
| 97 | + const { captured } = await invoke({ |
| 98 | + query: `?measurement_id=${VALID_ID}&api_secret=s`, |
| 99 | + body: JSON.stringify({ client_id: '123', events: [] }), |
| 100 | + connectingIp: '203.0.113.7', |
| 101 | + }) |
| 102 | + const forwarded = JSON.parse(String(captured[0].init.body)) |
| 103 | + expect(forwarded.ip_override).toBe('203.0.113.7') |
| 104 | + expect(forwarded.client_id).toBe('123') |
| 105 | + }) |
| 106 | + |
| 107 | + it('does not add ip_override when the edge did not set CF-Connecting-IP', async () => { |
| 108 | + const { captured } = await invoke({ |
| 109 | + query: `?measurement_id=${VALID_ID}&api_secret=s`, |
| 110 | + body: JSON.stringify({ client_id: '123' }), |
| 111 | + }) |
| 112 | + expect(JSON.parse(String(captured[0].init.body))).not.toHaveProperty('ip_override') |
| 113 | + }) |
| 114 | + |
| 115 | + it('forwards a malformed body verbatim instead of throwing', async () => { |
| 116 | + const { res, captured } = await invoke({ |
| 117 | + query: `?measurement_id=${VALID_ID}&api_secret=s`, |
| 118 | + body: 'not json', |
| 119 | + connectingIp: '203.0.113.7', |
| 120 | + }) |
| 121 | + expect(res.status).toBe(204) |
| 122 | + expect(String(captured[0].init.body)).toBe('not json') |
| 123 | + }) |
| 124 | +}) |
0 commit comments