|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { assert } = require('chai') |
| 4 | +const dc = require('dc-polyfill') |
| 5 | +const axios = require('axios') |
| 6 | +const agent = require('../../dd-trace/test/plugins/agent') |
| 7 | +const { storage } = require('../../datadog-core') |
| 8 | + |
| 9 | +withVersions('express-session', 'express-session', version => { |
| 10 | + describe('express-session instrumentation', () => { |
| 11 | + const sessionMiddlewareCh = dc.channel('datadog:express-session:middleware:finish') |
| 12 | + let port, server, subscriberStub, routeHandlerStub |
| 13 | + |
| 14 | + before(() => { |
| 15 | + return agent.load(['http'], { client: false }) |
| 16 | + }) |
| 17 | + |
| 18 | + before((done) => { |
| 19 | + const express = require('../../../versions/express').get() |
| 20 | + const expressSession = require(`../../../versions/express-session@${version}`).get() |
| 21 | + |
| 22 | + const app = express() |
| 23 | + |
| 24 | + app.use(expressSession({ |
| 25 | + secret: 'secret', |
| 26 | + resave: false, |
| 27 | + rolling: true, |
| 28 | + saveUninitialized: true, |
| 29 | + genid: () => 'sid_123' |
| 30 | + })) |
| 31 | + |
| 32 | + app.get('/', (req, res) => { |
| 33 | + routeHandlerStub() |
| 34 | + |
| 35 | + res.send('OK') |
| 36 | + }) |
| 37 | + |
| 38 | + server = app.listen(0, () => { |
| 39 | + port = server.address().port |
| 40 | + done() |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + beforeEach(() => { |
| 45 | + routeHandlerStub = sinon.stub() |
| 46 | + subscriberStub = sinon.stub() |
| 47 | + |
| 48 | + sessionMiddlewareCh.subscribe(subscriberStub) |
| 49 | + }) |
| 50 | + |
| 51 | + afterEach(() => { |
| 52 | + sessionMiddlewareCh.unsubscribe(subscriberStub) |
| 53 | + }) |
| 54 | + |
| 55 | + after(() => { |
| 56 | + server.close() |
| 57 | + return agent.close({ ritmReset: false }) |
| 58 | + }) |
| 59 | + |
| 60 | + it('should not do anything when there are no subscribers', async () => { |
| 61 | + sessionMiddlewareCh.unsubscribe(subscriberStub) |
| 62 | + |
| 63 | + const res = await axios.get(`http://localhost:${port}/`) |
| 64 | + |
| 65 | + assert.equal(res.data, 'OK') |
| 66 | + sinon.assert.notCalled(subscriberStub) |
| 67 | + sinon.assert.calledOnce(routeHandlerStub) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should call the subscriber when the middleware is called', async () => { |
| 71 | + subscriberStub.callsFake(({ sessionId }) => { |
| 72 | + assert.equal(sessionId, 'sid_123') |
| 73 | + }) |
| 74 | + |
| 75 | + const res = await axios.get(`http://localhost:${port}/`) |
| 76 | + |
| 77 | + assert.equal(res.data, 'OK') |
| 78 | + sinon.assert.calledOnce(subscriberStub) |
| 79 | + sinon.assert.calledOnce(routeHandlerStub) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should not call next when the subscriber calls abort()', async () => { |
| 83 | + subscriberStub.callsFake(({ res, abortController }) => { |
| 84 | + res.end('BLOCKED') |
| 85 | + abortController.abort() |
| 86 | + }) |
| 87 | + |
| 88 | + const res = await axios.get(`http://localhost:${port}/`) |
| 89 | + |
| 90 | + assert.equal(res.data, 'BLOCKED') |
| 91 | + sinon.assert.calledOnce(subscriberStub) |
| 92 | + sinon.assert.notCalled(routeHandlerStub) |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments