-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathposthog-proxy.controller.spec.ts
More file actions
154 lines (135 loc) · 5.75 KB
/
posthog-proxy.controller.spec.ts
File metadata and controls
154 lines (135 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { PosthogProxyController } from '../posthog-proxy.controller';
const POSTHOG_HOST = process.env.POSTHOG_HOST ?? 'https://eu.i.posthog.com';
function makeReply() {
const reply = {
status: jest.fn(),
header: jest.fn(),
send: jest.fn(),
};
reply.status.mockReturnValue(reply);
reply.header.mockReturnValue(reply);
return reply;
}
function makeReq(overrides: Partial<{ method: string; url: string; headers: Record<string, string>; body: unknown }> = {}) {
return {
method: 'POST',
url: '/ingest/e/',
headers: { 'content-type': 'application/json' },
body: { event: 'pageview', distinct_id: 'user_1' },
...overrides,
} as any;
}
function mockFetchResponse(status: number, body: string, contentType = 'application/json') {
return jest.spyOn(global, 'fetch').mockResolvedValueOnce({
status,
text: () => Promise.resolve(body),
headers: { get: (k: string) => (k === 'content-type' ? contentType : null) },
} as any);
}
describe('PosthogProxyController', () => {
let controller: PosthogProxyController;
beforeEach(() => {
controller = new PosthogProxyController();
jest.restoreAllMocks();
});
describe('URL routing', () => {
it('forwards /ingest/e/ → {POSTHOG_HOST}/e/', async () => {
const fetchSpy = mockFetchResponse(200, '{"status":1}');
await controller.proxy(makeReq({ url: '/ingest/e/' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(`${POSTHOG_HOST}/e/`, expect.any(Object));
});
it('forwards /ingest/decide → {POSTHOG_HOST}/decide', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(makeReq({ url: '/ingest/decide', method: 'POST' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(`${POSTHOG_HOST}/decide`, expect.any(Object));
});
it('strips /api/ingest prefix in production', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(makeReq({ url: '/api/ingest/batch/', method: 'POST' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(`${POSTHOG_HOST}/batch/`, expect.any(Object));
});
it('preserves query strings', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(makeReq({ url: '/ingest/decide?v=1&token=abc', method: 'POST' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(`${POSTHOG_HOST}/decide?v=1&token=abc`, expect.any(Object));
});
});
describe('request forwarding', () => {
it('forwards POST body as JSON', async () => {
const fetchSpy = mockFetchResponse(200, '{"status":1}');
const body = { event: 'click', distinct_id: 'u1' };
await controller.proxy(makeReq({ body, method: 'POST' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ method: 'POST', body: JSON.stringify(body) }),
);
});
it('sends no body for GET requests', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(makeReq({ method: 'GET', url: '/ingest/flags/' }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({ body: undefined }),
);
});
it('forwards content-type header', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(
makeReq({ headers: { 'content-type': 'application/json; charset=utf-8' } }),
makeReply() as any,
);
expect(fetchSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
headers: expect.objectContaining({ 'content-type': 'application/json; charset=utf-8' }),
}),
);
});
it('defaults content-type to application/json when absent', async () => {
const fetchSpy = mockFetchResponse(200, '{}');
await controller.proxy(makeReq({ headers: {} }), makeReply() as any);
expect(fetchSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
headers: expect.objectContaining({ 'content-type': 'application/json' }),
}),
);
});
});
describe('response forwarding', () => {
it('returns the upstream status code', async () => {
mockFetchResponse(200, '{"status":1}');
const reply = makeReply();
await controller.proxy(makeReq(), reply as any);
expect(reply.status).toHaveBeenCalledWith(200);
});
it('forwards upstream error status codes', async () => {
mockFetchResponse(400, '{"error":"bad request"}');
const reply = makeReply();
await controller.proxy(makeReq(), reply as any);
expect(reply.status).toHaveBeenCalledWith(400);
});
it('forwards the upstream response body', async () => {
mockFetchResponse(200, '{"status":1}');
const reply = makeReply();
await controller.proxy(makeReq(), reply as any);
expect(reply.send).toHaveBeenCalledWith('{"status":1}');
});
it('forwards the upstream content-type header', async () => {
mockFetchResponse(200, '{"status":1}', 'application/json; charset=utf-8');
const reply = makeReply();
await controller.proxy(makeReq(), reply as any);
expect(reply.header).toHaveBeenCalledWith('content-type', 'application/json; charset=utf-8');
});
it('defaults content-type to application/json when upstream omits it', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
status: 200,
text: () => Promise.resolve('{}'),
headers: { get: () => null },
} as any);
const reply = makeReply();
await controller.proxy(makeReq(), reply as any);
expect(reply.header).toHaveBeenCalledWith('content-type', 'application/json');
});
});
});