Skip to content

Commit

Permalink
Merge pull request #71 from snyk-tech-services/develop
Browse files Browse the repository at this point in the history
RELEASE
  • Loading branch information
lili2311 authored Nov 2, 2022
2 parents 618aea3 + 0171e5f commit 062fcdc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
***

[![Known Vulnerabilities](https://snyk.io/test/github/snyk-tech-services/snyk-request-manager/badge.svg)](https://snyk.io/test/github/snyk-tech-services/snyk-request-manager)
[![Actively Maintained](https://img.shields.io/badge/Maintenance%20Level-Actively%20Maintained-green.svg)](https://gist.github.com/cheerfulstoic/d107229326a01ff0f333a1d3476e068d)

Snyk helps you find, fix and monitor for known vulnerabilities in your dependencies, both on an ad hoc basis and as part of your CI (Build) system.

Expand Down
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 062fcdc

Please sign in to comment.