1
1
import { Transmitter } from "../transmitter"
2
- import nock from "nock"
2
+ import nock , { Scope } from "nock"
3
3
import https from "https"
4
4
import fs from "fs"
5
5
import http , { ClientRequest } from "http"
@@ -33,8 +33,8 @@ describe("Transmitter", () => {
33
33
return new Transmitter ( "https://example.com/foo" , "request body" )
34
34
}
35
35
36
- function mockSampleRequest ( responseBody : object | string , query = { } ) {
37
- nock ( "https://example.com" )
36
+ function mockSampleRequest ( responseBody : object | string , query = { } ) : Scope {
37
+ return nock ( "https://example.com" )
38
38
. post ( "/foo" , "request body" )
39
39
. query ( {
40
40
api_key : "" ,
@@ -59,17 +59,21 @@ describe("Transmitter", () => {
59
59
it ( "resolves to a response on success" , async ( ) => {
60
60
const transmitter = sampleTransmitter ( )
61
61
62
- mockSampleRequest ( { json : "response" } )
62
+ const scope = mockSampleRequest ( { json : "response" } )
63
63
64
64
await expectResponse ( transmitter . transmit ( ) , { json : "response" } )
65
+
66
+ scope . done ( )
65
67
} )
66
68
67
69
it ( "resolves to an empty object if the response is not JSON" , async ( ) => {
68
70
const transmitter = sampleTransmitter ( )
69
71
70
- mockSampleRequest ( "not JSON" )
72
+ const scope = mockSampleRequest ( "not JSON" )
71
73
72
74
await expectResponse ( transmitter . transmit ( ) , { } )
75
+
76
+ scope . done ( )
73
77
} )
74
78
75
79
it ( "rejects on error" , async ( ) => {
@@ -91,7 +95,7 @@ describe("Transmitter", () => {
91
95
92
96
const transmitter = sampleTransmitter ( )
93
97
94
- mockSampleRequest (
98
+ const scope = mockSampleRequest (
95
99
{ } ,
96
100
{
97
101
api_key : "some_api_key" ,
@@ -102,14 +106,16 @@ describe("Transmitter", () => {
102
106
)
103
107
104
108
await expectResponse ( transmitter . transmit ( ) , { } )
109
+
110
+ scope . done ( )
105
111
} )
106
112
} )
107
113
108
114
describe ( ".downloadStream" , ( ) => {
109
115
const transmitter = new Transmitter ( "http://example.com/foo" )
110
116
111
117
it ( "resolves to a response stream on success" , async ( ) => {
112
- nock ( "http://example.com" ) . get ( "/foo" ) . reply ( 200 , "response body" )
118
+ const scope = nock ( "http://example.com" ) . get ( "/foo" ) . reply ( 200 , "response body" )
113
119
114
120
const stream = await transmitter . downloadStream ( )
115
121
@@ -118,15 +124,19 @@ describe("Transmitter", () => {
118
124
stream . on ( "data" , resolve )
119
125
} )
120
126
) . resolves . toEqual ( Buffer . from ( "response body" ) )
127
+
128
+ scope . done ( )
121
129
} )
122
130
123
131
it ( "rejects if the status code is not successful" , async ( ) => {
124
- nock ( "http://example.com" ) . get ( "/foo" ) . reply ( 404 , "not found" )
132
+ const scope = nock ( "http://example.com" ) . get ( "/foo" ) . reply ( 404 , "not found" )
125
133
126
134
await expect ( transmitter . downloadStream ( ) ) . rejects . toMatchObject ( {
127
135
kind : "statusCode" ,
128
136
statusCode : 404
129
137
} )
138
+
139
+ scope . done ( )
130
140
} )
131
141
132
142
it ( "rejects if there's a request error" , async ( ) => {
@@ -194,7 +204,7 @@ describe("Transmitter", () => {
194
204
}
195
205
196
206
it ( "performs an HTTP GET request" , async ( ) => {
197
- nock ( "http://example.invalid" ) . get ( "/foo" ) . reply ( 200 , "response body" )
207
+ const scope = nock ( "http://example.invalid" ) . get ( "/foo" ) . reply ( 200 , "response body" )
198
208
199
209
const { callback, onData, onError } = await transmitterRequest (
200
210
"GET" ,
@@ -207,10 +217,12 @@ describe("Transmitter", () => {
207
217
208
218
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
209
219
expect ( onError ) . not . toHaveBeenCalled ( )
220
+
221
+ scope . done ( )
210
222
} )
211
223
212
224
it ( "performs an HTTP POST request" , async ( ) => {
213
- nock ( "http://example.invalid" )
225
+ const scope = nock ( "http://example.invalid" )
214
226
. post ( "/foo" , "request body" )
215
227
. reply ( 200 , "response body" )
216
228
@@ -226,10 +238,12 @@ describe("Transmitter", () => {
226
238
227
239
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
228
240
expect ( onError ) . not . toHaveBeenCalled ( )
241
+
242
+ scope . done ( )
229
243
} )
230
244
231
245
it ( "performs an HTTP request with query parameters" , async ( ) => {
232
- nock ( "http://example.invalid" )
246
+ const scope = nock ( "http://example.invalid" )
233
247
. get ( "/foo" )
234
248
. query ( { bar : "baz" } )
235
249
. reply ( 200 , "response body" )
@@ -247,6 +261,8 @@ describe("Transmitter", () => {
247
261
248
262
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
249
263
expect ( onError ) . not . toHaveBeenCalled ( )
264
+
265
+ scope . done ( )
250
266
} )
251
267
252
268
it ( "listens to errors on the request" , async ( ) => {
@@ -264,7 +280,7 @@ describe("Transmitter", () => {
264
280
} )
265
281
266
282
it ( "follows redirects" , async ( ) => {
267
- nock ( "http://example.invalid" )
283
+ const scope = nock ( "http://example.invalid" )
268
284
. get ( "/301" )
269
285
. reply ( 301 , undefined , {
270
286
Location : "http://example.invalid/302" ,
@@ -301,10 +317,12 @@ describe("Transmitter", () => {
301
317
302
318
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
303
319
expect ( onError ) . not . toHaveBeenCalled ( )
320
+
321
+ scope . done ( )
304
322
} )
305
323
306
324
it ( "redirects to GET method for status code 301/302/303" , async ( ) => {
307
- nock ( "http://example.invalid" )
325
+ const scope = nock ( "http://example.invalid" )
308
326
. post ( "/301" , "request body" )
309
327
. reply ( 301 , undefined , {
310
328
Location : "http://example.invalid/foo"
@@ -335,10 +353,12 @@ describe("Transmitter", () => {
335
353
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
336
354
expect ( onError ) . not . toHaveBeenCalled ( )
337
355
}
356
+
357
+ scope . done ( )
338
358
} )
339
359
340
360
it ( "redirects to the same method for status code 307/308" , async ( ) => {
341
- nock ( "http://example.invalid" )
361
+ const scope = nock ( "http://example.invalid" )
342
362
. post ( "/307" , "request body" )
343
363
. reply ( 307 , undefined , {
344
364
Location : "http://example.invalid/foo"
@@ -365,10 +385,12 @@ describe("Transmitter", () => {
365
385
expect ( onData ) . toHaveBeenCalledWith ( Buffer . from ( "response body" ) )
366
386
expect ( onError ) . not . toHaveBeenCalled ( )
367
387
}
388
+
389
+ scope . done ( )
368
390
} )
369
391
370
392
it ( "throws an error on a redirect loop" , async ( ) => {
371
- nock ( "http://example.invalid" )
393
+ const scope = nock ( "http://example.invalid" )
372
394
. persist ( )
373
395
. get ( "/foo" )
374
396
. reply ( 302 , undefined , {
@@ -392,6 +414,8 @@ describe("Transmitter", () => {
392
414
message : "Maximum number of redirects reached"
393
415
} )
394
416
)
417
+
418
+ scope . done ( )
395
419
} )
396
420
397
421
it ( "uses the CA file from the config" , async ( ) => {
0 commit comments