Skip to content

Commit 0b2b27c

Browse files
committed
fix: send vnd.api+json for rest API calls with body
1 parent 507fc86 commit 0b2b27c

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"@types/jest": "^25.1.1",
51-
"@types/lodash": "^4.14.149",
51+
"@types/lodash": "4.14.186",
5252
"@types/node": "^12.12.26",
5353
"@typescript-eslint/eslint-plugin": "^2.18.0",
5454
"@typescript-eslint/parser": "^2.18.0",

src/lib/request/request.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ const makeSnykRequest = async (
4141
? userAgentPrefix + '/'
4242
: userAgentPrefix;
4343
const requestHeaders: Record<string, any> = {
44-
'Content-Type': 'application/json',
44+
'Content-Type':
45+
request.useRESTApi && request.body
46+
? 'application/vnd.api+json'
47+
: 'application/json',
4548
Authorization: 'token ' + snykToken,
4649
'User-Agent': `${topParentModuleName}${userAgentPrefixChecked}tech-services/snyk-request-manager/1.0`,
4750
};
@@ -68,6 +71,9 @@ const makeSnykRequest = async (
6871
case 'PUT':
6972
res = await apiClient.put(request.url, request.body);
7073
break;
74+
case 'PATCH':
75+
res = await apiClient.patch(request.url, request.body);
76+
break;
7177
case 'DELETE':
7278
res = await apiClient.delete(request.url);
7379
break;

test/lib/request/rest-request.test.ts

+65
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as fs from 'fs';
33
import * as nock from 'nock';
44
import * as _ from 'lodash';
55
import * as path from 'path';
6+
import axios from 'axios';
7+
68
import {
79
NotFoundError,
810
ApiError,
@@ -43,6 +45,15 @@ beforeEach(() => {
4345
default:
4446
}
4547
})
48+
.patch(/^(?!.*xyz).*$/)
49+
.reply(200, (uri, requestBody) => {
50+
switch (uri) {
51+
case '/rest/':
52+
return requestBody;
53+
break;
54+
default:
55+
}
56+
})
4657
.get(/^(?!.*xyz).*$/)
4758
.reply(200, (uri) => {
4859
switch (uri) {
@@ -65,10 +76,12 @@ beforeEach(() => {
6576

6677
afterEach(() => {
6778
process.env = OLD_ENV;
79+
jest.restoreAllMocks();
6880
});
6981

7082
describe('Test Snyk Utils make request properly', () => {
7183
it('Test GET command on /', async () => {
84+
const axiosSpy = jest.spyOn(axios, 'create');
7285
const response = await makeSnykRequest(
7386
{ verb: 'GET', url: '/', useRESTApi: true },
7487
'token123',
@@ -79,8 +92,21 @@ describe('Test Snyk Utils make request properly', () => {
7992
.toString(),
8093
);
8194
expect(response.data).toEqual(fixturesJSON);
95+
expect(axiosSpy).toHaveBeenCalledWith({
96+
baseURL: 'https://api.snyk.io/rest/',
97+
headers: {
98+
Authorization: 'token token123',
99+
'Content-Type': 'application/json',
100+
'User-Agent': 'tech-services/snyk-request-manager/1.0',
101+
},
102+
responseType: 'json',
103+
timeout: 30000,
104+
transitional: { clarifyTimeoutError: true },
105+
});
82106
});
83107
it('Test POST command on /', async () => {
108+
const axiosSpy = jest.spyOn(axios, 'create');
109+
84110
const bodyToSend = {
85111
testbody: {},
86112
};
@@ -94,6 +120,45 @@ describe('Test Snyk Utils make request properly', () => {
94120
'token123',
95121
);
96122
expect(response.data).toEqual(bodyToSend);
123+
expect(axiosSpy).toHaveBeenCalledWith({
124+
baseURL: 'https://api.snyk.io/rest/',
125+
headers: {
126+
Authorization: 'token token123',
127+
'Content-Type': 'application/vnd.api+json',
128+
'User-Agent': 'tech-services/snyk-request-manager/1.0',
129+
},
130+
responseType: 'json',
131+
timeout: 30000,
132+
transitional: { clarifyTimeoutError: true },
133+
});
134+
});
135+
it('Test PATCH command on /', async () => {
136+
const axiosSpy = jest.spyOn(axios, 'create');
137+
138+
const bodyToSend = {
139+
testbody: {},
140+
};
141+
const response = await makeSnykRequest(
142+
{
143+
verb: 'PATCH',
144+
url: '/',
145+
body: JSON.stringify(bodyToSend),
146+
useRESTApi: true,
147+
},
148+
'token123',
149+
);
150+
expect(response.data).toEqual(bodyToSend);
151+
expect(axiosSpy).toHaveBeenCalledWith({
152+
baseURL: 'https://api.snyk.io/rest/',
153+
headers: {
154+
Authorization: 'token token123',
155+
'Content-Type': 'application/vnd.api+json',
156+
'User-Agent': 'tech-services/snyk-request-manager/1.0',
157+
},
158+
responseType: 'json',
159+
timeout: 30000,
160+
transitional: { clarifyTimeoutError: true },
161+
});
97162
});
98163
});
99164

0 commit comments

Comments
 (0)