@@ -36,18 +36,32 @@ test('auth.hook(request, "GET /repos/octocat/hello-world")', async () => {
36
36
"user-agent" : "test" ,
37
37
} ;
38
38
39
- const matchGetUser : fetchMock . MockMatcherFunction = ( url , { headers } ) => {
40
- expect ( url ) . toEqual ( "https://api.github.com/repos/octocat/hello-world" ) ;
41
- expect ( headers ) . toStrictEqual ( expectedRequestHeaders ) ;
39
+ const matchGetUser : fetchMock . MockMatcherFunction = ( request ) => {
40
+ expect ( request . url ) . toEqual (
41
+ "https://api.github.com/repos/octocat/hello-world" ,
42
+ ) ;
43
+
44
+ const headersObject = mock . callHistory . calls ( ) [ 0 ] . options . headers ! ;
45
+ expect ( headersObject ) . toStrictEqual ( expectedRequestHeaders ) ;
46
+
42
47
return true ;
43
48
} ;
44
49
50
+ const mock = fetchMock . createInstance ( ) ;
51
+
52
+ mock . getOnce ( matchGetUser , {
53
+ status : 200 ,
54
+ body : {
55
+ id : 123 ,
56
+ } ,
57
+ } ) ;
58
+
45
59
const requestMock = request . defaults ( {
46
60
headers : {
47
61
"user-agent" : "test" ,
48
62
} ,
49
63
request : {
50
- fetch : fetchMock . sandbox ( ) . getOnce ( matchGetUser , { id : 123 } ) ,
64
+ fetch : mock . fetchHandler ,
51
65
} ,
52
66
} ) ;
53
67
@@ -63,29 +77,38 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns 404', async (
63
77
"user-agent" : "test" ,
64
78
} ;
65
79
66
- const matchGetUser : fetchMock . MockMatcherFunction = ( url , { headers } ) => {
67
- expect ( url ) . toEqual ( "https://api.github.com/repos/octocat/hello-world" ) ;
68
- expect ( headers ) . toStrictEqual ( expectedRequestHeaders ) ;
80
+ const matchGetUser : fetchMock . MockMatcherFunction = ( request ) => {
81
+ expect ( request . url ) . toEqual (
82
+ "https://api.github.com/repos/octocat/hello-world" ,
83
+ ) ;
84
+
85
+ const headersObject = mock . callHistory . calls ( ) [ 0 ] . options . headers ! ;
86
+ expect ( headersObject ) . toStrictEqual ( expectedRequestHeaders ) ;
87
+
69
88
return true ;
70
89
} ;
71
90
91
+ const mock = fetchMock . createInstance ( ) ;
92
+ mock . getOnce ( matchGetUser , {
93
+ status : 404 ,
94
+ body : {
95
+ message : "Not Found" ,
96
+ } ,
97
+ } ) ;
98
+
72
99
const requestMock = request . defaults ( {
73
100
headers : {
74
101
"user-agent" : "test" ,
75
102
} ,
76
103
request : {
77
- fetch : fetchMock . sandbox ( ) . getOnce ( matchGetUser , {
78
- status : 404 ,
79
- body : {
80
- message : "Not Found" ,
81
- } ,
82
- } ) ,
104
+ fetch : mock . fetchHandler ,
83
105
} ,
84
106
} ) ;
85
107
86
108
const { hook } = createUnauthenticatedAuth ( { reason : "test" } ) ;
87
109
88
110
try {
111
+ // Call the hook with the requestMock
89
112
await hook ( requestMock , "GET /repos/octocat/hello-world" ) ;
90
113
throw new Error ( "should not resolve" ) ;
91
114
} catch ( error : any ) {
@@ -101,27 +124,34 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
101
124
"user-agent" : "test" ,
102
125
} ;
103
126
104
- const matchGetUser : fetchMock . MockMatcherFunction = ( url , { headers } ) => {
105
- expect ( url ) . toEqual ( "https://api.github.com/repos/octocat/hello-world" ) ;
106
- expect ( headers ) . toStrictEqual ( expectedRequestHeaders ) ;
127
+ const matchGetUser : fetchMock . MockMatcherFunction = ( request ) => {
128
+ expect ( request . url ) . toEqual (
129
+ "https://api.github.com/repos/octocat/hello-world" ,
130
+ ) ;
131
+
132
+ const headersObject = mock . callHistory . calls ( ) [ 0 ] . options . headers ! ;
133
+ expect ( headersObject ) . toStrictEqual ( expectedRequestHeaders ) ;
134
+
107
135
return true ;
108
136
} ;
109
137
138
+ const mock = fetchMock . createInstance ( ) . getOnce ( matchGetUser , {
139
+ status : 403 ,
140
+ headers : {
141
+ "x-ratelimit-remaining" : 0 ,
142
+ } ,
143
+ body : {
144
+ message :
145
+ "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)" ,
146
+ } ,
147
+ } ) ;
148
+
110
149
const requestMock = request . defaults ( {
111
150
headers : {
112
151
"user-agent" : "test" ,
113
152
} ,
114
153
request : {
115
- fetch : fetchMock . sandbox ( ) . getOnce ( matchGetUser , {
116
- status : 403 ,
117
- headers : {
118
- "x-ratelimit-remaining" : 0 ,
119
- } ,
120
- body : {
121
- message :
122
- "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)" ,
123
- } ,
124
- } ) ,
154
+ fetch : mock . fetchHandler ,
125
155
} ,
126
156
} ) ;
127
157
@@ -143,24 +173,31 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
143
173
"user-agent" : "test" ,
144
174
} ;
145
175
146
- const matchGetUser : fetchMock . MockMatcherFunction = ( url , { headers } ) => {
147
- expect ( url ) . toEqual ( "https://api.github.com/repos/octocat/hello-world" ) ;
148
- expect ( headers ) . toStrictEqual ( expectedRequestHeaders ) ;
176
+ const matchGetUser : fetchMock . MockMatcherFunction = ( request ) => {
177
+ expect ( request . url ) . toEqual (
178
+ "https://api.github.com/repos/octocat/hello-world" ,
179
+ ) ;
180
+
181
+ const headersObject = mock . callHistory . calls ( ) [ 0 ] . options . headers ! ;
182
+ expect ( headersObject ) . toStrictEqual ( expectedRequestHeaders ) ;
183
+
149
184
return true ;
150
185
} ;
151
186
187
+ const mock = fetchMock . createInstance ( ) . getOnce ( matchGetUser , {
188
+ status : 403 ,
189
+ body : {
190
+ message :
191
+ "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later." ,
192
+ } ,
193
+ } ) ;
194
+
152
195
const requestMock = request . defaults ( {
153
196
headers : {
154
197
"user-agent" : "test" ,
155
198
} ,
156
199
request : {
157
- fetch : fetchMock . sandbox ( ) . getOnce ( matchGetUser , {
158
- status : 403 ,
159
- body : {
160
- message :
161
- "You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later." ,
162
- } ,
163
- } ) ,
200
+ fetch : mock . fetchHandler ,
164
201
} ,
165
202
} ) ;
166
203
@@ -177,14 +214,15 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") returns rate limit re
177
214
} ) ;
178
215
179
216
test ( 'auth.hook(request, "PATCH /repos/octocat/hello-world") with 401 response' , async ( ) => {
217
+ const mock = fetchMock
218
+ . createInstance ( )
219
+ . patchOnce ( "path:/repos/octocat/hello-world" , 401 ) ;
180
220
const requestMock = request . defaults ( {
181
221
headers : {
182
222
"user-agent" : "test" ,
183
223
} ,
184
224
request : {
185
- fetch : fetchMock
186
- . sandbox ( )
187
- . patchOnce ( "path:/repos/octocat/hello-world" , 401 ) ,
225
+ fetch : mock . fetchHandler ,
188
226
} ,
189
227
} ) ;
190
228
@@ -201,14 +239,21 @@ test('auth.hook(request, "PATCH /repos/octocat/hello-world") with 401 response',
201
239
} ) ;
202
240
203
241
test ( 'auth.hook(request, "GET /repos/octocat/hello-world") does not swallow non-request related errors' , async ( ) => {
242
+ const mock = fetchMock
243
+ . createInstance ( )
244
+ . get ( "path:/repos/octocat/hello-world" , {
245
+ status : 500 ,
246
+ body : {
247
+ message : "unrelated" ,
248
+ } ,
249
+ } ) ;
250
+
204
251
const requestMock = request . defaults ( {
205
252
headers : {
206
253
"user-agent" : "test" ,
207
254
} ,
208
255
request : {
209
- fetch ( ) {
210
- throw new Error ( "unrelated" ) ;
211
- } ,
256
+ fetch : mock . fetchHandler ,
212
257
} ,
213
258
} ) ;
214
259
@@ -223,19 +268,21 @@ test('auth.hook(request, "GET /repos/octocat/hello-world") does not swallow non-
223
268
} ) ;
224
269
225
270
test ( 'auth.hook(request, "POST /repos/octocat/hello-world/issues/123/comments") with 403 response' , async ( ) => {
271
+ const mock = fetchMock
272
+ . createInstance ( )
273
+ . postOnce ( "path:/repos/octocat/hello-world/issues/123/comments" , {
274
+ status : 403 ,
275
+ body : {
276
+ message : "You cannot comment on locked issues" ,
277
+ } ,
278
+ } ) ;
279
+
226
280
const requestMock = request . defaults ( {
227
281
headers : {
228
282
"user-agent" : "test" ,
229
283
} ,
230
284
request : {
231
- fetch : fetchMock
232
- . sandbox ( )
233
- . postOnce ( "path:/repos/octocat/hello-world/issues/123/comments" , {
234
- status : 403 ,
235
- body : {
236
- message : "You cannot comment on locked issues" ,
237
- } ,
238
- } ) ,
285
+ fetch : mock . fetchHandler ,
239
286
} ,
240
287
} ) ;
241
288
@@ -256,12 +303,14 @@ test('auth.hook(request, "POST /repos/octocat/hello-world/issues/123/comments")
256
303
} ) ;
257
304
258
305
test ( "500 response" , async ( ) => {
306
+ const mock = fetchMock . createInstance ( ) . getOnce ( "path:/" , 500 ) ;
307
+
259
308
const requestMock = request . defaults ( {
260
309
headers : {
261
310
"user-agent" : "test" ,
262
311
} ,
263
312
request : {
264
- fetch : fetchMock . sandbox ( ) . getOnce ( "path:/" , 500 ) ,
313
+ fetch : mock . fetchHandler ,
265
314
} ,
266
315
} ) ;
267
316
0 commit comments