All files / test middlewares.spec.js

91.49% Statements 43/47
50% Branches 2/4
95% Functions 19/20
91.49% Lines 43/47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86    1x 1x 3x     1x 1x   1x   1x 1x 1x     1x 1x       1x 1x 1x 1x   1x     1x 1x 1x   1x 1x 1x   1x           1x   1x 1x 1x         1x 1x     1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x         1x       1x        
'use strict';
 
describe("Middlewares", function() {
  afterEach(function(done) {
    this.stopServer().then(done, done.fail);
  });
 
  it('should call a single middleware if defined', function(done) {
    let middlewareCalled = false;
 
    this.startServer({
      dataPath: 'test/data/middlewares',
      middlewares: [() => (req, res, next) => {
        middlewareCalled = true;
        next();
      }]
    }).
    then(() => this.checkPaths('GET', {'api': {status: 200}})).
    then(() => { expect(middlewareCalled).toBe(true); }).
    then(done, done.fail);
  });
 
  it('should call multiple middlewares if defined', function(done) {
    let middleware1Called = false;
    let middleware2Called = false;
    let middleware3Called = false;
 
    this.startServer({
      dataPath: 'test/data/middlewares',
      middlewares: [
        () => (req, res, next) => {
          middleware1Called = true;
          next();
        },
        () => (req, res) => {
          middleware2Called = true;
          res.status(201).end();
        },
        () => (req, res, next) => {
          middleware3Called = true;
          next();
        },
      ]
    }).
    then(() => this.checkPaths('GET', {'api': {status: 201}})).
    then(() => {
      expect(middleware1Called).toBe(true, 'The 1st middleware should have been called');
      expect(middleware2Called).toBe(true, 'The 2nd middleware should have been called');
      expect(middleware3Called).toBe(false, 'The 3rd middleware should NOT have been called');
    }).
    then(done, done.fail);
  });
 
  it('should pass the logger and the configuration to middlewares', function(done) {
    this.startServer({
      dataPath: 'test/data/middlewares',
      middlewares: [
        ({logger, configuration}) => (req, res, next) => {
          Eif (logger) {
            expect(typeof logger.trace).toBe('function');
            expect(typeof logger.debug).toBe('function');
            expect(typeof logger.info).toBe('function');
            expect(typeof logger.warn).toBe('function');
            expect(typeof logger.error).toBe('function');
            expect(typeof logger.fatal).toBe('function');
          } else {
            fail('Expected logger to be set');
          }
 
          Eif (configuration) {
            expect(configuration.get('middlewares')).toBeDefined();
            expect(configuration.get('middlewares')).not.toBeNull();
            expect(configuration.get('middlewares').length).toBe(1);
          } else {
            fail('Expected configuration to be set');
          }
 
          next();
        }
      ]
    }).
    then(() => this.checkPaths('GET', {'api': {}})).
    then(done, done.fail);
  });
});