0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/test/unit/server/web/parent/middleware/request-id.test.js
Hannah Wolfe 9e96b04542
Moved server unit tests into the server folder
- this is a small part of a bit of cleanup of our test files
- the goal is to make the existing tests clearer with a view to making it easier to write more tests
- this makes the test structure follow the codebase structure more closely
- eventually we will colocate the tests as we break the codebase down further
2021-10-06 12:01:09 +01:00

49 lines
1.3 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const validator = require('@tryghost/validator');
const requestId = require('../../../../../../core/server/web/parent/middleware/request-id');
describe('Request ID middleware', function () {
let res;
let req;
let next;
beforeEach(function () {
req = {
get: sinon.stub()
};
res = {
redirect: sinon.spy(),
set: sinon.spy()
};
next = sinon.spy();
});
afterEach(function () {
sinon.restore();
});
it('generates a new request ID if X-Request-ID not present', function () {
should.not.exist(req.requestId);
requestId(req, res, next);
should.exist(req.requestId);
validator.isUUID(req.requestId).should.be.true();
res.set.calledOnce.should.be.false();
});
it('keeps the request ID if X-Request-ID is present', function () {
should.not.exist(req.requestId);
req.get.withArgs('X-Request-ID').returns('abcd');
requestId(req, res, next);
should.exist(req.requestId);
req.requestId.should.eql('abcd');
res.set.calledOnce.should.be.true();
res.set.calledWith('X-Request-ID', 'abcd').should.be.true();
});
});