/
test_reply_function_async.js
151 lines (122 loc) · 4.5 KB
/
test_reply_function_async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict'
// Tests for invoking `.reply()` with a function which invokes the error-first
// callback with the response body or an array containing the status code and
// optional response body and headers.
const { expect } = require('chai')
const assertRejects = require('assert-rejects')
const sinon = require('sinon')
const nock = require('..')
const got = require('./got_client')
require('./setup')
describe('asynchronous `reply()` function', () => {
describe('using callback', () => {
it('reply can take a callback', async () => {
const scope = nock('http://example.test')
.get('/')
.reply(200, (path, requestBody, callback) =>
callback(null, 'Hello World!')
)
const { body } = await got('http://example.test/', {
responseType: 'buffer',
})
expect(body).to.be.an.instanceOf(Buffer)
expect(body.toString('utf8')).to.equal('Hello World!')
scope.done()
})
it('reply takes a callback for status code', async () => {
const responseBody = 'Hello, world!'
const scope = nock('http://example.test')
.get('/')
.reply((path, requestBody, callback) => {
setTimeout(
() =>
callback(null, [
202,
responseBody,
{ 'X-Custom-Header': 'abcdef' },
]),
1
)
})
const { statusCode, headers, body } = await got('http://example.test/')
expect(statusCode).to.equal(202)
expect(headers).to.deep.equal({ 'x-custom-header': 'abcdef' })
expect(body).to.equal(responseBody)
scope.done()
})
it('reply should throw on error on the callback', async () => {
nock('http://example.test')
.get('/')
.reply(500, (path, requestBody, callback) =>
callback(new Error('Database failed'))
)
await assertRejects(got('http://example.test'), /Database failed/)
})
it('an error passed to the callback propagates when [err, fullResponseArray] is expected', async () => {
nock('http://example.test')
.get('/')
.reply((path, requestBody, callback) => {
callback(Error('boom'))
})
await assertRejects(got('http://example.test'), /boom/)
})
it('subsequent calls to the reply callback are ignored', async () => {
const replyFnCalled = sinon.spy()
const scope = nock('http://example.test')
.get('/')
.reply(201, (path, requestBody, callback) => {
replyFnCalled()
callback(null, 'one')
callback(null, 'two')
callback(new Error('three'))
})
const { statusCode, body } = await got('http://example.test/')
expect(replyFnCalled).to.have.been.calledOnce()
expect(statusCode).to.equal(201)
expect(body).to.equal('one')
scope.done()
})
})
describe('using async/promises', () => {
it('reply can take a status code with an 2-arg async function, and passes it the correct arguments', async () => {
const scope = nock('http://example.com')
.post('/foo')
.reply(201, async (path, requestBody) => {
expect(path).to.equal('/foo')
expect(requestBody).to.equal('request-body')
return 'response-body'
})
const { statusCode, body } = await got.post('http://example.com/foo', {
body: 'request-body',
})
expect(statusCode).to.equal(201)
expect(body).to.equal('response-body')
scope.done()
})
it('reply can take a status code with a 0-arg async function, and passes it the correct arguments', async () => {
const scope = nock('http://example.com')
.get('/')
.reply(async () => [201, 'Hello World!'])
const { statusCode, body } = await got('http://example.com/')
expect(statusCode).to.equal(201)
expect(body).to.equal('Hello World!')
scope.done()
})
it('when reply is called with a status code and an async function that throws, it propagates the error', async () => {
nock('http://example.test')
.get('/')
.reply(201, async () => {
throw Error('oh no!')
})
await assertRejects(got('http://example.test'), /oh no!/)
})
it('when reply is called with an async function that throws, it propagates the error', async () => {
nock('http://example.test')
.get('/')
.reply(async () => {
throw Error('oh no!')
})
await assertRejects(got('http://example.test'), /oh no!/)
})
})
})