-
-
Notifications
You must be signed in to change notification settings - Fork 733
/
test_reply_body.js
160 lines (120 loc) · 4.79 KB
/
test_reply_body.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
152
153
154
155
156
157
158
159
160
'use strict'
// Tests for the body argument passed to `.reply()`.
const { expect } = require('chai')
const nock = require('../..')
const got = require('./got_client')
describe('`reply()` body', () => {
it('stringifies an object', async () => {
const responseBody = { hello: 'world' }
const scope = nock('http://example.test').get('/').reply(200, responseBody)
const { statusCode, headers, body } = await got('http://example.test/')
expect(statusCode).to.equal(200)
expect(headers).not.to.have.property('date')
expect(headers).not.to.have.property('content-length')
expect(headers).to.include({ 'content-type': 'application/json' })
expect(body).to.be.a('string').and.equal(JSON.stringify(responseBody))
scope.done()
})
it('stringifies an array', async () => {
const responseBody = [{ hello: 'world' }]
const scope = nock('http://example.test').get('/').reply(200, responseBody)
const { statusCode, headers, body } = await got('http://example.test/')
expect(statusCode).to.equal(200)
expect(headers).not.to.have.property('date')
expect(headers).not.to.have.property('content-length')
expect(headers).to.include({ 'content-type': 'application/json' })
expect(body).to.be.a('string').and.equal(JSON.stringify(responseBody))
scope.done()
})
// While `false` and `null` are falsy, they are valid JSON value so they
// should be returned as strings that `JSON.parse()` would convert back to
// native values.
it('stringifies a boolean (including `false`)', async () => {
const scope = nock('http://example.test').get('/').reply(204, false)
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(204)
// `'false'` is json-stringified `false`.
expect(body).to.be.a('string').and.equal('false')
scope.done()
})
it('stringifies null', async () => {
const scope = nock('http://example.test').get('/').reply(204, null)
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(204)
// `'null'` is json-stringified `null`.
expect(body).to.be.a('string').and.equal('null')
scope.done()
})
describe('content-type header', () => {
it('is set for a JSON-encoded response', async () => {
const scope = nock('http://example.test').get('/').reply(200, { A: 'b' })
const { headers } = await got('http://example.test/')
expect(headers).to.include({ 'content-type': 'application/json' })
scope.done()
})
it("doesn't overwrite existing content-type header", async () => {
const scope = nock('http://example.test')
.get('/')
.reply(200, { A: 'b' }, { 'Content-Type': 'unicorns' })
const { headers } = await got('http://example.test/')
expect(headers).to.include({ 'content-type': 'unicorns' })
scope.done()
})
it("isn't set for a blank response", async () => {
const scope = nock('http://example.test').get('/').reply()
const { headers } = await got('http://example.test/')
expect(headers).not.to.have.property('content-type')
scope.done()
})
})
it('unencodable object throws the expected error', () => {
const unencodableObject = {
toJSON() {
throw Error('bad!')
},
}
expect(() =>
nock('http://localhost').get('/').reply(200, unencodableObject),
).to.throw(Error, 'Error encoding response body into JSON')
})
it('without a body, defaults to empty', async () => {
const scope = nock('http://example.test').get('/').reply(204)
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(204)
expect(body).to.be.a('string').and.equal('')
scope.done()
})
it('does not modify the object used for response', async () => {
const patchBody = { number: 1234 }
const form = new FormData()
form.append('number', 1234)
const scope = nock('http://example.test')
.patch('/', patchBody)
.reply(200, patchBody)
await got.patch('http://example.test/', {
form,
})
expect(patchBody.number).to.be.a('number').and.equal(1234)
scope.done()
})
})
describe('`reply()` status code', () => {
it('reply with missing status code defaults to 200', async () => {
const scope = nock('http://example.test').get('/').reply()
const { statusCode, body } = await got('http://example.test/')
expect(statusCode).to.equal(200)
expect(body).to.be.a('string').and.equal('')
scope.done()
})
it('reply with invalid status code throws', () => {
const scope = nock('http://localhost').get('/')
expect(() => scope.reply('200')).to.throw(
Error,
'Invalid string value for status code',
)
expect(() => scope.reply(false)).to.throw(
Error,
'Invalid boolean value for status code',
)
})
})