1
1
import superagent from 'superagent' ;
2
+ import { request } from 'chai-http' ;
3
+ import polka from 'polka' ;
4
+
5
+ const SERVER_URL = 'http://localhost:8008' ;
6
+
2
7
describe ( 'request' , function ( ) {
3
- const isNode = typeof process === 'object' ;
4
- const isBrowser = typeof window === 'object' ;
8
+ let server ;
9
+ let aborter ;
10
+
11
+ beforeEach ( ( ) => {
12
+ aborter = new AbortController ( ) ;
13
+ server = polka ( ) ;
14
+ server . listen ( {
15
+ port : 8008 ,
16
+ signal : aborter . signal
17
+ } ) ;
18
+ } ) ;
19
+
20
+ afterEach ( ( ) => {
21
+ aborter . abort ( ) ;
22
+ } ) ;
5
23
6
24
describe ( 'Browser and Node.js' , function ( ) {
7
25
it ( 'is present on chai' , function ( ) {
@@ -10,36 +28,47 @@ describe('request', function () {
10
28
} ) ;
11
29
12
30
it ( 'request method returns instanceof superagent' , function ( ) {
13
- const req = request . execute ( '' ) . get ( '/' ) ;
31
+ server . get ( '/' , ( _req , res ) => {
32
+ res . statusCode = 200 ;
33
+ } ) ;
34
+ const req = request . execute ( SERVER_URL ) . get ( '/' ) ;
14
35
req . should . be . instanceof ( request . Request . super_ ) ;
15
- if ( isNode ) {
16
- req . should . be . instanceof ( superagent . Request ) ;
17
- }
36
+ req . should . be . instanceof ( superagent . Request ) ;
18
37
} ) ;
19
38
20
39
it ( 'can request a web page' , function ( done ) {
40
+ server . get ( '/foo' , ( _req , res ) => {
41
+ res . statusCode = 200 ;
42
+ res . setHeader ( 'content-type' , 'text/html' ) ;
43
+ res . end ( '<h1>bleep bloop</h1>' ) ;
44
+ } ) ;
45
+
21
46
request
22
- . execute ( 'https://chaijs.com' )
23
- . get ( '/guide/ ' )
47
+ . execute ( SERVER_URL )
48
+ . get ( '/foo ' )
24
49
. end ( function ( err , res ) {
25
50
res . should . have . status ( 200 ) ;
26
51
res . should . be . html ;
27
52
res . should . not . be . text ;
28
53
res . should . not . be . json ;
29
54
res . text . should . be . a ( 'string' ) . with . length . above ( 0 ) ;
30
55
31
- // Slightly different behavior in SuperAgent in Node/browsers
32
- isNode && res . body . should . deep . equal ( { } ) ;
33
- isBrowser && expect ( res . body ) . to . be . null ;
56
+ res . body . should . deep . equal ( { } ) ;
34
57
35
58
done ( err ) ;
36
59
} ) ;
37
60
} ) ;
38
61
39
62
it ( 'can request JSON data' , function ( done ) {
63
+ server . get ( '/foo' , ( _req , res ) => {
64
+ res . statusCode = 200 ;
65
+ res . setHeader ( 'content-type' , 'application/json' ) ;
66
+ res . end ( '{"foo":"bar"}' ) ;
67
+ } ) ;
68
+
40
69
request
41
- . execute ( 'https://chaijs.com' )
42
- . get ( '/package-lock.json ' )
70
+ . execute ( SERVER_URL )
71
+ . get ( '/foo ' )
43
72
. end ( function ( err , res ) {
44
73
res . should . have . status ( 200 ) ;
45
74
res . should . be . json ;
@@ -52,284 +81,177 @@ describe('request', function () {
52
81
} ) ;
53
82
54
83
it ( 'can read response headers' , function ( done ) {
55
- this . timeout ( 5000 ) ;
84
+ server . get ( '/foo' , ( _req , res ) => {
85
+ res . statusCode = 200 ;
86
+ res . setHeader ( 'content-type' , 'application/json' ) ;
87
+ res . setHeader ( 'x-foo' , '303' ) ;
88
+ res . setHeader ( 'x-bar' , '808' ) ;
89
+ res . end ( '{"foo":"bar"}' ) ;
90
+ } ) ;
91
+
56
92
request
57
- . execute ( 'https://webhook.site' )
58
- . post ( '/token ' )
93
+ . execute ( SERVER_URL )
94
+ . get ( '/foo ' )
59
95
. end ( function ( err , res ) {
60
- const uuid = res . body . uuid ;
61
- request
62
- . execute ( 'https://webhook.site' )
63
- . get ( '/' + uuid )
64
- . query ( { 'content-type' : 'application/json' } )
65
- . query ( { pragma : 'test1' } )
66
- . query ( { location : 'test2' } )
67
- . query ( { 'x-api-key' : 'test3' } )
68
- . end ( function ( err , res ) {
69
- res . should . have . status ( 200 ) ;
70
- request
71
- . execute ( 'https://webhook.site' )
72
- . get ( '/token/' + uuid + '/requests?sorting=newest&per_page=1' )
73
- . end ( function ( err , res ) {
74
- // Content-Type and Pragma are supported on Node and browser
75
- res . should . be . json ;
76
- res . should . have . nested . property (
77
- '.body.data.0.query.content-type' ,
78
- 'application/json'
79
- ) ;
80
- res . should . have . nested . property (
81
- '.body.data.0.query.pragma' ,
82
- 'test1'
83
- ) ;
84
-
85
- // When running in a browser, only "simple" headers are readable
86
- // https://www.w3.org/TR/cors/#simple-response-header
87
- isNode &&
88
- res . should . have . nested . property (
89
- '.body.data.0.query.location' ,
90
- 'test2'
91
- ) ;
92
- isNode &&
93
- res . should . have . nested . property (
94
- '.body.data.0.query.x-api-key' ,
95
- 'test3'
96
- ) ;
97
- isBrowser &&
98
- res . should . not . have . nested . property (
99
- '.body.data.0.query.location'
100
- ) ;
101
- isBrowser &&
102
- res . should . not . have . nested . property (
103
- '.body.data.0.query.x-api-key'
104
- ) ;
105
-
106
- done ( err ) ;
107
- } ) ;
108
- } ) ;
96
+ res . should . be . json ;
97
+ res . headers . should . have . property ( 'x-foo' , '303' ) ;
98
+ res . headers . should . have . property ( 'x-bar' , '808' ) ;
99
+ done ( err ) ;
109
100
} ) ;
110
101
} ) ;
111
102
112
103
it ( 'succeeds when response has an error status' , function ( done ) {
104
+ server . get ( '/foo' , ( _req , res ) => {
105
+ res . statusCode = 404 ;
106
+ res . end ( ) ;
107
+ } ) ;
113
108
request
114
- . execute ( 'https://chaijs.com' )
115
- . get ( '/404 ' )
109
+ . execute ( SERVER_URL )
110
+ . get ( '/foo ' )
116
111
. end ( function ( err , res ) {
117
112
res . should . have . status ( 404 ) ;
118
113
done ( err ) ;
119
114
} ) ;
120
115
} ) ;
121
116
122
- it ( 'can be augmented with promises' , function ( done ) {
123
- this . timeout ( 5000 ) ;
124
- let uuid = '' ;
125
- request
126
- . execute ( 'https://webhook.site' )
127
- . post ( '/token' )
128
- . then ( function ( res ) {
129
- uuid = res . body . uuid ;
130
- return res . body . uuid ;
131
- } )
132
- . then ( function ( uuid ) {
133
- return request
134
- . execute ( 'https://webhook.site' )
135
- . get ( '/' + uuid )
136
- . query ( { 'content-type' : 'application/json' } )
137
- . query ( { 'x-api-key' : 'test3' } ) ;
138
- } )
139
- . then ( function ( res ) {
140
- res . should . have . status ( 200 ) ;
141
- return request
142
- . execute ( 'https://webhook.site' )
143
- . get ( '/token/' + uuid + '/requests?sorting=newest&per_page=1' ) ;
144
- } )
145
- . then ( function ( res ) {
146
- res . should . have . status ( 200 ) ;
147
- res . should . be . json ;
148
- res . should . have . nested . property (
149
- '.body.data.0.query.content-type' ,
150
- 'application/json'
151
- ) ;
152
- res . should . have . nested . property (
153
- '.body.data.0.query.x-api-key' ,
154
- 'test3'
155
- ) ;
156
- } )
157
- . then ( function ( ) {
158
- throw new Error ( 'Testing catch' ) ;
159
- } )
160
- . then ( function ( ) {
161
- throw new Error ( 'This should not have fired' ) ;
162
- } )
163
- . catch ( function ( err ) {
164
- if ( err . message !== 'Testing catch' ) {
165
- throw err ;
166
- }
167
- } )
168
- . then ( done , done ) ;
117
+ it ( 'can be augmented with promises' , async function ( ) {
118
+ server . get ( '/foo' , ( _req , res ) => {
119
+ res . statusCode = 200 ;
120
+ res . setHeader ( 'content-type' , 'application/json' ) ;
121
+ res . end ( '{"foo":"bar"}' ) ;
122
+ } ) ;
123
+ const response = await request
124
+ . execute ( SERVER_URL )
125
+ . get ( '/foo' ) ;
126
+ response . should . have . status ( 200 ) ;
127
+ response . should . be . json ;
169
128
} ) ;
170
129
171
- it ( 'can resolve a promise given status code of 404' , function ( ) {
172
- return request
173
- . execute ( 'https://chaijs.com' )
174
- . get ( '/404' )
175
- . then ( function ( res ) {
176
- res . should . have . status ( 404 ) ;
177
- } ) ;
130
+ it ( 'can resolve a promise given status code of 404' , async function ( ) {
131
+ server . get ( '/foo' , ( _req , res ) => {
132
+ res . statusCode = 404 ;
133
+ res . end ( ) ;
134
+ } ) ;
135
+
136
+ const response = await request
137
+ . execute ( SERVER_URL )
138
+ . get ( '/foo' ) ;
139
+ response . should . have . status ( 404 ) ;
178
140
} ) ;
179
141
} ) ;
180
142
181
- isNode &&
182
- describe ( 'Node.js' , function ( ) {
183
- it ( 'can request a functioned "app"' , function ( done ) {
184
- const app = function ( req , res ) {
185
- req . headers [ 'x-api-key' ] . should . equal ( 'testing' ) ;
186
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
187
- res . end ( 'hello universe' ) ;
188
- } ;
143
+ describe ( 'Node.js' , function ( ) {
144
+ it ( 'can request a functioned "app"' , function ( done ) {
145
+ const app = function ( req , res ) {
146
+ req . headers [ 'x-api-key' ] . should . equal ( 'testing' ) ;
147
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
148
+ res . end ( 'hello universe' ) ;
149
+ } ;
189
150
190
- request
191
- . execute ( app )
192
- . get ( '/' )
193
- . set ( 'X-API-Key' , 'testing' )
194
- . end ( function ( err , res ) {
195
- if ( err ) return done ( err ) ;
196
- res . should . have . status ( 200 ) ;
197
- res . text . should . equal ( 'hello universe' ) ;
198
- done ( ) ;
199
- } ) ;
200
- } ) ;
201
-
202
- it ( 'can request an already existing url' , function ( done ) {
203
- const server = http . createServer ( function ( req , res ) {
204
- req . headers [ 'x-api-key' ] . should . equal ( 'test2' ) ;
205
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
206
- res . end ( 'hello world' ) ;
207
- } ) ;
208
-
209
- server . listen ( 0 , function ( ) {
210
- request
211
- . execute ( 'http://127.0.0.1:' + server . address ( ) . port )
212
- . get ( '/' )
213
- . set ( 'X-API-Key' , 'test2' )
214
- . end ( function ( err , res ) {
215
- res . should . have . status ( 200 ) ;
216
- res . text . should . equal ( 'hello world' ) ;
217
- server . once ( 'close' , function ( ) {
218
- done ( err ) ;
219
- } ) ;
220
- server . close ( ) ;
221
- } ) ;
151
+ request
152
+ . execute ( app )
153
+ . get ( '/' )
154
+ . set ( 'X-API-Key' , 'testing' )
155
+ . end ( function ( err , res ) {
156
+ if ( err ) return done ( err ) ;
157
+ res . should . have . status ( 200 ) ;
158
+ res . text . should . equal ( 'hello universe' ) ;
159
+ done ( ) ;
222
160
} ) ;
223
- } ) ;
224
-
225
- it ( 'agent can be used to persist cookies' , function ( done ) {
226
- const app = function ( req , res ) {
227
- res . setHeader ( 'Set-Cookie' , 'mycookie=test' ) ;
228
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
229
- res . end ( 'your cookie: ' + req . headers . cookie ) ;
230
- } ;
231
- const agent = request . agent ( app ) ;
161
+ } ) ;
232
162
233
- agent
234
- . get ( '/' )
235
- . then ( function ( res ) {
236
- res . headers [ 'set-cookie' ] [ 0 ] . should . equal ( 'mycookie=test' ) ;
237
- res . text . should . equal ( 'your cookie: undefined' ) ;
238
- } )
239
- . then ( function ( ) {
240
- return agent . get ( '/' ) ;
241
- } )
242
- . then ( function ( res ) {
243
- res . text . should . equal ( 'your cookie: mycookie=test' ) ;
244
- agent . close ( ) ;
245
- } )
246
- . then ( done , done ) ;
163
+ it ( 'can request an already existing url' , function ( done ) {
164
+ const server = http . createServer ( function ( req , res ) {
165
+ req . headers [ 'x-api-key' ] . should . equal ( 'test2' ) ;
166
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
167
+ res . end ( 'hello world' ) ;
247
168
} ) ;
248
169
249
- it ( 'automatically closes the server down once done with it' , function ( done ) {
250
- const server = http . createServer ( function ( req , res ) {
251
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
252
- res . end ( 'hello world' ) ;
253
- } ) ;
254
-
170
+ server . listen ( 0 , function ( ) {
255
171
request
256
- . execute ( server )
172
+ . execute ( 'http://127.0.0.1:' + server . address ( ) . port )
257
173
. get ( '/' )
174
+ . set ( 'X-API-Key' , 'test2' )
258
175
. end ( function ( err , res ) {
259
176
res . should . have . status ( 200 ) ;
260
177
res . text . should . equal ( 'hello world' ) ;
261
- should . not . exist ( server . address ( ) ) ;
262
- done ( err ) ;
178
+ server . once ( 'close' , function ( ) {
179
+ done ( err ) ;
180
+ } ) ;
181
+ server . close ( ) ;
263
182
} ) ;
264
183
} ) ;
184
+ } ) ;
265
185
266
- it ( 'can use keepOpen() to not close the server' , function ( done ) {
267
- const server = http . createServer ( function ( req , res ) {
268
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
269
- res . end ( 'hello world' ) ;
270
- } ) ;
271
- const cachedRequest = request . execute ( server ) . keepOpen ( ) ;
272
- server . listen = function ( ) {
273
- throw new Error ( 'listen was called when it shouldnt have been' ) ;
274
- } ;
275
- cachedRequest . get ( '/' ) . end ( function ( err ) {
276
- cachedRequest . get ( '/' ) . end ( function ( err2 ) {
277
- server . close ( function ( ) {
278
- done ( err || err2 ) ;
279
- } ) ;
280
- } ) ;
281
- } ) ;
186
+ it ( 'agent can be used to persist cookies' , function ( done ) {
187
+ const app = function ( req , res ) {
188
+ res . setHeader ( 'Set-Cookie' , 'mycookie=test' ) ;
189
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
190
+ res . end ( 'your cookie: ' + req . headers . cookie ) ;
191
+ } ;
192
+ const agent = request . agent ( app ) ;
193
+
194
+ agent
195
+ . get ( '/' )
196
+ . then ( function ( res ) {
197
+ res . headers [ 'set-cookie' ] [ 0 ] . should . equal ( 'mycookie=test' ) ;
198
+ res . text . should . equal ( 'your cookie: undefined' ) ;
199
+ } )
200
+ . then ( function ( ) {
201
+ return agent . get ( '/' ) ;
202
+ } )
203
+ . then ( function ( res ) {
204
+ res . text . should . equal ( 'your cookie: mycookie=test' ) ;
205
+ agent . close ( ) ;
206
+ } )
207
+ . then ( done , done ) ;
208
+ } ) ;
209
+
210
+ it ( 'automatically closes the server down once done with it' , function ( done ) {
211
+ const server = http . createServer ( function ( _req , res ) {
212
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
213
+ res . end ( 'hello world' ) ;
282
214
} ) ;
283
215
284
- it ( 'can close server after using keepOpen()' , function ( done ) {
285
- const server = http . createServer ( function ( req , res ) {
286
- res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
287
- res . end ( 'hello world' ) ;
288
- } ) ;
289
- const cachedRequest = request . execute ( server ) . keepOpen ( ) ;
290
- cachedRequest . close ( function ( ) {
216
+ request
217
+ . execute ( server )
218
+ . get ( '/' )
219
+ . end ( function ( err , res ) {
220
+ res . should . have . status ( 200 ) ;
221
+ res . text . should . equal ( 'hello world' ) ;
291
222
should . not . exist ( server . address ( ) ) ;
292
- done ( ) ;
223
+ done ( err ) ;
293
224
} ) ;
294
- } ) ;
295
225
} ) ;
296
226
297
- isBrowser &&
298
- describe ( 'Browser' , function ( ) {
299
- it ( 'cannot request a functioned "app"' , function ( ) {
300
- function tryToRequestAFunctionedApp ( ) {
301
- const app = function ( ) { } ;
302
- request . execute ( app ) ;
303
- }
304
- expect ( tryToRequestAFunctionedApp ) . to . throw (
305
- Error ,
306
- / h t t p .c r e a t e S e r v e r i s n o t a f u n c t i o n | c r e a t e S e r v e r /
307
- ) ;
227
+ it ( 'can use keepOpen() to not close the server' , function ( done ) {
228
+ const server = http . createServer ( function ( _req , res ) {
229
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
230
+ res . end ( 'hello world' ) ;
308
231
} ) ;
232
+ const cachedRequest = request . execute ( server ) . keepOpen ( ) ;
233
+ server . listen = function ( ) {
234
+ throw new Error ( 'listen was called when it shouldnt have been' ) ;
235
+ } ;
236
+ cachedRequest . get ( '/' ) . end ( function ( err ) {
237
+ cachedRequest . get ( '/' ) . end ( function ( err2 ) {
238
+ server . close ( function ( ) {
239
+ done ( err || err2 ) ;
240
+ } ) ;
241
+ } ) ;
242
+ } ) ;
243
+ } ) ;
309
244
310
- it ( 'agent can be used to persist cookies' , function ( done ) {
311
- const agent = request . agent ( 'https://httpbin.org' ) ;
312
-
313
- agent
314
- . get ( '/cookies/set' )
315
- . query ( { foo : 'bar' , biz : 'baz' } )
316
- . then ( function ( res ) {
317
- // When running in a web browser, cookies are protected and cannot be read by SuperAgent.
318
- // They ARE set, but only the browser has access to them.
319
- expect ( res . headers [ 'set-cookie' ] ) . to . be . undefined ;
320
- res . should . not . have . cookie ( 'foo' ) ;
321
- res . should . not . have . cookie ( 'bar' ) ;
322
- } )
323
- . then ( function ( ) {
324
- // When making a subsequent request to the same server, the cookies will be sent
325
- return agent . get ( '/cookies' ) ;
326
- } )
327
- . then ( function ( res ) {
328
- // HttpBin echoes the cookies back as JSON
329
- res . body . cookies . foo . should . equal ( 'bar' ) ;
330
- res . body . cookies . biz . should . equal ( 'baz' ) ;
331
- } )
332
- . then ( done , done ) ;
245
+ it ( 'can close server after using keepOpen()' , function ( done ) {
246
+ const server = http . createServer ( function ( _req , res ) {
247
+ res . writeHeader ( 200 , { 'content-type' : 'text/plain' } ) ;
248
+ res . end ( 'hello world' ) ;
249
+ } ) ;
250
+ const cachedRequest = request . execute ( server ) . keepOpen ( ) ;
251
+ cachedRequest . close ( function ( ) {
252
+ should . not . exist ( server . address ( ) ) ;
253
+ done ( ) ;
333
254
} ) ;
334
255
} ) ;
256
+ } ) ;
335
257
} ) ;
0 commit comments