Skip to content

Commit 5656a36

Browse files
committed
test: Node 6 compatibility
1 parent 942d5bc commit 5656a36

File tree

1 file changed

+84
-58
lines changed

1 file changed

+84
-58
lines changed

test/request-test.js

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const chai = require('chai')
22
const getUserAgent = require('universal-user-agent')
3-
const fetchMock = require('fetch-mock')
3+
const fetchMock = require('fetch-mock/es5/server')
44
const sinonChai = require('sinon-chai')
55

66
const octokitRequest = require('..')
@@ -20,7 +20,7 @@ describe('octokitRequest()', () => {
2020
expect(octokitRequest).to.be.a('function')
2121
})
2222

23-
it('README example', async () => {
23+
it('README example', () => {
2424
octokitRequest.fetch = fetchMock.sandbox()
2525
.mock('https://api.github.com/orgs/octokit/repos?type=private', [], {
2626
headers: {
@@ -30,18 +30,20 @@ describe('octokitRequest()', () => {
3030
}
3131
})
3232

33-
const result = await octokitRequest('GET /orgs/:org/repos', {
33+
return octokitRequest('GET /orgs/:org/repos', {
3434
headers: {
3535
authorization: 'token 0000000000000000000000000000000000000001'
3636
},
3737
org: 'octokit',
3838
type: 'private'
3939
})
4040

41-
expect(result.data).to.deep.equal([])
41+
.then(response => {
42+
expect(response.data).to.deep.equal([])
43+
})
4244
})
4345

44-
it('README example alternative', async () => {
46+
it('README example alternative', () => {
4547
octokitRequest.fetch = fetchMock.sandbox()
4648
.mock('https://api.github.com/orgs/octokit/repos?type=private', [], {
4749
headers: {
@@ -54,7 +56,7 @@ describe('octokitRequest()', () => {
5456
octokitRequest.fetch = fetchMock.sandbox()
5557
.mock('https://api.github.com/orgs/octokit/repos?type=private', [])
5658

57-
const result = await octokitRequest({
59+
return octokitRequest({
5860
method: 'GET',
5961
url: '/orgs/:org/repos',
6062
headers: {
@@ -64,18 +66,20 @@ describe('octokitRequest()', () => {
6466
type: 'private'
6567
})
6668

67-
expect(result.data).to.deep.equal([])
69+
.then(response => {
70+
expect(response.data).to.deep.equal([])
71+
})
6872
})
6973

70-
it('Request with body', async () => {
74+
it('Request with body', () => {
7175
octokitRequest.fetch = fetchMock.sandbox()
7276
.mock('https://api.github.com/repos/octocat/hello-world/issues', 201, {
7377
headers: {
7478
'content-type': 'application/json; charset=utf-8'
7579
}
7680
})
7781

78-
const response = await octokitRequest('POST /repos/:owner/:repo/issues', {
82+
octokitRequest('POST /repos/:owner/:repo/issues', {
7983
owner: 'octocat',
8084
repo: 'hello-world',
8185
headers: {
@@ -92,29 +96,33 @@ describe('octokitRequest()', () => {
9296
]
9397
})
9498

95-
expect(response.status).to.equal(201)
99+
.then(response => {
100+
expect(response.status).to.equal(201)
101+
})
96102
})
97103

98-
it('Put without request body', async () => {
104+
it('Put without request body', () => {
99105
octokitRequest.fetch = fetchMock.sandbox()
100106
.mock('https://api.github.com/user/starred/octocat/hello-world', 204, {
101107
headers: {
102108
'content-length': 0
103109
}
104110
})
105111

106-
const response = await octokitRequest('PUT /user/starred/:owner/:repo', {
112+
octokitRequest('PUT /user/starred/:owner/:repo', {
107113
headers: {
108114
authorization: `token 0000000000000000000000000000000000000001`
109115
},
110116
owner: 'octocat',
111117
repo: 'hello-world'
112118
})
113119

114-
expect(response.status).to.equal(204)
120+
.then(response => {
121+
expect(response.status).to.equal(204)
122+
})
115123
})
116124

117-
it('HEAD requests (octokit/rest.js#841)', async () => {
125+
it('HEAD requests (octokit/rest.js#841)', () => {
118126
octokitRequest.fetch = fetchMock.sandbox()
119127
.head('https://api.github.com/repos/whatwg/html/pulls/1', {
120128
status: 200,
@@ -136,20 +144,25 @@ describe('octokitRequest()', () => {
136144
repo: 'html',
137145
number: 1
138146
}
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-
}
147147

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+
})
150163
})
151164

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)', () => {
153166
octokitRequest.fetch = fetchMock.sandbox()
154167
.get('https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master', {
155168
status: 200,
@@ -160,19 +173,21 @@ describe('octokitRequest()', () => {
160173
}
161174
})
162175

163-
const response = await octokitRequest('GET /repos/:owner/:repo/:archive_format/:ref', {
176+
return octokitRequest('GET /repos/:owner/:repo/:archive_format/:ref', {
164177
owner: 'octokit-fixture-org',
165178
repo: 'get-archive',
166179
archive_format: 'tarball',
167180
ref: 'master'
168181
})
169182

170-
expect(response.data.length).to.equal(172)
183+
.then(response => {
184+
expect(response.data.length).to.equal(172)
185+
})
171186
})
172187

173188
// TODO: fails with "response.buffer is not a function" in browser
174189
if (!process.browser) {
175-
it('Binary response', async () => {
190+
it('Binary response', () => {
176191
octokitRequest.fetch = fetchMock.sandbox()
177192
.get('https://codeload.github.com/octokit-fixture-org/get-archive/legacy.tar.gz/master', {
178193
status: 200,
@@ -186,44 +201,49 @@ describe('octokitRequest()', () => {
186201
}
187202
})
188203

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')
190205
})
191206
}
192207

193-
it('304 etag', async () => {
208+
it('304 etag', () => {
194209
octokitRequest.fetch = fetchMock.sandbox()
195210
.get((url, { headers }) => {
196211
return url === 'https://api.github.com/orgs/myorg' &&
197212
headers['if-none-match'] === 'etag'
198213
}, 304)
199214

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)
204226
})
205-
throw new Error('should not resolve')
206-
} catch (error) {
207-
expect(error.code).to.equal(304)
208-
}
209227
})
210228

211-
it('Not found', async () => {
229+
it('Not found', () => {
212230
octokitRequest.fetch = fetchMock.sandbox()
213231
.get('path:/orgs/nope', 404)
214232

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')
218239
})
219240

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+
})
224244
})
225245

226-
it('non-JSON response', async () => {
246+
it('non-JSON response', () => {
227247
octokitRequest.fetch = fetchMock.sandbox()
228248
.get('path:/repos/octokit-fixture-org/hello-world/contents/README.md', {
229249
status: 200,
@@ -234,7 +254,7 @@ describe('octokitRequest()', () => {
234254
}
235255
})
236256

237-
const response = await octokitRequest('GET /repos/:owner/:repo/contents/:path', {
257+
return octokitRequest('GET /repos/:owner/:repo/contents/:path', {
238258
headers: {
239259
accept: 'application/vnd.github.v3.raw'
240260
},
@@ -243,25 +263,31 @@ describe('octokitRequest()', () => {
243263
path: 'README.md'
244264
})
245265

246-
expect(response.data).to.equal('# hello-world')
266+
.then((response) => {
267+
expect(response.data).to.equal('# hello-world')
268+
})
247269
})
248270

249271
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+
})
257283
})
258284
}
259285

260-
it('custom user-agent', async () => {
286+
it('custom user-agent', () => {
261287
octokitRequest.fetch = fetchMock.sandbox()
262288
.get((url, { headers }) => headers['user-agent'] === 'funky boom boom pow', 200)
263289

264-
await octokitRequest('GET /', {
290+
return octokitRequest('GET /', {
265291
headers: {
266292
'user-agent': 'funky boom boom pow'
267293
}

0 commit comments

Comments
 (0)