|
| 1 | +import * as express from 'express'; |
| 2 | +import 'jest-extended'; |
| 3 | +import { Writable } from 'stream'; |
| 4 | +import * as supertest from 'supertest'; |
| 5 | + |
| 6 | +let loggerFactory; |
| 7 | + |
| 8 | +beforeEach(() => { |
| 9 | + jest.resetModules(); |
| 10 | + loggerFactory = require('..').default; |
| 11 | +}); |
| 12 | + |
| 13 | +const testWriteStream = (resolve, assert) => ({ |
| 14 | + stream: new Writable({ |
| 15 | + write: (chunk, encoding, next) => { |
| 16 | + const json = JSON.parse(chunk); |
| 17 | + assert(json); |
| 18 | + next(); |
| 19 | + resolve(); |
| 20 | + }, |
| 21 | + }), |
| 22 | +}); |
| 23 | + |
| 24 | +test('express binds', () => { |
| 25 | + const logger = loggerFactory(); |
| 26 | + const app = express(); |
| 27 | + const request = supertest(app); |
| 28 | + app.use(logger.express); |
| 29 | + return request.get('/'); |
| 30 | +}); |
| 31 | + |
| 32 | +test('GET requests are logged by default', () => |
| 33 | + new Promise((resolve, reject) => { |
| 34 | + const logger = loggerFactory({ |
| 35 | + streams: [testWriteStream(resolve, json => expect(json.req.method).toBe('GET'))], |
| 36 | + }); |
| 37 | + const app = express(); |
| 38 | + const request = supertest(app); |
| 39 | + app.use(logger.express); |
| 40 | + request.get('/').then(() => null); |
| 41 | + })); |
| 42 | + |
| 43 | +test('OPTIONS requests are ignored by default', () => { |
| 44 | + const loggerWrites = jest.fn(); |
| 45 | + const logger = loggerFactory({ |
| 46 | + streams: [ |
| 47 | + { |
| 48 | + stream: new Writable({ |
| 49 | + write: (chunk, encoding, next) => { |
| 50 | + loggerWrites(); |
| 51 | + next(); |
| 52 | + }, |
| 53 | + }), |
| 54 | + }, |
| 55 | + ], |
| 56 | + }); |
| 57 | + const app = express(); |
| 58 | + const request = supertest(app); |
| 59 | + app.use(logger.express); |
| 60 | + return request.options('/').then(() => { |
| 61 | + expect(loggerWrites).not.toBeCalled(); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'].forEach(method => { |
| 66 | + test(`${method} HTTP method can be ignored by options`, () => { |
| 67 | + const loggerWrites = jest.fn(); |
| 68 | + const logger = loggerFactory({ |
| 69 | + ignoredHttpMethods: [method], |
| 70 | + streams: [ |
| 71 | + { |
| 72 | + stream: new Writable({ |
| 73 | + write: (chunk, encoding, next) => { |
| 74 | + loggerWrites(); |
| 75 | + next(); |
| 76 | + }, |
| 77 | + }), |
| 78 | + }, |
| 79 | + ], |
| 80 | + }); |
| 81 | + const app = express(); |
| 82 | + const request = supertest(app); |
| 83 | + app.use(logger.express); |
| 84 | + return request[method.toLowerCase()]('/').then(() => { |
| 85 | + expect(loggerWrites).not.toBeCalled(); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +test('route can be ignored by logger options', () => { |
| 91 | + const loggerWrites = jest.fn(); |
| 92 | + const logger = loggerFactory({ |
| 93 | + streams: [ |
| 94 | + { |
| 95 | + stream: new Writable({ |
| 96 | + write: (chunk, encoding, next) => { |
| 97 | + loggerWrites(); |
| 98 | + next(); |
| 99 | + }, |
| 100 | + }), |
| 101 | + }, |
| 102 | + ], |
| 103 | + skip: (req: express.Request) => req.url === '/not-logged', |
| 104 | + }); |
| 105 | + const app = express(); |
| 106 | + const request = supertest(app); |
| 107 | + app.use(logger.express); |
| 108 | + return request.get('/not-logged').then(() => { |
| 109 | + expect(loggerWrites).not.toBeCalled(); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +test('route can be ignored using regexp helper', () => { |
| 114 | + const { matchPath } = require('../utils'); |
| 115 | + const loggerWrites = jest.fn(); |
| 116 | + const logger = loggerFactory({ |
| 117 | + streams: [ |
| 118 | + { |
| 119 | + stream: new Writable({ |
| 120 | + write: (chunk, encoding, next) => { |
| 121 | + loggerWrites(); |
| 122 | + next(); |
| 123 | + }, |
| 124 | + }), |
| 125 | + }, |
| 126 | + ], |
| 127 | + skip: matchPath(/heal.h/), |
| 128 | + }); |
| 129 | + const app = express(); |
| 130 | + const request = supertest(app); |
| 131 | + app.use(logger.express); |
| 132 | + return request.get('/healthcheck').then(() => { |
| 133 | + expect(loggerWrites).not.toBeCalled(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments