Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ability to specify request options via config #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/mixpanel-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ var create_client = function(token, config) {
}

var request_options = {
host: 'api.mixpanel.com',
host: 'api.mixpanel.com',
headers: {}
};

request_options = _extend(request_options, metrics.config.request_options);

if (metrics.config.test) { request_data.test = 1; }

var query = querystring.stringify(request_data);
Expand Down Expand Up @@ -518,13 +520,26 @@ var create_client = function(token, config) {
mixpanel client config
*/
metrics.set_config = function(config) {
for (var c in config) {
if (config.hasOwnProperty(c)) {
metrics.config[c] = config[c];
metrics.config = _extend(metrics.config, config);
};

/**
_extend(obj1, obj2)
---
Copies properties from obj2 to obj1
*/

_extend = function(obj1, obj2) {
for (var prop in obj2) {
if (obj2.hasOwnProperty(prop)) {
obj1[prop] = obj2[prop];
}
}

return obj1;
};


if (config) {
metrics.set_config(config);
}
Expand Down
45 changes: 29 additions & 16 deletions test/send_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ var Mixpanel = require('../lib/mixpanel-node'),
events = require('events');

exports.send_request = {

setUp: function(next) {
this.mixpanel = Mixpanel.init('token');

this.endpoint = "/track",
this.data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
};

this.expected_http_get = {
host: 'api.mixpanel.com',
headers: {},
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=0&verbose=0'
};


Sinon.stub(http, 'get');

this.http_emitter = new events.EventEmitter;
Expand All @@ -25,29 +43,24 @@ exports.send_request = {
},

"sends correct data": function(test) {
var endpoint = "/track",
data = {
event: 'test',
properties: {
key1: 'val1',
token: 'token',
time: 1346876621
}
};
this.mixpanel.send_request(this.endpoint, this.data);

var expected_http_get = {
host: 'api.mixpanel.com',
headers: {},
path: '/track?data=eyJldmVudCI6InRlc3QiLCJwcm9wZXJ0aWVzIjp7ImtleTEiOiJ2YWwxIiwidG9rZW4iOiJ0b2tlbiIsInRpbWUiOjEzNDY4NzY2MjF9fQ%3D%3D&ip=0&verbose=0'
};
test.ok(http.get.calledWithMatch(this.expected_http_get), "send_request didn't call http.get with correct arguments");

this.mixpanel.send_request(endpoint, data);
test.done();
},

"includes config parameters": function(test) {
this.mixpanel.set_config({ request_options: { scheme: 'test' } });
this.expected_http_get.scheme = 'test'

test.ok(http.get.calledWithMatch(expected_http_get), "send_request didn't call http.get with correct arguments");
this.mixpanel.send_request(this.endpoint, this.data);
test.ok(http.get.calledWithMatch(this.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) {
Expand Down