Skip to content

Commit fa54817

Browse files
author
Olivier Van den Mooter
committed
cover
1 parent d5ce307 commit fa54817

2 files changed

Lines changed: 121 additions & 9 deletions

File tree

lib/request.logger.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = (config) => {
2828
host: parsedUrl.host,
2929
path: parsedUrl.pathname,
3030
method: options?.method || 'GET',
31-
payload: options?.body || params || undefined,
31+
payload: options?.body || params,
3232
search: parsedUrl.search,
3333
};
3434
try {
@@ -40,22 +40,16 @@ module.exports = (config) => {
4040
duration,
4141
};
4242
const log = logRequest(logger, correlationId, req, res, parsedUrl.protocol );
43-
let logged = false;
4443
if (validatedConfig.logResponsePayload) {
4544
try {
4645
if(response.json) {
4746
const json = await response.clone().json();
4847
log(json);
49-
logged = true;
5048
}
5149
} catch {
52-
if(response.text) {
53-
const text = await response.clone().text();
54-
log(text);
55-
logged = true;
56-
}
50+
const text = await response.clone().text();
51+
log(text);
5752
}
58-
if(!logged) log();
5953
} else {
6054
log();
6155
}

test/request.logger.test.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,124 @@ describe('Requestlog:', () => {
2424
app.stop();
2525
done();
2626
});
27+
it('http POST with body /externalcall {} 200', () => {
28+
const logger = requestlogger({ logResponsePayload: true, logRequestPayload: true });
29+
const logspy = sandbox.spy(logger, 'log');
30+
const postData = JSON.stringify({
31+
'msg': 'Hello World!',
32+
});
33+
34+
const options = {
35+
hostname: `localhost`,
36+
port: server.address().port,
37+
path: '/externalcall',
38+
protocol: 'http:',
39+
method: 'POST',
40+
headers: {
41+
'Content-Type': 'application/json',
42+
'Content-Length': Buffer.byteLength(postData),
43+
},
44+
};
45+
46+
return new Promise((resolve, reject) => {
47+
const req = http.request(options, (res) => {
48+
res.on('data', () => {
49+
});
50+
res.on('end', () => {
51+
console.log('Response ended: ');
52+
sinon.assert.calledWith(logspy, {
53+
request: {
54+
host: sinon.match(/localhost:[0-9]+/gm),
55+
path: '/externalcall',
56+
method: 'POST',
57+
payload: '{"msg":"Hello World!"}'
58+
},
59+
response: {
60+
status: 200,
61+
duration: sinon.match.number,
62+
payload: '{"ok":"ok"}'
63+
},
64+
protocol: 'http:',
65+
type: ['application'],
66+
});
67+
resolve('ok');
68+
});
69+
}).on('error', (err) => {
70+
console.log('Error: ', err.message);
71+
reject(err);
72+
});
73+
req.write(postData);
74+
req.end();
75+
});
76+
});
77+
it('http fails', () => {
78+
const logger = requestlogger({ logResponsePayload: true, log: true });
79+
const logspy = sandbox.spy(logger, 'log');
80+
return new Promise((resolve, reject) => {
81+
http.get(`http://localhost/externalcall`, (res) => {
82+
res.on('data', () => {
83+
});
84+
res.on('end', () => {
85+
reject('should not end');
86+
});
87+
}).on('error', (err) => {
88+
console.log('Error: ', err.message);
89+
sinon.assert.calledWith(logspy, {
90+
request: {
91+
host: 'localhost',
92+
path: '/externalcall',
93+
},
94+
response: {
95+
status: sinon.match.any,
96+
duration: sinon.match.any,
97+
},
98+
protocol: 'http:',
99+
type: ['application'],
100+
});
101+
resolve();
102+
});
103+
});
104+
});
105+
it('http fails with correlationid header', () => {
106+
const logger = requestlogger({ logResponsePayload: true, log: true });
107+
const logspy = sandbox.spy(logger, 'log');
108+
const options = {
109+
hostname: `localhost`,
110+
path: '/externalcall',
111+
protocol: 'http:',
112+
method: 'POST',
113+
headers: {
114+
'Content-Type': 'application/json',
115+
'dgp-correlation': 'correlationid',
116+
},
117+
};
118+
return new Promise((resolve, reject) => {
119+
http.request(options, (res) => {
120+
res.on('data', () => {
121+
});
122+
res.on('end', () => {
123+
reject('should not end');
124+
});
125+
}).on('error', (err) => {
126+
console.log('Error: ', err.message);
127+
sinon.assert.calledWith(logspy, {
128+
correlationId: 'correlationid',
129+
request: {
130+
host: 'localhost',
131+
path: '/externalcall',
132+
method: 'POST',
133+
},
134+
response: {
135+
status: sinon.match.any,
136+
duration: sinon.match.any,
137+
},
138+
protocol: 'http:',
139+
type: ['application'],
140+
});
141+
resolve();
142+
});
143+
});
144+
});
27145
it('GET /externalcall {} 200', async () => {
28146
const logger = requestlogger();
29147
const logspy = sandbox.spy(logger, 'log');

0 commit comments

Comments
 (0)