From 1b9970c83468f0c53b7d6ea1dfaf5306487731d4 Mon Sep 17 00:00:00 2001 From: Steve Lamb Date: Fri, 29 Aug 2014 13:55:18 -0700 Subject: [PATCH] added ability to specify request options via config --- lib/mixpanel-node.js | 23 ++++++++++++++++++---- test/send_request.js | 45 ++++++++++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/lib/mixpanel-node.js b/lib/mixpanel-node.js index d3403df..e6be66b 100644 --- a/lib/mixpanel-node.js +++ b/lib/mixpanel-node.js @@ -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); @@ -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); } diff --git a/test/send_request.js b/test/send_request.js index 0f718fc..206ea06 100644 --- a/test/send_request.js +++ b/test/send_request.js @@ -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; @@ -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) {