forked from mixpanel/mixpanel-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_request.js
310 lines (266 loc) · 10.4 KB
/
send_request.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
let Mixpanel;
const proxyquire = require('proxyquire');
const https = require('https');
const events = require('events');
const httpProxyOrig = process.env.HTTP_PROXY;
const httpsProxyOrig = process.env.HTTPS_PROXY;
let HttpsProxyAgent;
describe("send_request", () => {
let mixpanel;
let http_emitter;
let res;
beforeEach(() => {
HttpsProxyAgent = vi.fn();
Mixpanel = proxyquire('../lib/mixpanel-node', {
'https-proxy-agent': HttpsProxyAgent,
});
http_emitter = new events.EventEmitter();
res = new events.EventEmitter();
vi.spyOn(https, 'request')
.mockImplementation((_, cb) => {
cb(res);
return http_emitter;
})
http_emitter.write = vi.fn();
http_emitter.end = vi.fn();
mixpanel = Mixpanel.init('token');
return () => {
https.request.mockRestore();
// restore proxy variables
process.env.HTTP_PROXY = httpProxyOrig;
process.env.HTTPS_PROXY = httpsProxyOrig;
}
});
it("sends correct data on GET", () => {
var endpoint = "/track",
data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
},
expected_http_request = {
method: 'GET',
host: 'api.mixpanel.com',
headers: {},
path: '/track?ip=0&verbose=0&data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D'
};
mixpanel.send_request({ method: 'get', endpoint: endpoint, data: data });
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function)
);
expect(http_emitter.end).toHaveBeenCalledTimes(1);
expect(http_emitter.write).toHaveBeenCalledTimes(0);
});
it("defaults to GET", () => {
var endpoint = "/track",
data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
},
expected_http_request = {
method: 'GET',
host: 'api.mixpanel.com',
headers: {},
path: '/track?ip=0&verbose=0&data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D'
};
mixpanel.send_request({ endpoint: endpoint, data: data }); // method option not defined
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
});
it("sends correct data on POST", () => {
var endpoint = "/track",
data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
},
expected_http_request = {
method: 'POST',
host: 'api.mixpanel.com',
headers: expect.any(Object),
path: '/track?ip=0&verbose=0'
},
expected_http_request_body = "data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ==";
mixpanel.send_request({ method: 'post', endpoint: endpoint, data: data });
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
expect(http_emitter.end).toHaveBeenCalledTimes(1);
expect(http_emitter.write).toHaveBeenCalledWith(expected_http_request_body);
});
it("sets ip=1 when geolocate option is on", () => {
mixpanel.set_config({ geolocate: true });
mixpanel.send_request({ method: "get", endpoint: "/track", event: "test", data: {} });
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining({
path: expect.stringContaining('ip=1'),
}),
expect.any(Function),
);
});
it("handles mixpanel errors", () => {
mixpanel.send_request({ endpoint: "/track", data: { event: "test" } }, function(e) {
expect(e.message).toBe('Mixpanel Server Error: 0')
});
res.emit('data', '0');
res.emit('end');
});
it("handles https.request errors", () => {
mixpanel.send_request({ endpoint: "/track", data: { event: "test" } }, function(e) {
expect(e).toBe('error');
});
http_emitter.emit('error', 'error');
});
it("default use keepAlive agent", () => {
var agent = new https.Agent({ keepAlive: false });
var httpsStub = {
request: vi.fn().mockImplementation((_, cb) => {
cb(res)
return http_emitter;
}),
Agent: vi.fn().mockReturnValue(agent),
};
// force SDK not use `undefined` string to initialize proxy-agent
delete process.env.HTTP_PROXY
delete process.env.HTTPS_PROXY
Mixpanel = proxyquire('../lib/mixpanel-node', {
'https': httpsStub
});
var proxyMixpanel = Mixpanel.init('token');
proxyMixpanel.send_request({ endpoint: '', data: {} });
var getConfig = httpsStub.request.mock.calls[0][0];
var agentOpts = httpsStub.Agent.mock.calls[0][0];
expect(agentOpts.keepAlive).toBe(true);
expect(getConfig.agent).toBe(agent);
});
it("uses correct hostname", () => {
var host = 'testhost.fakedomain';
var customHostnameMixpanel = Mixpanel.init('token', { host: host });
var expected_http_request = {
host: host
};
customHostnameMixpanel.send_request({ endpoint: "", data: {} });
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
});
it("uses correct port", () => {
var host = 'testhost.fakedomain:1337';
var customHostnameMixpanel = Mixpanel.init('token', { host: host });
var expected_http_request = {
host: 'testhost.fakedomain',
port: 1337
};
customHostnameMixpanel.send_request({ endpoint: "", data: {} });
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
});
it("uses correct path", () => {
var host = 'testhost.fakedomain';
var customPath = '/mypath';
var customHostnameMixpanel = Mixpanel.init('token', {
host,
path: customPath,
});
var expected_http_request = {
host,
path: '/mypath?ip=0&verbose=0&data=e30%3D',
};
customHostnameMixpanel.send_request({endpoint: "", data: {}});
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
});
it("combines custom path and endpoint", () => {
var host = 'testhost.fakedomain';
var customPath = '/mypath';
var customHostnameMixpanel = Mixpanel.init('token', {
host,
path: customPath,
});
var expected_http_request = {
host,
path: '/mypath/track?ip=0&verbose=0&data=e30%3D',
};
customHostnameMixpanel.send_request({endpoint: '/track', data: {}});
expect(https.request).toHaveBeenCalledWith(
expect.objectContaining(expected_http_request),
expect.any(Function),
);
});
it("uses HTTP_PROXY if set", () => {
HttpsProxyAgent.mockReset(); // Mixpanel is instantiated in setup, need to reset callcount
delete process.env.HTTPS_PROXY;
process.env.HTTP_PROXY = 'this.aint.real.https';
var proxyMixpanel = Mixpanel.init('token');
proxyMixpanel.send_request({ endpoint: '', data: {} });
expect(HttpsProxyAgent).toHaveBeenCalledTimes(1);
var agentOpts = HttpsProxyAgent.mock.calls[0][0];
expect(agentOpts.pathname).toBe('this.aint.real.https');
expect(agentOpts.keepAlive).toBe(true);
var getConfig = https.request.mock.calls[0][0];
expect(getConfig.agent).toBeTruthy();
});
it("uses HTTPS_PROXY if set", () => {
HttpsProxyAgent.mockReset(); // Mixpanel is instantiated in setup, need to reset callcount
delete process.env.HTTP_PROXY;
process.env.HTTPS_PROXY = 'this.aint.real.https';
var proxyMixpanel = Mixpanel.init('token');
proxyMixpanel.send_request({ endpoint: '', data: {} });
expect(HttpsProxyAgent).toHaveBeenCalledTimes(1);
var proxyOpts = HttpsProxyAgent.mock.calls[0][0];
expect(proxyOpts.pathname).toBe('this.aint.real.https');
var getConfig = https.request.mock.calls[0][0];
expect(getConfig.agent).toBeTruthy();
});
it("requires credentials for import requests", () => {
expect(() => {
mixpanel.send_request({
endpoint: `/import`,
data: {event: `test event`},
})
}).toThrowError(
/The Mixpanel Client needs a Mixpanel API Secret when importing old events/,
)
});
it("sets basic auth header if API secret is provided", () => {
mixpanel.set_config({secret: `foobar`});
mixpanel.send_request({
endpoint: `/import`,
data: {event: `test event`},
});
expect(https.request).toHaveBeenCalledTimes(1);
expect(https.request.mock.calls[0][0].headers).toEqual({
'Authorization': `Basic Zm9vYmFyOg==`, // base64 of "foobar:"
})
});
it("still supports import with api_key (legacy)", () => {
mixpanel.set_config({key: `barbaz`});
mixpanel.send_request({
endpoint: `/import`,
data: {},
});
expect(https.request).toHaveBeenCalledTimes(1);
expect(https.request.mock.calls[0][0].path).toBe(
`/import?ip=0&verbose=0&data=e30%3D&api_key=barbaz`,
);
});
});