diff --git a/package-lock.json b/package-lock.json index fb186aa..69c0f34 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "insomnia-plugin-mastercard", - "version": "2.3.1", + "version": "2.4.0", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index a15d97e..47c7c81 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,12 @@ { "name": "insomnia-plugin-mastercard", - "version": "2.3.1", + "version": "2.4.0", "description": "An Insomnia plugin for consuming Mastercard APIs", "license": "Apache-2.0", - "repository": "https://github.com/Mastercard/insomnia-plugin-mastercard-auth", + "repository": { + "type": "git", + "url": "git+https://github.com/Mastercard/insomnia-plugin-mastercard-auth.git" + }, "bugs": { "url": "https://github.com/Mastercard/insomnia-plugin-mastercard-auth" }, @@ -45,8 +48,8 @@ } }, "dependencies": { - "mastercard-client-encryption": "^1.6.0", - "mastercard-oauth1-signer": "^1.1.6", + "mastercard-client-encryption": "^1.10.3", + "mastercard-oauth1-signer": "^1.1.7", "node-forge": "^1.3.0" }, "devDependencies": { diff --git a/src/mastercard-context.js b/src/mastercard-context.js index ccfcc80..dcb049d 100644 --- a/src/mastercard-context.js +++ b/src/mastercard-context.js @@ -30,16 +30,27 @@ function MastercardContext(context) { return !(!this.config) && isMastercardDomain; }; + this.isJsonHeader = (header) => { + if(header) { + if (header.toLowerCase().includes('application/json')) + return true; + else if (header.toLowerCase().includes('application/merge-patch+json')) + return true; + } + return false; + }; + this.isJsonRequest = () => { const header = context.request.getHeader('content-type'); - return header ? header.toLowerCase().includes('application/json') : false; + return this.isJsonHeader(header); }; this.isJsonResponse = () => { const header = context.response.getHeader('content-type'); - return header ? header.toLowerCase().includes('application/json') : false; + return this.isJsonHeader(header); }; + this.requestBody = () => { return context.request.getBody(); }; diff --git a/test/encryption/client-encryption.test.js b/test/encryption/client-encryption.test.js index b371c37..bfbe038 100644 --- a/test/encryption/client-encryption.test.js +++ b/test/encryption/client-encryption.test.js @@ -27,7 +27,11 @@ describe('Encryption', () => { url: 'https://api.mastercard.com/service/api/resource', body: body }); - + + const contextPatch = helper.contextPatch({ + url: 'https://api.mastercard.com/service/api/resource', + body: body + }); const contextWithHeader = helper.context({ url: 'https://api.mastercard.com/service/api/resource', body: bodyWithHeader, @@ -39,6 +43,10 @@ describe('Encryption', () => { body: body }); + const contextJWEPatch = helper.contextJWEPatch({ + url: 'https://api.mastercard.com/service/api/resource', + body: body + }); const contextWithRootElem = helper.context({ url: 'https://api.mastercard.com/service/api/resource', body: body, @@ -107,6 +115,23 @@ describe('Encryption', () => { fs.readFileSync.restore(); }); + it('should decrypt the response with merge-patch+json content type', async () => { + mockResponse('./test/__res__/mock-response.json'); + + sinon.spy(contextPatch.response, 'setBody'); + sinon.mock(contextPatch.response).expects('getHeader').returns('application/merge-patch+json'); + sinon.mock(contextPatch.response).expects('getBodyStream').returns({ path: 'mocked-response-path' }); + + await plugin.responseHooks[0](contextPatch); // decrypt + + const body = contextPatch.response.setBody.getCall(0).args[0]; + const json = JSON.parse(body); + assert.ok(body); + assert.ok(json); + assert.strictEqual(json.foo.accountNumber, '5123456789012345'); + fs.readFileSync.restore(); + }); + it('should JWE decrypt the response', async () => { mockResponse('./test/__res__/mock-jwe-response.json'); @@ -124,6 +149,23 @@ describe('Encryption', () => { fs.readFileSync.restore(); }); + it('should JWE decrypt the response with merge-patch+json content type', async () => { + mockResponse('./test/__res__/mock-jwe-response.json'); + + sinon.spy(contextJWEPatch.response, 'setBody'); + sinon.mock(contextJWEPatch.response).expects('getHeader').returns('application/merge-patch+json'); + sinon.mock(contextJWEPatch.response).expects('getBodyStream').returns({ path: 'mocked-response-path' }); + + await plugin.responseHooks[0](contextJWEPatch); // decrypt + + const body = contextJWEPatch.response.setBody.getCall(0).args[0]; + const json = JSON.parse(body); + assert.ok(body); + assert.ok(json); + assert.strictEqual(json.foo.accountNumber, '5123456789012345'); + fs.readFileSync.restore(); + }); + it('should encrypt the request with header', async () => { sinon.spy(contextWithHeader.request, 'setBodyText'); diff --git a/test/test/helper.js b/test/test/helper.js index c842e2a..5c8719c 100644 --- a/test/test/helper.js +++ b/test/test/helper.js @@ -44,6 +44,24 @@ module.exports = { }, config ), + contextPatch: ( + { + url = 'https://api.mastercard.com/service/api', + config = require('../__res__/config.json').mastercard, + body = null, + header = 'application/merge-patch+json;charset=UTF-8' + } = {} + ) => mockContext( + 'PATCH', + url, + [{ name: 'name', value: 'value' }], + header, + () => [{ name: 'foo', value: 'bar' }], + body, + () => { + }, + config + ), contextJWE: ( { url = 'https://api.mastercard.com/service/api', @@ -61,5 +79,23 @@ module.exports = { () => { }, config + ), + contextJWEPatch: ( + { + url = 'https://api.mastercard.com/service/api', + config = require('../__res__/config-jwe.json').mastercard, + body = null, + header = 'application/merge-patch+json;charset=UTF-8' + } = {} + ) => mockContext( + 'PATCH', + url, + [{ name: 'name', value: 'value' }], + header, + () => [{ name: 'foo', value: 'bar' }], + body, + () => { + }, + config ) };