Skip to content

Commit

Permalink
Merge pull request #52 from Mastercard/adding_patch_support
Browse files Browse the repository at this point in the history
adding PATCH and merge-patch+json support
  • Loading branch information
karen-avetisyan-mc authored Oct 2, 2024
2 parents 7b12d84 + 0b10eb4 commit bf50668
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down Expand Up @@ -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": {
Expand Down
15 changes: 13 additions & 2 deletions src/mastercard-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down
44 changes: 43 additions & 1 deletion test/encryption/client-encryption.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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');

Expand All @@ -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');
Expand Down
36 changes: 36 additions & 0 deletions test/test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
)
};

0 comments on commit bf50668

Please sign in to comment.