Skip to content

Commit 2984b66

Browse files
ibrahimgunduz34Ibrahim Gunduz
andauthored
Fix '[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated' issue (#46)
* Fix '[DEP0066] DeprecationWarning: OutgoingMessage.prototype._headers is deprecated' issue * Check if _headers property exist with hasOwnProperty * Flush the headers property with empty object isteadof undefined * Avoid internal property accesses regarding headers * Fix the failed test Co-authored-by: Ibrahim Gunduz <ibrahim.gunduz@payu.ro>
1 parent 5b7203a commit 2984b66

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

lib/logger-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function shouldBeParsed(maskFields, excludeFields) {
167167
}
168168

169169
function getResponseAudit(req, res, options) {
170-
var headers = _.get(res, '_headers');
170+
var headers = res && 'function' === typeof res.getHeaders ? res.getHeaders() : _.get(res, '_headers');
171171
var elapsed = req && res ? res.timestamp - req.timestamp : 0;
172172
var timestamp = res && res.timestamp ? res.timestamp.toISOString() : NA;
173173
var timestamp_ms = res && res.timestamp ? res.timestamp.valueOf() : NA;

test/logger-helper-test.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ describe('logger-helpers tests', function () {
7878
response = httpMocks.createResponse();
7979
response._bodyStr = JSON.stringify(body);
8080
response.timestamp = endTime;
81-
response.headers = { 'header2': 'some-other-value', 'content-type': 'application/json' };
82-
response._headers = response.headers;
81+
response.setHeader('header2', 'some-other-value')
82+
response.setHeader('content-type', 'application/json')
8383

8484
options.logger.info = function () { };
8585
options.logger.warn = function () { };
@@ -879,12 +879,12 @@ describe('logger-helpers tests', function () {
879879
});
880880

881881
beforeEach(function () {
882-
response.headers[headerToExclude] = 'other-value';
882+
response.setHeader(headerToExclude, 'other-value');
883883
});
884884

885885
it('Should audit log without the specified header', function () {
886886
options.response.excludeHeaders = [headerToExclude];
887-
let prevHeaders = _.cloneDeep(response.headers);
887+
let prevHeaders = _.cloneDeep(response.getHeaders());
888888
loggerHelper.auditResponse(request, response, options);
889889
sinon.assert.calledOnce(loggerInfoStub);
890890
sinon.assert.calledWith(loggerInfoStub, {
@@ -895,11 +895,11 @@ describe('logger-helpers tests', function () {
895895
'millis-timestamp': expectedMillisTimestamp
896896
});
897897

898-
should.deepEqual(response.headers, prevHeaders, 'headers of response change');
898+
should.deepEqual(response.getHeaders(), prevHeaders, 'headers of response change');
899899
});
900900
it('Should audit log without all headers', function () {
901901
options.response.excludeHeaders = [ALL_FIELDS];
902-
let prevHeaders = _.cloneDeep(response.headers);
902+
let prevHeaders = _.cloneDeep(response.getHeaders());
903903
loggerHelper.auditResponse(request, response, options);
904904
expectedAuditResponse.headers = NA;
905905
sinon.assert.calledOnce(loggerInfoStub);
@@ -911,14 +911,14 @@ describe('logger-helpers tests', function () {
911911
'millis-timestamp': expectedMillisTimestamp
912912
});
913913

914-
should.deepEqual(response.headers, prevHeaders, 'headers of response change');
914+
should.deepEqual(response.getHeaders(), prevHeaders, 'headers of response change');
915915
});
916916
it('Should audit log without the specified headers, if there are more than one', function () {
917917
var anotherHeaderToExclude = 'another';
918918
options.response.excludeHeaders = [headerToExclude, anotherHeaderToExclude];
919-
response.headers[anotherHeaderToExclude] = 'some value';
919+
response.setHeader(anotherHeaderToExclude, 'some value');
920920

921-
let prevHeaders = _.cloneDeep(response.headers);
921+
let prevHeaders = _.cloneDeep(response.getHeaders());
922922
loggerHelper.auditResponse(request, response, options);
923923
sinon.assert.calledOnce(loggerInfoStub);
924924
sinon.assert.calledWith(loggerInfoStub, {
@@ -929,7 +929,7 @@ describe('logger-helpers tests', function () {
929929
'millis-timestamp': expectedMillisTimestamp
930930
});
931931

932-
should.deepEqual(response.headers, prevHeaders, 'headers of response change');
932+
should.deepEqual(response.getHeaders(), prevHeaders, 'headers of response change');
933933
});
934934
it('Should audit log with all headers, if exclude headers is an empty list', function () {
935935
options.response.excludeHeaders = ['other-header'];
@@ -986,7 +986,7 @@ describe('logger-helpers tests', function () {
986986
body: 'body',
987987
test1: 'test2'
988988
};
989-
response.headers['content-type'] = testContentType;
989+
response.setHeader('content-type', testContentType)
990990
response._bodyStr = _.cloneDeep(newBody);
991991
let prevBody = _.cloneDeep(response.body);
992992

@@ -1011,8 +1011,10 @@ describe('logger-helpers tests', function () {
10111011
body: 'body',
10121012
test1: 'test2'
10131013
};
1014-
response.headers = undefined;
1015-
response._headers = undefined;
1014+
1015+
Object.keys(response.getHeaders())
1016+
.map(headerName => response.removeHeader(headerName));
1017+
10161018
response._bodyStr = _.cloneDeep(newBody);
10171019
let prevBody = _.cloneDeep(response.body);
10181020

@@ -1038,15 +1040,14 @@ describe('logger-helpers tests', function () {
10381040
body: 'body',
10391041
test1: 'test2'
10401042
};
1041-
response.headers['content-type'] = undefined;
1042-
response._headers['content-type'] = undefined;
1043+
response.removeHeader('content-type') ;
10431044
response._bodyStr = _.cloneDeep(newBody);
10441045
let prevBody = _.cloneDeep(response.body);
10451046

10461047
loggerHelper.auditResponse(request, response, options);
10471048

10481049
expectedAuditResponse.body = JSON.stringify(newBody);
1049-
expectedAuditResponse.headers['content-type'] = undefined;
1050+
delete expectedAuditResponse.headers['content-type'];
10501051

10511052
sinon.assert.calledWith(loggerInfoStub, {
10521053
stage: 'end',

0 commit comments

Comments
 (0)