-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathsend_request.js
129 lines (101 loc) · 3.87 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
var Mixpanel = require('../lib/mixpanel-node'),
Sinon = require('sinon'),
http = require('http'),
events = require('events');
exports.send_request = {
setUp: function(next) {
this.mixpanel = Mixpanel.init('token');
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);
next();
},
tearDown: function(next) {
http.get.restore();
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 mixpanel verbose errors": function(test) {
test.expect(1);
this.mixpanel.config.verbose = true;
this.mixpanel.send_request("/track", { event: "test" }, function(e) {
test.equal(e.message, 'Mixpanel Server Error: Foobar');
test.done();
});
this.res.emit('data', JSON.stringify({
status: 0,
error: 'Foobar'
}));
this.res.emit('end');
this.mixpanel.config.verbose = false;
},
"handles mixpanel verbose errors with non-verbose response": function(test) {
test.expect(1);
this.mixpanel.config.verbose = true;
this.mixpanel.send_request("/track", { event: "test" }, function(e) {
test.equal(e.message, 'Could not parse response from Mixpanel: ""');
test.done();
});
this.res.emit('data', '');
this.res.emit('end');
this.mixpanel.config.verbose = false;
},
"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();
}
};