UNPKG

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