-
-
Notifications
You must be signed in to change notification settings - Fork 734
/
test_reply_function_async.js
127 lines (104 loc) · 3.18 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
'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 assertRejects = require('assert-rejects')
const http = require('http')
const { test } = require('tap')
const nock = require('..')
const got = require('./got_client')
require('./cleanup_after_each')()
test('reply can take a callback', async t => {
const scope = nock('http://example.test')
.get('/')
.reply(200, (path, requestBody, callback) => callback(null, 'Hello World!'))
const response = await got('http://example.test/', {
encoding: null,
})
scope.done()
t.type(response.body, Buffer)
t.equal(response.body.toString('utf8'), 'Hello World!')
})
test('reply takes a callback for status code', async t => {
const expectedStatusCode = 202
const responseBody = 'Hello, world!'
const headers = {
'X-Custom-Header': 'abcdef',
}
const scope = nock('http://example.test')
.get('/')
.reply((path, requestBody, cb) => {
setTimeout(() => cb(null, [expectedStatusCode, responseBody, headers]), 1)
})
const response = await got('http://example.test/')
t.equal(response.statusCode, expectedStatusCode, 'sends status code')
t.deepEqual(
response.headers,
{ 'x-custom-header': 'abcdef' },
'sends headers'
)
t.equal(response.body, responseBody, 'sends request body')
scope.done()
})
test('reply should throw on error on the callback', t => {
let dataCalled = false
const scope = nock('http://example.test')
.get('/')
.reply(500, (path, requestBody, callback) =>
callback(new Error('Database failed'))
)
// TODO When this request is converted to `got`, it causes the request not
// to match.
const req = http.request(
{
host: 'example.test',
path: '/',
port: 80,
},
res => {
t.equal(res.statusCode, 500, 'Status code is 500')
res.on('data', data => {
dataCalled = true
t.ok(data instanceof Buffer, 'data should be buffer')
t.ok(
data.toString().indexOf('Error: Database failed') === 0,
'response should match'
)
})
res.on('end', () => {
t.ok(dataCalled, 'data handler was called')
scope.done()
t.end()
})
}
)
req.end()
})
test('an error passed to the callback propagates when [err, fullResponseArray] is expected', async t => {
const scope = nock('http://example.test')
.get('/')
.reply((path, requestBody, callback) => {
callback(Error('boom'))
})
await assertRejects(got('http://example.test/'), ({ statusCode, body }) => {
t.is(statusCode, 500)
t.matches(body, 'Error: boom')
return true
})
scope.done()
})
test('subsequent calls to the reply callback are ignored', async t => {
t.plan(3)
const scope = nock('http://example.test')
.get('/')
.reply(201, (path, requestBody, callback) => {
callback(null, 'one')
callback(null, 'two')
callback(null, 'three')
t.pass()
})
const { statusCode, body } = await got('http://example.test/')
scope.done()
t.is(statusCode, 201)
t.equal(body, 'one')
})