Skip to content

Commit 5e089fa

Browse files
committed
Fix unit tests
1 parent d8acacd commit 5e089fa

File tree

2 files changed

+18
-27
lines changed

2 files changed

+18
-27
lines changed

config/morgan.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ logger.token('transfer-state', (req, res) => {
8787
// especially when collaborating with other developers testing the running instance.
8888
// In production, omit the IP address to reduce the risk of leaking sensitive information and to support
8989
// compliance with GDPR and other privacy regulations.
90-
const morganFormat = process.env.NODE_ENV === 'production' ? ':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state - :parsed-user-agent' : ':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state :remote-addr :parsed-user-agent';
90+
// Also using a function so we can test it in our unit tests.
91+
const getMorganFormat = () =>
92+
process.env.NODE_ENV === 'production' ? ':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state - :parsed-user-agent' : ':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state :remote-addr :parsed-user-agent';
93+
94+
// Set the format once at initialization for the actual middleware so we don't have to evaluate on each call
95+
const morganFormat = getMorganFormat();
9196

9297
// Create a middleware to capture original content length
9398
const captureContentLength = (req, res, next) => {
@@ -124,3 +129,6 @@ exports.morganLogger = () => (req, res, next) => {
124129
})(req, res, next);
125130
});
126131
};
132+
133+
// Expose for testing
134+
exports._getMorganFormat = getMorganFormat;

test/morgan.js

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { expect } = require('chai');
33
const sinon = require('sinon');
44

55
// Import the morgan configuration to ensure tokens are registered
6-
require('../config/morgan');
6+
const { _getMorganFormat } = require('../config/morgan');
77

88
describe('Morgan Configuration Tests', () => {
99
let req;
@@ -115,38 +115,21 @@ describe('Morgan Configuration Tests', () => {
115115
});
116116

117117
describe('Complete Morgan Format', () => {
118+
// const { _getMorganFormat } = require('../config/morgan');
119+
118120
it('should combine all tokens correctly in development', () => {
119121
process.env.NODE_ENV = 'development';
120-
// Setup response with known values
121-
res.statusCode = 200;
122-
res.setHeader('Content-Length', '2048');
123-
124-
const formatter = morgan.compile(':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state :remote-addr :parsed-user-agent');
125-
122+
const formatter = morgan.compile(_getMorganFormat());
126123
const output = formatter(morgan, req, res);
127-
128-
// Test the complete output contains all expected parts
129-
expect(output).to.include('2024-01-01 12:00:00'); // date
130-
expect(output).to.include('GET'); // method
131-
expect(output).to.include('/test'); // url
132-
expect(output).to.include('\x1b[32m200\x1b[0m'); // colored status
133-
expect(output).to.include('2.00KB'); // bytes sent
134-
expect(output).to.include('127.0.0.1'); // IP address in development
135-
expect(output).to.include('Windows/Chrome v120'); // parsed user agent
124+
expect(output).to.include('127.0.0.1'); // Should include IP in development
136125
});
137126

138-
it('should redact IP address in production', () => {
127+
it('should exclude IP address in production', () => {
139128
process.env.NODE_ENV = 'production';
140-
res.statusCode = 200;
141-
res.setHeader('Content-Length', '2048');
142-
143-
const formatter = morgan.compile(':short-date :method :url :colored-status :response-time[0]ms :bytes-sent :transfer-state REDACTED :parsed-user-agent');
144-
129+
const formatter = morgan.compile(_getMorganFormat());
145130
const output = formatter(morgan, req, res);
146-
147-
expect(output).to.include('REDACTED');
148-
expect(output).to.not.include('127.0.0.1');
149-
expect(output).to.include('2.00KB'); // bytes sent
131+
expect(output).to.not.include('127.0.0.1'); // Should not include IP
132+
expect(output).to.include(' - '); // Should have hyphen instead
150133
});
151134
});
152135
});

0 commit comments

Comments
 (0)