@@ -3,7 +3,7 @@ const { expect } = require('chai');
33const 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
88describe ( '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