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