forked from mixpanel/mixpanel-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_request.js
153 lines (114 loc) · 4.95 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
var Mixpanel,
Sinon = require('sinon'),
proxyquire = require('proxyquire'),
http = require('http'),
events = require('events'),
httpProxyOrig = process.env.HTTP_PROXY,
httpsProxyOrig = process.env.HTTPS_PROXY,
HttpsProxyAgent;
exports.send_request = {
setUp: function(next) {
Sinon.stub(http, 'get');
this.http_emitter = new events.EventEmitter;
this.res = new events.EventEmitter;
http.get.returns(this.http_emitter);
http.get.callsArgWith(1, this.res);
HttpsProxyAgent = Sinon.stub();
Mixpanel = proxyquire('../lib/mixpanel-node', {
'https-proxy-agent': HttpsProxyAgent
});
this.mixpanel = Mixpanel.init('token');
next();
},
tearDown: function(next) {
http.get.restore();
// restore proxy variables
process.env.HTTP_PROXY = httpProxyOrig;
process.env.HTTPS_PROXY = httpsProxyOrig;
next();
},
"sends correct data": function(test) {
var endpoint = "/track",
data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
};
var expected_http_get = {
host: 'api.mixpanel.com',
headers: {},
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=0&verbose=0'
};
this.mixpanel.send_request(endpoint, data);
test.ok(http.get.calledWithMatch(expected_http_get), "send_request didn't call http.get with correct arguments");
test.done();
},
"handles mixpanel errors": function(test) {
test.expect(1);
this.mixpanel.send_request("/track", { event: "test" }, function(e) {
test.equal(e.message, 'Mixpanel Server Error: 0', "error did not get passed back to callback");
test.done();
});
this.res.emit('data', '0');
this.res.emit('end');
},
"handles http.get errors": function(test) {
test.expect(1);
this.mixpanel.send_request("/track", { event: "test" }, function(e) {
test.equal(e, 'error', "error did not get passed back to callback");
test.done();
});
this.http_emitter.emit('error', 'error');
},
"uses correct hostname": function(test) {
var host = 'testhost.fakedomain';
var customHostnameMixpanel = Mixpanel.init('token', { host: host })
var expected_http_get = {
host: host
};
customHostnameMixpanel.send_request('', {});
test.ok(http.get.calledWithMatch(expected_http_get), "send_request didn't call http.get with correct hostname");
test.done();
},
"uses correct port": function(test) {
var host = 'testhost.fakedomain:1337';
var customHostnameMixpanel = Mixpanel.init('token', { host: host });
var expected_http_get = {
host: 'testhost.fakedomain',
port: 1337
};
customHostnameMixpanel.send_request('', {});
test.ok(http.get.calledWithMatch(expected_http_get), "send_request didn't call http.get with correct hostname and port");
test.done();
},
"uses HTTP_PROXY if set": function(test) {
HttpsProxyAgent.reset(); // Mixpanel is instantiated in setup, need to reset callcount
delete process.env.HTTPS_PROXY;
process.env.HTTP_PROXY = 'this.aint.real.http';
delete process.env.HTTPS_PROXY;
var proxyMixpanel = Mixpanel.init('token');
proxyMixpanel.send_request('', {});
test.ok(HttpsProxyAgent.calledOnce, "HttpsProxyAgent was not called when process.env.HTTP_PROXY was set");
var proxyPath = HttpsProxyAgent.firstCall.args[0];
test.ok(proxyPath === 'this.aint.real.http', "HttpsProxyAgent was not called with the correct proxy path");
var getConfig = http.get.firstCall.args[0];
test.ok(getConfig.agent !== undefined, "send_request didn't call http.get with agent");
test.done();
},
"uses HTTPS_PROXY if set": function(test) {
HttpsProxyAgent.reset(); // 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('', {});
test.ok(HttpsProxyAgent.calledOnce, "HttpsProxyAgent was not called when process.env.HTTPS_PROXY was set");
var proxyPath = HttpsProxyAgent.firstCall.args[0];
test.ok(proxyPath === 'this.aint.real.https', "HttpsProxyAgent was not called with the correct proxy path");
var getConfig = http.get.firstCall.args[0];
test.ok(getConfig.agent !== undefined, "send_request didn't call http.get with agent");
test.done();
}
};