UNPKG

3.48 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
17 describe("middleware", function () {
18
19 it("should return a middleware-capable function", function () {
20 var middleware = ua.middleware()
21 middleware.length.should.equal(3, "function signature should have three arguments")
22 });
23
24 it("should try to create a visitor based on session data", function () {
25 var req = {session: {cid: uuid.v4()}}
26 var options = {debug: true}
27 var middleware = ua.middleware("tid", options)
28 var next = sinon.spy()
29
30 middleware(req, {}, next)
31
32 next.calledOnce.should.equal(true, "next() should've been called once")
33 req.visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
34 req.visitor.cid.should.equal(req.session.cid, "The client ID should've been propagated")
35 req.visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
36 req.visitor.options.should.eql(options, "The options should've been proprapated")
37 });
38
39
40 it("should try to create a visitor based on the _ga cookie", function () {
41 var req = {cookies: {_ga: "GA1.2.46218180.1366882461"}}
42 var options = {debug: false}
43 var middleware = ua.middleware("tid", options)
44 var next = sinon.spy()
45
46 middleware(req, {}, next)
47
48 next.calledOnce.should.equal(true, "next() should've been called once")
49 req.visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
50 req.visitor.cid.should.equal("46218180.1366882461", "The client ID should've been extracted from the cookie")
51 req.visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
52 req.visitor.options.should.eql(options, "The options should've been proprapated")
53 })
54
55
56 it("should allow changing the _ga cookie name", function () {
57 var req = {cookies: {foo: "GA1.2.46218180.1366882461"}}
58 var options = {cookieName: "foo"}
59 var middleware = ua.middleware("tid", options)
60 var next = sinon.spy()
61
62 middleware(req, {}, next)
63
64 req.visitor.cid.should.equal("46218180.1366882461", "The client ID should've been extracted from the cookie")
65 })
66
67
68 it("should store the cid in the session", function () {
69 var req = {cookies: {_ga: "GA1.2.46218180.1366882461"}, session: {}}
70 var options = {debug: false}
71 var middleware = ua.middleware("tid", options)
72 var next = sinon.spy()
73
74 middleware(req, {}, next)
75
76 req.session.cid.should.equal("46218180.1366882461", "The client ID should've saved to the session")
77 })
78 });
79
80 describe("createFromSession", function () {
81
82 it("should combine an existing tracking ID with a client ID from the session", function () {
83 var options = {debug: false}
84 var middleware = ua.middleware("tid", options)
85 var session = {cid: uuid.v4()}
86
87 var visitor = ua.createFromSession(session);
88
89 visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
90 visitor.cid.should.equal(session.cid, "The client ID should've been extracted from the cookie")
91 visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
92 visitor.options.should.eql(options, "The options should've been proprapated")
93 })
94
95 })
96
97
98});
99
100
101
102
103
104
105
106
107
108