Skip to content

Commit

Permalink
Fix parsing response as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Branco authored and joaopaulofonseca committed Oct 25, 2018
1 parent 42a6a11 commit 972270b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/logging/request-obfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ function obfuscateResponse(request, instance) {
return;
}

request.body = JSON.parse(request.body);

const requestBody = JSON.parse(instance.body);

if (isArray(request.body)) {
const methodsById = mapKeys(requestBody, method => method.id);

request.body = map(request.body, request => obfuscateResponseBody(request, methodsById[request.id].method));

return;
} else {
request.body = obfuscateResponseBody(request.body, requestBody.method);
}

request.body = obfuscateResponseBody(request.body, requestBody.method);
request.body = JSON.stringify(request.body);
}

/**
Expand Down
21 changes: 9 additions & 12 deletions test/logging/request-obfuscator_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ describe('RequestObfuscator', () => {
});

it('should obfuscate the private key from `body` when `method` is `dumpprivkey`', () => {
const request = { body: { id: '1485369469422-0', result: 'foobiz' }, type: 'response' };
const request = { body: '{"id":"1485369469422-0","result":"foobiz"}', type: 'response' };

obfuscate(request, { body: '{"id":"1485369469422","method":"dumpprivkey","params":["foobar"]}' });

request.body.should.eql({ id: '1485369469422-0', result: '******' });
JSON.parse(request.body).should.eql({ id: '1485369469422-0', result: '******' });
});

it('should obfuscate the `body` when `headers.content-type` is `application/octet-stream`', () => {
Expand All @@ -167,24 +167,21 @@ describe('RequestObfuscator', () => {

it('should obfuscate the `request.body` of a batch request', () => {
const request = {
body: [
body: JSON.stringify([
{ id: '1485369469422-0', result: 'foobar' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: 'foo' }
],
]),
type: 'response'
};

obfuscate(request, { body: '[{"id":"1485369469422-0","method":"dumpprivkey","params":["foobar"]},{"id":"1485369469422-2","method":"getnewaddress","params":["foobiz"]},{"id":"1485369469422-1","method":"dumpprivkey","params":["foobiz"]}]' });

request.should.eql({
body: [
{ id: '1485369469422-0', result: '******' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: '******' }
],
type: 'response'
});
JSON.parse(request.body).should.eql([
{ id: '1485369469422-0', result: '******' },
{ id: '1485369469422-2', result: 'foobiz' },
{ id: '1485369469422-1', result: '******' }
]);
});
});
});

0 comments on commit 972270b

Please sign in to comment.