|
1 | | -const pick = require('lodash.pick'); |
| 1 | +const pick = require('pick-deep'); |
2 | 2 | const omitDeep = require('omit-deep'); |
3 | 3 | const stream = require('stream'); |
| 4 | +const express = require('express'); |
| 5 | +const supertest = require('supertest'); |
4 | 6 |
|
5 | 7 | let loggerFactory; |
6 | 8 |
|
@@ -172,3 +174,96 @@ test('Enable custom path', () => { |
172 | 174 | logger.info({ req }); |
173 | 175 | expect(loggerWrites).toBeCalled(); |
174 | 176 | }); |
| 177 | + |
| 178 | +test('Some express headers are enabled by default', () => { |
| 179 | + const loggerWrites = jest.fn(); |
| 180 | + const logger = loggerFactory({ |
| 181 | + streams: [ |
| 182 | + { |
| 183 | + stream: new stream.Writable({ |
| 184 | + write: (chunk, encoding, next) => { |
| 185 | + const json = JSON.parse(chunk); |
| 186 | + const validHeaders = ['x-deviceid', 'authorization', 'user-agent']; |
| 187 | + validHeaders.forEach(header => |
| 188 | + expect(json.req.headers[header], `${header} header should be defined`).toBeDefined() |
| 189 | + ); |
| 190 | + Object.keys(json.req.headers).forEach(header => |
| 191 | + expect(validHeaders.includes(header), `${header} header should not be defined`).toBe(true) |
| 192 | + ); |
| 193 | + loggerWrites(); |
| 194 | + next(); |
| 195 | + }, |
| 196 | + }), |
| 197 | + }, |
| 198 | + ], |
| 199 | + }); |
| 200 | + const app = express(); |
| 201 | + const request = supertest(app); |
| 202 | + app.use(logger.express); |
| 203 | + return request |
| 204 | + .get('/') |
| 205 | + .set('x-deviceid', '12345abcde') |
| 206 | + .set('Authorization', 'Basic pass') |
| 207 | + .set('User-Agent', 'Jest tests') |
| 208 | + .set('Age', 100) |
| 209 | + .then(() => { |
| 210 | + expect(loggerWrites).toBeCalled(); |
| 211 | + }); |
| 212 | +}); |
| 213 | + |
| 214 | +test('Express fields and headers can be enabled', () => { |
| 215 | + const loggerWrites = jest.fn(); |
| 216 | + const logger = loggerFactory({ |
| 217 | + enableFields: ['req.protocol', 'req.headers.host'], |
| 218 | + streams: [ |
| 219 | + { |
| 220 | + stream: new stream.Writable({ |
| 221 | + write: (chunk, encoding, next) => { |
| 222 | + const json = JSON.parse(chunk); |
| 223 | + expect(json.req.protocol).toBe('http'); |
| 224 | + expect(json.req.headers.host).toBe('localhost'); |
| 225 | + loggerWrites(); |
| 226 | + next(); |
| 227 | + }, |
| 228 | + }), |
| 229 | + }, |
| 230 | + ], |
| 231 | + }); |
| 232 | + const app = express(); |
| 233 | + const request = supertest(app); |
| 234 | + app.use(logger.express); |
| 235 | + return request |
| 236 | + .get('/') |
| 237 | + .set('host', 'localhost') |
| 238 | + .then(() => { |
| 239 | + expect(loggerWrites).toBeCalled(); |
| 240 | + }); |
| 241 | +}); |
| 242 | + |
| 243 | +test('Default express headers can be disabled', () => { |
| 244 | + const loggerWrites = jest.fn(); |
| 245 | + const logger = loggerFactory({ |
| 246 | + disableFields: ['req.headers.user-agent'], |
| 247 | + streams: [ |
| 248 | + { |
| 249 | + stream: new stream.Writable({ |
| 250 | + write: (chunk, encoding, next) => { |
| 251 | + const json = JSON.parse(chunk); |
| 252 | + expect(json.req.headers['user-agent']).toBeUndefined(); |
| 253 | + loggerWrites(); |
| 254 | + next(); |
| 255 | + }, |
| 256 | + }), |
| 257 | + }, |
| 258 | + ], |
| 259 | + }); |
| 260 | + const app = express(); |
| 261 | + const request = supertest(app); |
| 262 | + app.use(logger.express); |
| 263 | + return request |
| 264 | + .get('/') |
| 265 | + .set('x-deviceid', '1234') |
| 266 | + .then(() => { |
| 267 | + expect(loggerWrites).toBeCalled(); |
| 268 | + }); |
| 269 | +}); |
0 commit comments