Skip to content

Commit

Permalink
fix: send vnd.api+json for rest API calls with body
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Nov 2, 2022
1 parent 507fc86 commit 0b2b27c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
},
"devDependencies": {
"@types/jest": "^25.1.1",
"@types/lodash": "^4.14.149",
"@types/lodash": "4.14.186",
"@types/node": "^12.12.26",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
Expand Down
8 changes: 7 additions & 1 deletion src/lib/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ const makeSnykRequest = async (
? userAgentPrefix + '/'
: userAgentPrefix;
const requestHeaders: Record<string, any> = {
'Content-Type': 'application/json',
'Content-Type':
request.useRESTApi && request.body
? 'application/vnd.api+json'
: 'application/json',
Authorization: 'token ' + snykToken,
'User-Agent': `${topParentModuleName}${userAgentPrefixChecked}tech-services/snyk-request-manager/1.0`,
};
Expand All @@ -68,6 +71,9 @@ const makeSnykRequest = async (
case 'PUT':
res = await apiClient.put(request.url, request.body);
break;
case 'PATCH':
res = await apiClient.patch(request.url, request.body);
break;
case 'DELETE':
res = await apiClient.delete(request.url);
break;
Expand Down
65 changes: 65 additions & 0 deletions test/lib/request/rest-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as fs from 'fs';
import * as nock from 'nock';
import * as _ from 'lodash';
import * as path from 'path';
import axios from 'axios';

import {
NotFoundError,
ApiError,
Expand Down Expand Up @@ -43,6 +45,15 @@ beforeEach(() => {
default:
}
})
.patch(/^(?!.*xyz).*$/)
.reply(200, (uri, requestBody) => {
switch (uri) {
case '/rest/':
return requestBody;
break;
default:
}
})
.get(/^(?!.*xyz).*$/)
.reply(200, (uri) => {
switch (uri) {
Expand All @@ -65,10 +76,12 @@ beforeEach(() => {

afterEach(() => {
process.env = OLD_ENV;
jest.restoreAllMocks();
});

describe('Test Snyk Utils make request properly', () => {
it('Test GET command on /', async () => {
const axiosSpy = jest.spyOn(axios, 'create');
const response = await makeSnykRequest(
{ verb: 'GET', url: '/', useRESTApi: true },
'token123',
Expand All @@ -79,8 +92,21 @@ describe('Test Snyk Utils make request properly', () => {
.toString(),
);
expect(response.data).toEqual(fixturesJSON);
expect(axiosSpy).toHaveBeenCalledWith({
baseURL: 'https://api.snyk.io/rest/',
headers: {
Authorization: 'token token123',
'Content-Type': 'application/json',
'User-Agent': 'tech-services/snyk-request-manager/1.0',
},
responseType: 'json',
timeout: 30000,
transitional: { clarifyTimeoutError: true },
});
});
it('Test POST command on /', async () => {
const axiosSpy = jest.spyOn(axios, 'create');

const bodyToSend = {
testbody: {},
};
Expand All @@ -94,6 +120,45 @@ describe('Test Snyk Utils make request properly', () => {
'token123',
);
expect(response.data).toEqual(bodyToSend);
expect(axiosSpy).toHaveBeenCalledWith({
baseURL: 'https://api.snyk.io/rest/',
headers: {
Authorization: 'token token123',
'Content-Type': 'application/vnd.api+json',
'User-Agent': 'tech-services/snyk-request-manager/1.0',
},
responseType: 'json',
timeout: 30000,
transitional: { clarifyTimeoutError: true },
});
});
it('Test PATCH command on /', async () => {
const axiosSpy = jest.spyOn(axios, 'create');

const bodyToSend = {
testbody: {},
};
const response = await makeSnykRequest(
{
verb: 'PATCH',
url: '/',
body: JSON.stringify(bodyToSend),
useRESTApi: true,
},
'token123',
);
expect(response.data).toEqual(bodyToSend);
expect(axiosSpy).toHaveBeenCalledWith({
baseURL: 'https://api.snyk.io/rest/',
headers: {
Authorization: 'token token123',
'Content-Type': 'application/vnd.api+json',
'User-Agent': 'tech-services/snyk-request-manager/1.0',
},
responseType: 'json',
timeout: 30000,
transitional: { clarifyTimeoutError: true },
});
});
});

Expand Down

0 comments on commit 0b2b27c

Please sign in to comment.