UNPKG

6.28 kBJavaScriptView Raw
1
2var _ = require("underscore");
3var request = require("request");
4var qs = require("querystring");
5var uuid = require("uuid");
6var should = require("should");
7var sinon = require("sinon");
8var url = require("url");
9
10var ua = require("../lib/index.js");
11var utils = require("../lib/utils.js")
12var config = require("../lib/config.js")
13
14
15describe("ua", function () {
16
17 describe("#send", function () {
18 var post;
19
20 beforeEach(function () {
21 post = sinon.stub(request, "post").callsArg(2);
22 });
23
24 afterEach(function () {
25 post.restore()
26 });
27
28 it("should immidiately return with an empty queue", function () {
29 var visitor = ua();
30 var fn = sinon.spy();
31
32 visitor.send(fn);
33
34 post.called.should.equal(false, "no request should have been sent")
35 fn.calledOnce.should.equal(true, "callback should have been called once")
36 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
37 fn.args[0].should.eql([null, 0], "no error, no requests");
38 });
39
40 it("should include data in POST body", function (done) {
41 var paramSets = [
42 {first: "123"}
43 ]
44
45 var fn = sinon.spy(function () {
46 fn.calledOnce.should.equal(true, "callback should have been called once")
47 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
48 fn.args[0].should.eql([null, 1], "no error, 1 requests");
49
50 post.callCount.should.equal(paramSets.length, "each param set should have been POSTed");
51
52 for (var i = 0; i < paramSets.length; i++) {
53 var params = paramSets[i];
54 var args = post.args[i];
55
56 var parsedUrl = url.parse(args[0]);
57
58 Math.random(); // I have absolutely no idea why it fails unless there was some processing to be done after url.parse…
59
60 (parsedUrl.protocol + "//" + parsedUrl.host).should.equal(config.hostname);
61 args[1].body.should.equal(qs.stringify(params));
62 }
63
64 done();
65 });
66
67 var visitor = ua();
68 visitor._queue.push.apply(visitor._queue, paramSets);
69 visitor.send(fn);
70 });
71
72 it("should send individual requests when batchting is false", function(done) {
73 var paramSets = [
74 {first: Math.random()},
75 {second: Math.random()},
76 {third: Math.random()}
77 ]
78
79 var fn = sinon.spy(function () {
80 fn.calledOnce.should.equal(true, "callback should have been called once")
81 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
82
83 fn.args[0].should.eql([null, 3], "no error, 3 requests");
84
85 done();
86 });
87
88 var visitor = ua({enableBatching:false});
89 visitor._queue.push.apply(visitor._queue, paramSets)
90 visitor.send(fn);
91 });
92
93 describe("#batching is true", function() {
94 it("should send request to collect path when only one payload", function(done) {
95 var paramSets = [
96 {first: Math.random()}
97 ]
98
99 var fn = sinon.spy(function () {
100 fn.args[0].should.eql([null, 1], "no error, 1 requests");
101 var args = post.args[0];
102
103 var parsedUrl = url.parse(args[0]);
104
105 parsedUrl.pathname.should.eql(config.path);
106 done();
107 });
108
109 var visitor = ua({enableBatching:true});
110 visitor._queue.push.apply(visitor._queue, paramSets)
111 visitor.send(fn);
112 });
113
114 it("should send request to batch path when more than one payload sent", function(done) {
115 var paramSets = [
116 {first: Math.random()},
117 {second: Math.random()},
118 {third: Math.random()}
119 ]
120
121 var fn = sinon.spy(function () {
122 fn.args[0].should.eql([null, 1], "no error, 1 requests");
123 var args = post.args[0];
124
125 var parsedUrl = url.parse(args[0]);
126
127 parsedUrl.pathname.should.eql(config.batchPath);
128 done();
129 });
130
131 var visitor = ua({enableBatching:true});
132 visitor._queue.push.apply(visitor._queue, paramSets)
133 visitor.send(fn);
134 });
135
136 it("should batch data in Post form", function(done) {
137 var paramSets = [
138 {first: Math.random()},
139 {second: Math.random()},
140 {third: Math.random()}
141 ]
142
143 var fn = sinon.spy(function () {
144 fn.calledOnce.should.equal(true, "callback should have been called once")
145 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
146
147 fn.args[0].should.eql([null, 1], "no error, 1 requests");
148 var args = post.args[0];
149
150 var params = paramSets;
151 var formParams = args[1].body.split("\n");
152 formParams.should.have.lengthOf(3);
153 formParams[0].should.equal(qs.stringify(params[0]));
154
155 done();
156 });
157
158 var visitor = ua({enableBatching:true});
159 visitor._queue.push.apply(visitor._queue, paramSets)
160 visitor.send(fn);
161 })
162
163 it("should batch data based on batchSize", function(done) {
164 var paramSets = [
165 {first: Math.random()},
166 {second: Math.random()},
167 {third: Math.random()}
168 ]
169
170 var fn = sinon.spy(function () {
171 fn.calledOnce.should.equal(true, "callback should have been called once")
172 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
173
174 fn.args[0].should.eql([null, 2], "no error, 2 requests");
175
176 var body = post.args[0][1].body;
177
178 body.split("\n").should.have.lengthOf(2);
179
180 done();
181 });
182
183 var visitor = ua({enableBatching:true, batchSize: 2});
184 visitor._queue.push.apply(visitor._queue, paramSets)
185 visitor.send(fn);
186 });
187 });
188
189
190
191
192
193
194 it("should add custom headers to request header", function (done) {
195 var fn = sinon.spy(function () {
196 fn.calledOnce.should.equal(true, "callback should have been called once");
197 fn.thisValues[0].should.equal(visitor, "callback should be called in the context of the visitor instance");
198
199 post.calledOnce.should.equal(true, "request should have been POSTed");
200
201 var parsedUrl = url.parse(post.args[0][0]);
202 var options = post.args[0][1];
203
204 (parsedUrl.protocol + "//" + parsedUrl.host).should.equal(config.hostname);
205
206 options.should.have.keys("headers","body")
207 options.headers.should.have.key("User-Agent");
208 options.headers["User-Agent"].should.equal("Test User Agent");
209
210 done();
211 });
212
213 var visitor = ua({
214 headers: {'User-Agent': 'Test User Agent'}
215 });
216 visitor._queue.push({});
217 visitor.send(fn);
218 });
219
220
221
222 })
223
224});
225
226
227
228
229
230
231
232
233
234