middleware.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var request = require("request");
  2. var qs = require("querystring");
  3. var uuid = require("uuid");
  4. var should = require("should");
  5. var sinon = require("sinon");
  6. var url = require("url");
  7. var ua = require("../lib/index.js");
  8. var utils = require("../lib/utils.js")
  9. var config = require("../lib/config.js")
  10. describe("ua", function () {
  11. describe("middleware", function () {
  12. it("should return a middleware-capable function", function () {
  13. var middleware = ua.middleware()
  14. middleware.length.should.equal(3, "function signature should have three arguments")
  15. });
  16. it("should try to create a visitor based on session data", function () {
  17. var req = {session: {cid: uuid.v4()}}
  18. var options = {debug: true}
  19. var middleware = ua.middleware("tid", options)
  20. var next = sinon.spy()
  21. middleware(req, {}, next)
  22. next.calledOnce.should.equal(true, "next() should've been called once")
  23. req.visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
  24. req.visitor.cid.should.equal(req.session.cid, "The client ID should've been propagated")
  25. req.visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
  26. req.visitor.options.should.eql(options, "The options should've been proprapated")
  27. });
  28. it("should try to create a visitor based on the _ga cookie", function () {
  29. var req = {cookies: {_ga: "GA1.2.46218180.1366882461"}}
  30. var options = {debug: false}
  31. var middleware = ua.middleware("tid", options)
  32. var next = sinon.spy()
  33. middleware(req, {}, next)
  34. next.calledOnce.should.equal(true, "next() should've been called once")
  35. req.visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
  36. req.visitor.cid.should.equal("46218180.1366882461", "The client ID should've been extracted from the cookie")
  37. req.visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
  38. req.visitor.options.should.eql(options, "The options should've been proprapated")
  39. })
  40. it("should allow changing the _ga cookie name", function () {
  41. var req = {cookies: {foo: "GA1.2.46218180.1366882461"}}
  42. var options = {cookieName: "foo"}
  43. var middleware = ua.middleware("tid", options)
  44. var next = sinon.spy()
  45. middleware(req, {}, next)
  46. req.visitor.cid.should.equal("46218180.1366882461", "The client ID should've been extracted from the cookie")
  47. })
  48. it("should store the cid in the session", function () {
  49. var req = {cookies: {_ga: "GA1.2.46218180.1366882461"}, session: {}}
  50. var options = {debug: false}
  51. var middleware = ua.middleware("tid", options)
  52. var next = sinon.spy()
  53. middleware(req, {}, next)
  54. req.session.cid.should.equal("46218180.1366882461", "The client ID should've saved to the session")
  55. })
  56. });
  57. describe("createFromSession", function () {
  58. it("should combine an existing tracking ID with a client ID from the session", function () {
  59. var options = {debug: false}
  60. var middleware = ua.middleware("tid", options)
  61. var session = {cid: uuid.v4()}
  62. var visitor = ua.createFromSession(session);
  63. visitor.should.be.instanceof(ua.Visitor, "The request should have gained a UA visitor instance")
  64. visitor.cid.should.equal(session.cid, "The client ID should've been extracted from the cookie")
  65. visitor.tid.should.equal("tid", "The tracking ID should've been propagated")
  66. visitor.options.should.eql(options, "The options should've been proprapated")
  67. })
  68. })
  69. });