Skip to content

Commit bf50668

Browse files
Merge pull request #52 from Mastercard/adding_patch_support
adding PATCH and merge-patch+json support
2 parents 7b12d84 + 0b10eb4 commit bf50668

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{
22
"name": "insomnia-plugin-mastercard",
3-
"version": "2.3.1",
3+
"version": "2.4.0",
44
"description": "An Insomnia plugin for consuming Mastercard APIs",
55
"license": "Apache-2.0",
6-
"repository": "https://github.com/Mastercard/insomnia-plugin-mastercard-auth",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/Mastercard/insomnia-plugin-mastercard-auth.git"
9+
},
710
"bugs": {
811
"url": "https://github.com/Mastercard/insomnia-plugin-mastercard-auth"
912
},
@@ -45,8 +48,8 @@
4548
}
4649
},
4750
"dependencies": {
48-
"mastercard-client-encryption": "^1.6.0",
49-
"mastercard-oauth1-signer": "^1.1.6",
51+
"mastercard-client-encryption": "^1.10.3",
52+
"mastercard-oauth1-signer": "^1.1.7",
5053
"node-forge": "^1.3.0"
5154
},
5255
"devDependencies": {

src/mastercard-context.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,27 @@ function MastercardContext(context) {
3030
return !(!this.config) && isMastercardDomain;
3131
};
3232

33+
this.isJsonHeader = (header) => {
34+
if(header) {
35+
if (header.toLowerCase().includes('application/json'))
36+
return true;
37+
else if (header.toLowerCase().includes('application/merge-patch+json'))
38+
return true;
39+
}
40+
return false;
41+
};
42+
3343
this.isJsonRequest = () => {
3444
const header = context.request.getHeader('content-type');
35-
return header ? header.toLowerCase().includes('application/json') : false;
45+
return this.isJsonHeader(header);
3646
};
3747

3848
this.isJsonResponse = () => {
3949
const header = context.response.getHeader('content-type');
40-
return header ? header.toLowerCase().includes('application/json') : false;
50+
return this.isJsonHeader(header);
4151
};
4252

53+
4354
this.requestBody = () => {
4455
return context.request.getBody();
4556
};

test/encryption/client-encryption.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ describe('Encryption', () => {
2727
url: 'https://api.mastercard.com/service/api/resource',
2828
body: body
2929
});
30-
30+
31+
const contextPatch = helper.contextPatch({
32+
url: 'https://api.mastercard.com/service/api/resource',
33+
body: body
34+
});
3135
const contextWithHeader = helper.context({
3236
url: 'https://api.mastercard.com/service/api/resource',
3337
body: bodyWithHeader,
@@ -39,6 +43,10 @@ describe('Encryption', () => {
3943
body: body
4044
});
4145

46+
const contextJWEPatch = helper.contextJWEPatch({
47+
url: 'https://api.mastercard.com/service/api/resource',
48+
body: body
49+
});
4250
const contextWithRootElem = helper.context({
4351
url: 'https://api.mastercard.com/service/api/resource',
4452
body: body,
@@ -107,6 +115,23 @@ describe('Encryption', () => {
107115
fs.readFileSync.restore();
108116
});
109117

118+
it('should decrypt the response with merge-patch+json content type', async () => {
119+
mockResponse('./test/__res__/mock-response.json');
120+
121+
sinon.spy(contextPatch.response, 'setBody');
122+
sinon.mock(contextPatch.response).expects('getHeader').returns('application/merge-patch+json');
123+
sinon.mock(contextPatch.response).expects('getBodyStream').returns({ path: 'mocked-response-path' });
124+
125+
await plugin.responseHooks[0](contextPatch); // decrypt
126+
127+
const body = contextPatch.response.setBody.getCall(0).args[0];
128+
const json = JSON.parse(body);
129+
assert.ok(body);
130+
assert.ok(json);
131+
assert.strictEqual(json.foo.accountNumber, '5123456789012345');
132+
fs.readFileSync.restore();
133+
});
134+
110135
it('should JWE decrypt the response', async () => {
111136
mockResponse('./test/__res__/mock-jwe-response.json');
112137

@@ -124,6 +149,23 @@ describe('Encryption', () => {
124149
fs.readFileSync.restore();
125150
});
126151

152+
it('should JWE decrypt the response with merge-patch+json content type', async () => {
153+
mockResponse('./test/__res__/mock-jwe-response.json');
154+
155+
sinon.spy(contextJWEPatch.response, 'setBody');
156+
sinon.mock(contextJWEPatch.response).expects('getHeader').returns('application/merge-patch+json');
157+
sinon.mock(contextJWEPatch.response).expects('getBodyStream').returns({ path: 'mocked-response-path' });
158+
159+
await plugin.responseHooks[0](contextJWEPatch); // decrypt
160+
161+
const body = contextJWEPatch.response.setBody.getCall(0).args[0];
162+
const json = JSON.parse(body);
163+
assert.ok(body);
164+
assert.ok(json);
165+
assert.strictEqual(json.foo.accountNumber, '5123456789012345');
166+
fs.readFileSync.restore();
167+
});
168+
127169
it('should encrypt the request with header', async () => {
128170

129171
sinon.spy(contextWithHeader.request, 'setBodyText');

test/test/helper.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ module.exports = {
4444
},
4545
config
4646
),
47+
contextPatch: (
48+
{
49+
url = 'https://api.mastercard.com/service/api',
50+
config = require('../__res__/config.json').mastercard,
51+
body = null,
52+
header = 'application/merge-patch+json;charset=UTF-8'
53+
} = {}
54+
) => mockContext(
55+
'PATCH',
56+
url,
57+
[{ name: 'name', value: 'value' }],
58+
header,
59+
() => [{ name: 'foo', value: 'bar' }],
60+
body,
61+
() => {
62+
},
63+
config
64+
),
4765
contextJWE: (
4866
{
4967
url = 'https://api.mastercard.com/service/api',
@@ -61,5 +79,23 @@ module.exports = {
6179
() => {
6280
},
6381
config
82+
),
83+
contextJWEPatch: (
84+
{
85+
url = 'https://api.mastercard.com/service/api',
86+
config = require('../__res__/config-jwe.json').mastercard,
87+
body = null,
88+
header = 'application/merge-patch+json;charset=UTF-8'
89+
} = {}
90+
) => mockContext(
91+
'PATCH',
92+
url,
93+
[{ name: 'name', value: 'value' }],
94+
header,
95+
() => [{ name: 'foo', value: 'bar' }],
96+
body,
97+
() => {
98+
},
99+
config
64100
)
65101
};

0 commit comments

Comments
 (0)