UNPKG

2.39 kBJavaScriptView Raw
1const chai = require('chai');
2const sinon = require('sinon');
3
4chai.should();
5
6const expresStatusMonitor = require('../src/middleware-wrapper');
7const defaultConfig = require('../src/helpers/default-config');
8
9describe('middleware-wrapper', () => {
10 describe('when initialised', () => {
11 const middleware = expresStatusMonitor();
12
13 it('then it should be an instance of a Function', () => {
14 middleware.should.be.an.instanceof(Function);
15 });
16
17 const req = { socket: {} };
18 const res = { send: sinon.stub() };
19 const next = sinon.stub();
20
21 describe('when invoked', () => {
22 beforeEach(() => {
23 req.path = defaultConfig.path;
24 res.send.reset();
25 });
26
27 it(`and req.path === ${defaultConfig.path}, then res.send called`, (done) => {
28 middleware(req, res, next);
29 setTimeout(() => {
30 sinon.assert.called(res.send);
31 done();
32 });
33 });
34
35 it(`and req.path !== ${defaultConfig.path}, then res.send not called`, (done) => {
36 req.path = '/another-path';
37 middleware(req, res, next);
38 setTimeout(() => {
39 sinon.assert.notCalled(res.send);
40 done();
41 });
42 });
43
44 it('and res.removeHeader is present, then header is removed', (done) => {
45 const middlewareWithConfig = expresStatusMonitor({
46 iframe: true,
47 });
48 const resWithHeaders = Object.assign({}, res);
49 resWithHeaders.headers = {
50 'X-Frame-Options': 1,
51 };
52 resWithHeaders.removeHeader = sinon.stub();
53
54 middlewareWithConfig(req, resWithHeaders, next);
55 setTimeout(() => {
56 sinon.assert.called(resWithHeaders.removeHeader);
57
58 resWithHeaders.removeHeader = undefined;
59 resWithHeaders.remove = sinon.stub();
60 middlewareWithConfig(req, resWithHeaders, next);
61 setTimeout(() => {
62 sinon.assert.called(resWithHeaders.remove);
63 done();
64 });
65 });
66 });
67
68 describe('and used as separate middleware and page handler', () => {
69 it('exposes a page handler', (done) => {
70 middleware.pageRoute.should.be.an.instanceof(Function);
71 middleware.pageRoute(req, res, next);
72 setTimeout(() => {
73 sinon.assert.called(res.send);
74 done();
75 });
76 });
77 });
78 });
79 });
80});