@@ -3,6 +3,8 @@ import * as fs from 'fs';
3
3
import * as nock from 'nock' ;
4
4
import * as _ from 'lodash' ;
5
5
import * as path from 'path' ;
6
+ import axios from 'axios' ;
7
+
6
8
import {
7
9
NotFoundError ,
8
10
ApiError ,
@@ -43,6 +45,15 @@ beforeEach(() => {
43
45
default :
44
46
}
45
47
} )
48
+ . patch ( / ^ (? ! .* x y z ) .* $ / )
49
+ . reply ( 200 , ( uri , requestBody ) => {
50
+ switch ( uri ) {
51
+ case '/rest/' :
52
+ return requestBody ;
53
+ break ;
54
+ default :
55
+ }
56
+ } )
46
57
. get ( / ^ (? ! .* x y z ) .* $ / )
47
58
. reply ( 200 , ( uri ) => {
48
59
switch ( uri ) {
@@ -65,10 +76,12 @@ beforeEach(() => {
65
76
66
77
afterEach ( ( ) => {
67
78
process . env = OLD_ENV ;
79
+ jest . restoreAllMocks ( ) ;
68
80
} ) ;
69
81
70
82
describe ( 'Test Snyk Utils make request properly' , ( ) => {
71
83
it ( 'Test GET command on /' , async ( ) => {
84
+ const axiosSpy = jest . spyOn ( axios , 'create' ) ;
72
85
const response = await makeSnykRequest (
73
86
{ verb : 'GET' , url : '/' , useRESTApi : true } ,
74
87
'token123' ,
@@ -79,8 +92,21 @@ describe('Test Snyk Utils make request properly', () => {
79
92
. toString ( ) ,
80
93
) ;
81
94
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
+ } ) ;
82
106
} ) ;
83
107
it ( 'Test POST command on /' , async ( ) => {
108
+ const axiosSpy = jest . spyOn ( axios , 'create' ) ;
109
+
84
110
const bodyToSend = {
85
111
testbody : { } ,
86
112
} ;
@@ -94,6 +120,45 @@ describe('Test Snyk Utils make request properly', () => {
94
120
'token123' ,
95
121
) ;
96
122
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
+ } ) ;
97
162
} ) ;
98
163
} ) ;
99
164
0 commit comments