Skip to content

Commit

Permalink
added tests for send_request
Browse files Browse the repository at this point in the history
  • Loading branch information
carlsverre committed Sep 5, 2012
1 parent f198003 commit e7be94b
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/send_request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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',
port: 80,
headers: {},
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=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) {
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) {
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');
}
};

0 comments on commit e7be94b

Please sign in to comment.