1
1
const chai = require ( 'chai' )
2
2
const getUserAgent = require ( 'universal-user-agent' )
3
- const fetchMock = require ( 'fetch-mock' )
3
+ const fetchMock = require ( 'fetch-mock/es5/server ' )
4
4
const sinonChai = require ( 'sinon-chai' )
5
5
6
6
const octokitRequest = require ( '..' )
@@ -20,7 +20,7 @@ describe('octokitRequest()', () => {
20
20
expect ( octokitRequest ) . to . be . a ( 'function' )
21
21
} )
22
22
23
- it ( 'README example' , async ( ) => {
23
+ it ( 'README example' , ( ) => {
24
24
octokitRequest . fetch = fetchMock . sandbox ( )
25
25
. mock ( 'https://api.github.com/orgs/octokit/repos?type=private' , [ ] , {
26
26
headers : {
@@ -30,18 +30,20 @@ describe('octokitRequest()', () => {
30
30
}
31
31
} )
32
32
33
- const result = await octokitRequest ( 'GET /orgs/:org/repos' , {
33
+ return octokitRequest ( 'GET /orgs/:org/repos' , {
34
34
headers : {
35
35
authorization : 'token 0000000000000000000000000000000000000001'
36
36
} ,
37
37
org : 'octokit' ,
38
38
type : 'private'
39
39
} )
40
40
41
- expect ( result . data ) . to . deep . equal ( [ ] )
41
+ . then ( response => {
42
+ expect ( response . data ) . to . deep . equal ( [ ] )
43
+ } )
42
44
} )
43
45
44
- it ( 'README example alternative' , async ( ) => {
46
+ it ( 'README example alternative' , ( ) => {
45
47
octokitRequest . fetch = fetchMock . sandbox ( )
46
48
. mock ( 'https://api.github.com/orgs/octokit/repos?type=private' , [ ] , {
47
49
headers : {
@@ -54,7 +56,7 @@ describe('octokitRequest()', () => {
54
56
octokitRequest . fetch = fetchMock . sandbox ( )
55
57
. mock ( 'https://api.github.com/orgs/octokit/repos?type=private' , [ ] )
56
58
57
- const result = await octokitRequest ( {
59
+ return octokitRequest ( {
58
60
method : 'GET' ,
59
61
url : '/orgs/:org/repos' ,
60
62
headers : {
@@ -64,18 +66,20 @@ describe('octokitRequest()', () => {
64
66
type : 'private'
65
67
} )
66
68
67
- expect ( result . data ) . to . deep . equal ( [ ] )
69
+ . then ( response => {
70
+ expect ( response . data ) . to . deep . equal ( [ ] )
71
+ } )
68
72
} )
69
73
70
- it ( 'Request with body' , async ( ) => {
74
+ it ( 'Request with body' , ( ) => {
71
75
octokitRequest . fetch = fetchMock . sandbox ( )
72
76
. mock ( 'https://api.github.com/repos/octocat/hello-world/issues' , 201 , {
73
77
headers : {
74
78
'content-type' : 'application/json; charset=utf-8'
75
79
}
76
80
} )
77
81
78
- const response = await octokitRequest ( 'POST /repos/:owner/:repo/issues' , {
82
+ octokitRequest ( 'POST /repos/:owner/:repo/issues' , {
79
83
owner : 'octocat' ,
80
84
repo : 'hello-world' ,
81
85
headers : {
@@ -92,29 +96,33 @@ describe('octokitRequest()', () => {
92
96
]
93
97
} )
94
98
95
- expect ( response . status ) . to . equal ( 201 )
99
+ . then ( response => {
100
+ expect ( response . status ) . to . equal ( 201 )
101
+ } )
96
102
} )
97
103
98
- it ( 'Put without request body' , async ( ) => {
104
+ it ( 'Put without request body' , ( ) => {
99
105
octokitRequest . fetch = fetchMock . sandbox ( )
100
106
. mock ( 'https://api.github.com/user/starred/octocat/hello-world' , 204 , {
101
107
headers : {
102
108
'content-length' : 0
103
109
}
104
110
} )
105
111
106
- const response = await octokitRequest ( 'PUT /user/starred/:owner/:repo' , {
112
+ octokitRequest ( 'PUT /user/starred/:owner/:repo' , {
107
113
headers : {
108
114
authorization : `token 0000000000000000000000000000000000000001`
109
115
} ,
110
116
owner : 'octocat' ,
111
117
repo : 'hello-world'
112
118
} )
113
119
114
- expect ( response . status ) . to . equal ( 204 )
120
+ . then ( response => {
121
+ expect ( response . status ) . to . equal ( 204 )
122
+ } )
115
123
} )
116
124
117
- it ( 'HEAD requests (octokit/rest.js#841)' , async ( ) => {
125
+ it ( 'HEAD requests (octokit/rest.js#841)' , ( ) => {
118
126
octokitRequest . fetch = fetchMock . sandbox ( )
119
127
. head ( 'https://api.github.com/repos/whatwg/html/pulls/1' , {
120
128
status : 200 ,
@@ -136,20 +144,25 @@ describe('octokitRequest()', () => {
136
144
repo : 'html' ,
137
145
number : 1
138
146
}
139
- let error
140
- const response = await octokitRequest ( `HEAD /repos/:owner/:repo/pulls/:number` , options )
141
- try {
142
- await octokitRequest ( `HEAD /repos/:owner/:repo/pulls/:number` , Object . assign ( options , { number : 2 } ) )
143
- throw new Error ( 'should not resolve' )
144
- } catch ( error_ ) {
145
- error = error_
146
- }
147
147
148
- expect ( response . status ) . to . equal ( 200 )
149
- expect ( error . code ) . to . equal ( 404 )
148
+ octokitRequest ( `HEAD /repos/:owner/:repo/pulls/:number` , options )
149
+
150
+ . then ( response => {
151
+ expect ( response . status ) . to . equal ( 200 )
152
+
153
+ return octokitRequest ( `HEAD /repos/:owner/:repo/pulls/:number` , Object . assign ( options , { number : 2 } ) )
154
+ } )
155
+
156
+ . then ( ( ) => {
157
+ throw new Error ( 'should not resolve' )
158
+ } )
159
+
160
+ . catch ( error => {
161
+ expect ( error . code ) . to . equal ( 404 )
162
+ } )
150
163
} )
151
164
152
- it . skip ( 'Binary response with redirect (🤔 unclear how to mock fetch redirect properly)' , async ( ) => {
165
+ it . skip ( 'Binary response with redirect (🤔 unclear how to mock fetch redirect properly)' , ( ) => {
153
166
octokitRequest . fetch = fetchMock . sandbox ( )
154
167
. get ( 'https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master' , {
155
168
status : 200 ,
@@ -160,19 +173,21 @@ describe('octokitRequest()', () => {
160
173
}
161
174
} )
162
175
163
- const response = await octokitRequest ( 'GET /repos/:owner/:repo/:archive_format/:ref' , {
176
+ return octokitRequest ( 'GET /repos/:owner/:repo/:archive_format/:ref' , {
164
177
owner : 'octokit-fixture-org' ,
165
178
repo : 'get-archive' ,
166
179
archive_format : 'tarball' ,
167
180
ref : 'master'
168
181
} )
169
182
170
- expect ( response . data . length ) . to . equal ( 172 )
183
+ . then ( response => {
184
+ expect ( response . data . length ) . to . equal ( 172 )
185
+ } )
171
186
} )
172
187
173
188
// TODO: fails with "response.buffer is not a function" in browser
174
189
if ( ! process . browser ) {
175
- it ( 'Binary response' , async ( ) => {
190
+ it ( 'Binary response' , ( ) => {
176
191
octokitRequest . fetch = fetchMock . sandbox ( )
177
192
. get ( 'https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master' , {
178
193
status : 200 ,
@@ -186,44 +201,49 @@ describe('octokitRequest()', () => {
186
201
}
187
202
} )
188
203
189
- await octokitRequest ( 'GET https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master' )
204
+ return octokitRequest ( 'GET https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master' )
190
205
} )
191
206
}
192
207
193
- it ( '304 etag' , async ( ) => {
208
+ it ( '304 etag' , ( ) => {
194
209
octokitRequest . fetch = fetchMock . sandbox ( )
195
210
. get ( ( url , { headers } ) => {
196
211
return url === 'https://api.github.com/orgs/myorg' &&
197
212
headers [ 'if-none-match' ] === 'etag'
198
213
} , 304 )
199
214
200
- try {
201
- await octokitRequest ( 'GET /orgs/:org' , {
202
- org : 'myorg' ,
203
- headers : { 'If-None-Match' : 'etag' }
215
+ return octokitRequest ( 'GET /orgs/:org' , {
216
+ org : 'myorg' ,
217
+ headers : { 'If-None-Match' : 'etag' }
218
+ } )
219
+
220
+ . then ( ( ) => {
221
+ throw new Error ( 'should not resolve' )
222
+ } )
223
+
224
+ . catch ( error => {
225
+ expect ( error . code ) . to . equal ( 304 )
204
226
} )
205
- throw new Error ( 'should not resolve' )
206
- } catch ( error ) {
207
- expect ( error . code ) . to . equal ( 304 )
208
- }
209
227
} )
210
228
211
- it ( 'Not found' , async ( ) => {
229
+ it ( 'Not found' , ( ) => {
212
230
octokitRequest . fetch = fetchMock . sandbox ( )
213
231
. get ( 'path:/orgs/nope' , 404 )
214
232
215
- try {
216
- await octokitRequest ( 'GET /orgs/:org' , {
217
- org : 'nope'
233
+ return octokitRequest ( 'GET /orgs/:org' , {
234
+ org : 'nope'
235
+ } )
236
+
237
+ . then ( ( ) => {
238
+ throw new Error ( 'should not resolve' )
218
239
} )
219
240
220
- throw new Error ( 'should not resolve' )
221
- } catch ( error ) {
222
- expect ( error . code ) . to . equal ( 404 )
223
- }
241
+ . catch ( error => {
242
+ expect ( error . code ) . to . equal ( 404 )
243
+ } )
224
244
} )
225
245
226
- it ( 'non-JSON response' , async ( ) => {
246
+ it ( 'non-JSON response' , ( ) => {
227
247
octokitRequest . fetch = fetchMock . sandbox ( )
228
248
. get ( 'path:/repos/octokit-fixture-org/hello-world/contents/README.md' , {
229
249
status : 200 ,
@@ -234,7 +254,7 @@ describe('octokitRequest()', () => {
234
254
}
235
255
} )
236
256
237
- const response = await octokitRequest ( 'GET /repos/:owner/:repo/contents/:path' , {
257
+ return octokitRequest ( 'GET /repos/:owner/:repo/contents/:path' , {
238
258
headers : {
239
259
accept : 'application/vnd.github.v3.raw'
240
260
} ,
@@ -243,25 +263,31 @@ describe('octokitRequest()', () => {
243
263
path : 'README.md'
244
264
} )
245
265
246
- expect ( response . data ) . to . equal ( '# hello-world' )
266
+ . then ( ( response ) => {
267
+ expect ( response . data ) . to . equal ( '# hello-world' )
268
+ } )
247
269
} )
248
270
249
271
if ( ! process . browser ) {
250
- it ( 'Request error' , async ( ) => {
251
- try {
252
- await octokitRequest ( 'GET https://127.0.0.1:8/' ) // port: 8 // officially unassigned port. See https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
253
- throw new Error ( 'should not resolve' )
254
- } catch ( error ) {
255
- expect ( error . code ) . to . equal ( 500 )
256
- }
272
+ it ( 'Request error' , ( ) => {
273
+ // port: 8 // officially unassigned port. See https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
274
+ return octokitRequest ( 'GET https://127.0.0.1:8/' )
275
+
276
+ . then ( ( ) => {
277
+ throw new Error ( 'should not resolve' )
278
+ } )
279
+
280
+ . catch ( error => {
281
+ expect ( error . code ) . to . equal ( 500 )
282
+ } )
257
283
} )
258
284
}
259
285
260
- it ( 'custom user-agent' , async ( ) => {
286
+ it ( 'custom user-agent' , ( ) => {
261
287
octokitRequest . fetch = fetchMock . sandbox ( )
262
288
. get ( ( url , { headers } ) => headers [ 'user-agent' ] === 'funky boom boom pow' , 200 )
263
289
264
- await octokitRequest ( 'GET /' , {
290
+ return octokitRequest ( 'GET /' , {
265
291
headers : {
266
292
'user-agent' : 'funky boom boom pow'
267
293
}
0 commit comments