Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for x-request-id handling
refs 0107ac8 - added unit tests for middleware added yesterday
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const should = require('should'); | ||
const sinon = require('sinon'); | ||
const validator = require('validator'); | ||
|
||
const requestId = require('../../../../server/web/shared/middlewares/request-id'); | ||
|
||
describe('Request ID middleware', function () { | ||
var res, req, 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(); | ||
}); | ||
}); |