UNPKG

quant-beat

Version:

**`Quant Beat`** is a simple logger that wraps around [lugg](https://github.com/aexmachina/lugg "lugg") which is in itself, a simple wrapper around [bunyan](https://github.com/trentm/node-bunyan "bunyan") (a powerful logging framework). **`Quant Beat`**

119 lines (105 loc) 3.28 kB
/** * Created by dman on 18/06/2017. */ const Code = require('code') const expect = Code.expect const middleware = require('./../lib/logger-express') const sinon = require('sinon') const EventEmitter = require('events').EventEmitter const httpMocks = require('node-mocks-http') const controller = require('./controller') const Logger = require('./../lib/logger') const Errors = require('quantal-errors') const buildResponse = () => httpMocks.createResponse({eventEmitter: require('events').EventEmitter}) describe('Logger Middeware Tests', () => { it('hello', function (done) { const response = buildResponse() const request = httpMocks.createRequest({ method: 'GET', url: '/hello', protocol: 'http', connection: { remoteAddress: '127.0.0.1' } }) response.on('finish', function (res) { expect(response.header('X-TraceId')).to.be.a.string() done() }) controller.handle(request, response) }) it('UPPER CASE', function (done) { const response = buildResponse() const request = httpMocks.createRequest({ method: 'GET', url: '/upper/hello', protocol: 'http', connection: { remoteAddress: '127.0.0.1' } }) response.on('finish', function (res) { expect(response.header('X-TraceId')).to.be.a.string() expect(response.header('X-Event')).to.be.a.string() expect(response.header('X-B3-TraceId')).to.be.a.string() done() }) controller.handle(request, response) }) it('should throw NullReferenceError given null in middleware', () => { expect(middleware(new Logger())).to.be.a.function() }) it('/event/new_event', function (done) { const response = buildResponse() const event = 'TEST_EVENT' const request = httpMocks.createRequest({ method: 'GET', url: '/event/new_event', headers: { 'X-Event': event }, protocol: 'http', connection: { remoteAddress: '127.0.0.1' } }) response.on('finish', function (res) { expect(response.header('X-TraceId')).to.be.a.string() expect(response.header('X-Event')).to.be.a.string().and.to.be.equal('NEW_EVENT') done() }) controller.handle(request, response) }) it('/event/original', function (done) { const response = buildResponse() const event = 'TEST_EVENT' const request = httpMocks.createRequest({ method: 'GET', url: '/event/original', headers: { 'X-Event': event }, protocol: 'http', connection: { remoteAddress: '127.0.0.1' } }) response.on('finish', function (res) { expect(response.header('X-TraceId')).to.be.a.string() expect(response.header('X-Event')).to.be.a.string().and.to.be.equal(event) done() }) controller.handle(request, response) }) it('should return middleware function', () => { const thrown = () => middleware(null) expect(thrown).to.throw(Errors.NullReferenceError, 'Logger instance cannot be null') }) it('should invoke the callback', function () { const spy = sinon.spy() const emitter = new EventEmitter() emitter.on('finish', spy) emitter.emit('finish') sinon.assert.calledOnce(spy) }) })