Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/handlersjs-http/lib/handlers/http.handler.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class MockHttpHandler implements HttpHandler {
this.logger.info('mock handling');

const response: HttpHandlerResponse = {
// body: 'a'.repeat(2000) + ' - not in domestic response log',
body: Buffer.from(`some mock output (${context.request.url.toString()})`),
// headers: { 'content-length': '200' },
headers: {},
status: 200,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe('NodeHttpRequestResponseHandler', () => {
it('should calculate the content-length of the response body as utf-8 when no charset is present', async () => {

const body = 'This is a response body with a certain length.';
nestedHttpHandler.handle = jest.fn().mockReturnValueOnce(of({ body, headers: { 'content-length': '2' }, status:200 }));
nestedHttpHandler.handle = jest.fn().mockReturnValueOnce(of({ body, headers: { }, status: 200 }));

await lastValueFrom(handler.handle(streamMock));

Expand All @@ -298,7 +298,7 @@ describe('NodeHttpRequestResponseHandler', () => {
it('should calculate the content-length of the response body with the given charset', async () => {

const body = Buffer.from('This is a response body with a certain length.').toString('base64');
nestedHttpHandler.handle = jest.fn().mockReturnValueOnce(of({ body, headers: { 'content-length': '2', 'content-type': 'text/html; charset=base64' }, status:200 }));
nestedHttpHandler.handle = jest.fn().mockReturnValueOnce(of({ body, headers: { 'content-type': 'text/html; charset=base64' }, status:200 }));

await lastValueFrom(handler.handle(streamMock));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
// case 'application/':
// return JSON.parse(`{"${decodeURIComponent(body).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"')}"}`);

this.logger.debug('Parsing request body', { body, contentType });
this.logger.debug('Parsing request body', { body: body.slice(0, 2000), contentType });

if (contentType?.startsWith('application/json')) {

Expand All @@ -79,7 +79,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {
) {

// don't log the body if it is a buffer. It results in a long, illegible log.
this.logger.debug('Parsing response body', { body: body instanceof Buffer ? '<Buffer>' : body, contentType });
this.logger.debug('Parsing response body', { body: body instanceof Buffer ? '<Buffer>' : typeof body === 'string' ? body.slice(0, 2000) : body, contentType });

if (contentType?.startsWith('application/json')) {

Expand Down Expand Up @@ -274,7 +274,7 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {

const extraHeaders = {
... (body !== undefined && body !== null && !response.headers['content-type'] && !response.headers['Content-Type'] && typeof response.body !== 'string' && !(response.body instanceof Buffer)) && { 'content-type': 'application/json' },
... (body !== undefined && body !== null) && { 'content-length': Buffer.byteLength(body, charsetString).toString() },
... (body !== undefined && body !== null && !response.headers['content-length'] && !response.headers['Content-Length']) && { 'content-length': Buffer.byteLength(body, charsetString).toString() },
... (this.hsts?.maxAge) && { 'strict-transport-security': `max-age=${this.hsts.maxAge}${this.hsts.includeSubDomains ? '; includeSubDomains' : ''}` },
'x-powered-by': this.poweredBy,
'x-request-id': this.requestId,
Expand Down Expand Up @@ -312,12 +312,24 @@ export class NodeHttpRequestResponseHandler implements NodeHttpStreamsHandler {

nodeHttpStreams.responseStream.end();

let bodyToLog = '';

// Set body to string '<Buffer>' if it is a Buffer Object to not pollute logs
if (response.body && response.body instanceof Buffer) {

bodyToLog = '<Buffer>';

} else if (response.body && typeof response.body === 'string') {

bodyToLog = response.body.slice(0, 2000);

}

this.logger.info('Domestic response:', {
eventType: 'domestic_response',
response: {
... response,
// Set body to string '<Buffer>' if it is a Buffer Object to not pollute logs
... (response.body && response.body instanceof Buffer) && { body: '<Buffer>' },
... (bodyToLog) && { body: bodyToLog },
},
});

Expand Down