forked from nock/nock
/
test_allow_unmocked.js
326 lines (269 loc) · 7.94 KB
/
test_allow_unmocked.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
'use strict'
const http = require('http')
const { expect } = require('chai')
const url = require('url')
const mikealRequest = require('request')
const { test } = require('tap')
const nock = require('..')
const got = require('./got_client')
require('./cleanup_after_each')()
test('with allowUnmocked, mocked request still works', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.post('/')
.reply(200, '99problems')
const { body, statusCode } = await got.post('http://example.test/')
t.equal(statusCode, 200)
t.equal(body, '99problems')
scope.done()
})
test('allow unmocked works after one interceptor is removed', async t => {
const server = http.createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())
await new Promise(resolve => server.listen(resolve))
const url = `http://localhost:${server.address().port}`
nock(url, { allowUnmocked: true })
.get('/')
.reply(200, 'Mocked')
t.equal((await got(url)).body, 'Mocked')
t.equal((await got(url)).body, 'live')
})
test('allow unmocked option works', t => {
t.plan(7)
const server = http.createServer((request, response) => {
t.pass('server received a request')
switch (url.parse(request.url).pathname) {
case '/':
response.writeHead(200)
response.write('server served a response')
break
case '/not/available':
response.writeHead(404)
break
case '/abc':
response.writeHead(200)
response.write('server served a response')
break
}
response.end()
})
server.listen(() => {
const scope = nock(`http://localhost:${server.address().port}`, {
allowUnmocked: true,
})
.get('/abc')
.reply(304, 'served from our mock')
.get('/wont/get/here')
.reply(304, 'served from our mock')
function secondIsDone() {
t.ok(!scope.isDone())
http
.request(
{
host: 'localhost',
path: '/',
port: server.address().port,
},
response => {
response.destroy()
t.is(response.statusCode, 200, 'Do not intercept /')
server.close(t.end)
}
)
.end()
}
function firstIsDone() {
t.ok(!scope.isDone())
http
.request(
{
host: 'localhost',
path: '/not/available',
port: server.address().port,
},
response => {
t.assert(
response.statusCode === 404,
'Server says it does not exist'
)
response.on('data', function() {})
response.on('end', secondIsDone)
}
)
.end()
}
const request = http.request(
{
host: 'localhost',
path: '/abc',
port: server.address().port,
},
response => {
t.is(response.statusCode, 304, 'Intercept /abc')
response.on('end', firstIsDone)
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
response.resume()
}
)
request.on('error', t.error)
request.end()
})
})
test('allow unmocked post with json data', t => {
t.plan(3)
t.once('end', function() {
server.close()
})
const server = http.createServer((request, response) => {
t.pass('server received a request')
response.writeHead(200)
response.end()
})
server.listen(() => {
nock(`http://localhost:${server.address().port}`, { allowUnmocked: true })
.get('/')
.reply(200, 'Hey!')
const options = {
method: 'POST',
uri: `http://localhost:${server.address().port}`,
json: { some: 'data' },
}
mikealRequest(options, function(err, resp) {
t.error(err)
t.equal(200, resp.statusCode)
t.end()
})
})
})
test('allow unmocked passthrough with mismatched bodies', t => {
t.plan(3)
t.once('end', function() {
server.close()
})
const server = http.createServer((request, response) => {
t.pass('server received a request')
response.writeHead(200)
response.end()
})
server.listen(() => {
nock(`http://localhost:${server.address().port}`, { allowUnmocked: true })
.post('/post', { some: 'other data' })
.reply(404, 'Hey!')
const options = {
method: 'POST',
uri: `http://localhost:${server.address().port}/post`,
json: { some: 'data' },
}
mikealRequest(options, function(err, resp) {
t.error(err)
t.equal(200, resp.statusCode)
t.end()
})
})
})
test('match path using regexp with allowUnmocked', t => {
nock('http://example.test', { allowUnmocked: true })
.get(/regex$/)
.reply(200, 'Match regex')
mikealRequest.get('http://example.test/resources/regex', function(
err,
res,
body
) {
t.type(err, 'null')
t.equal(res.statusCode, 200)
t.equal(body, 'Match regex')
t.end()
})
})
test('match hostname using regexp with allowUnmocked (issue-1076)', t => {
nock(/localhost/, { allowUnmocked: true })
.get('/no/regex/here')
.reply(200, 'Match regex')
mikealRequest.get('http://localhost:3000/no/regex/here', function(
err,
res,
body
) {
t.type(err, 'null')
t.equal(res.statusCode, 200)
t.equal(body, 'Match regex')
t.end()
})
})
// https://github.com/nock/nock/issues/1867
test('match path using callback with allowUnmocked', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get(uri => uri.endsWith('bar'))
.reply()
const { statusCode } = await got('http://example.test/foo/bar')
expect(statusCode).to.equal(200)
scope.done()
})
// https://github.com/nock/nock/issues/835
test('match multiple paths to domain using regexp with allowUnmocked', async t => {
const server = http.createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())
await new Promise(resolve => server.listen(resolve))
const url = `http://localhost:${server.address().port}`
const scope1 = nock(/localhost/, { allowUnmocked: true })
.get(/alpha/)
.reply(200, 'this is alpha')
const scope2 = nock(/localhost/, { allowUnmocked: true })
.get(/bravo/)
.reply(200, 'bravo, bravo!')
t.equal((await got(`${url}`)).body, 'live')
t.equal((await got(`${url}/alphalicious`)).body, 'this is alpha')
t.equal((await got(`${url}/bravo-company`)).body, 'bravo, bravo!')
scope1.done()
scope2.done()
})
test('match domain and path with literal query params and allowUnmocked', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get('/foo?bar=baz')
.reply()
const { statusCode } = await got('http://example.test/foo?bar=baz')
t.is(statusCode, 200)
scope.done()
})
test('match domain and path using regexp with query params and allowUnmocked', t => {
const imgResponse = 'Matched Images Page'
const opts = { allowUnmocked: true }
const scope = nock(/example/, opts)
.get(/imghp\?hl=en/)
.reply(200, imgResponse)
mikealRequest.get('http://example.test/imghp?hl=en', function(
err,
res,
body
) {
scope.done()
t.type(err, 'null')
t.equal(res.statusCode, 200)
t.equal(body, imgResponse)
t.end()
})
})
// https://github.com/nock/nock/issues/490
test('match when query is specified with allowUnmocked', async t => {
const server = http.createServer((request, response) => {
response.write('live')
response.end()
})
t.once('end', () => server.close())
await new Promise(resolve => server.listen(resolve))
const url = `http://localhost:${server.address().port}`
const scope = nock(url, { allowUnmocked: true })
.get('/search')
.query({ q: 'cat pictures' })
.reply(200, '😻')
t.equal((await got(url)).body, 'live')
t.equal((await got(`${url}/search?q=cat%20pictures`)).body, '😻')
scope.done()
})