nock 11.3.6 → 11.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/index.js +2 -0
- package/lib/common.js +33 -9
- package/lib/delayed_body.js +1 -1
- package/lib/intercept.js +41 -40
- package/lib/intercepted_request_router.js +10 -14
- package/lib/interceptor.js +10 -4
- package/lib/match_body.js +2 -5
- package/lib/playback_interceptor.js +9 -10
- package/lib/recorder.js +27 -30
- package/lib/scope.js +3 -2
- package/lib/socket.js +16 -0
- package/package.json +14 -6
- package/types/index.d.ts +2 -0
- package/.all-contributorsrc +0 -123
- package/.eslintignore +0 -3
- package/.eslintrc.yml +0 -34
- package/.github/FUNDING.yml +0 -1
- package/.github/ISSUE_TEMPLATE/01_bug_report.md +0 -26
- package/.github/ISSUE_TEMPLATE/02_feature_request.md +0 -20
- package/.github/ISSUE_TEMPLATE/03_support.md +0 -10
- package/.github/ISSUE_TEMPLATE/04_thanks.md +0 -20
- package/.github/lock.yml +0 -19
- package/.github/stale.yml +0 -25
- package/.github/toc.yml +0 -1
- package/.istanbul.yml +0 -5
- package/.prettierignore +0 -5
- package/.prettierrc.yml +0 -4
- package/.travis.yml +0 -47
- package/CODE_OF_CONDUCT.md +0 -46
- package/CONTRIBUTING.md +0 -164
- package/assets/reply_file_1.txt +0 -1
- package/assets/reply_file_2.txt.gz +0 -0
- package/examples/.eslintrc.yml +0 -3
- package/examples/_log.js +0 -12
- package/examples/delay-connection.js +0 -15
- package/examples/delay-response.js +0 -15
- package/examples/net-connect-default-no-mock.js +0 -18
- package/examples/net-connect-default-other-mock.js +0 -23
- package/examples/net-connect-disabled-different-host.js +0 -26
- package/examples/net-connect-mock-same-host-different-path.js +0 -24
- package/examples/socket-delay-abort.js +0 -19
- package/examples/socket-delay-no-abort.js +0 -15
- package/rfcs/rfc-001.md +0 -43
- package/types/tests.ts +0 -810
- package/types/tsconfig.json +0 -11
- package/types/tslint.json +0 -6
package/types/tests.ts
DELETED
|
@@ -1,810 +0,0 @@
|
|
|
1
|
-
import nock from 'nock'
|
|
2
|
-
import * as fs from 'fs'
|
|
3
|
-
import { URL, URLSearchParams } from 'url'
|
|
4
|
-
|
|
5
|
-
let scope: nock.Scope = nock('http://example.test')
|
|
6
|
-
let inst: nock.Interceptor
|
|
7
|
-
let str = 'foo'
|
|
8
|
-
let strings = ['foo', 'bar']
|
|
9
|
-
let defs: nock.Definition[]
|
|
10
|
-
let options: nock.Options = {}
|
|
11
|
-
|
|
12
|
-
const buffer = Buffer.from('')
|
|
13
|
-
const num = 42
|
|
14
|
-
const obj: { [k: string]: any } = {}
|
|
15
|
-
const objWithUndefinedValue: { a: string; b?: string } = { a: 'a' }
|
|
16
|
-
const regex = /test/
|
|
17
|
-
|
|
18
|
-
scope.head(str) // $ExpectType Interceptor
|
|
19
|
-
|
|
20
|
-
inst = scope.get(str)
|
|
21
|
-
inst = scope.get(str, str)
|
|
22
|
-
inst = scope.get(str, str, options)
|
|
23
|
-
|
|
24
|
-
inst = scope.options(str)
|
|
25
|
-
inst = scope.options(str, str)
|
|
26
|
-
inst = scope.options(str, str, options)
|
|
27
|
-
|
|
28
|
-
inst = scope.patch(str)
|
|
29
|
-
inst = scope.patch(str, str)
|
|
30
|
-
inst = scope.patch(str, obj)
|
|
31
|
-
inst = scope.patch(str, obj, options)
|
|
32
|
-
inst = scope.patch(str, regex)
|
|
33
|
-
|
|
34
|
-
inst = scope.post(str)
|
|
35
|
-
inst = scope.post(str, str)
|
|
36
|
-
inst = scope.post(str, str, options)
|
|
37
|
-
inst = scope.post(str, obj)
|
|
38
|
-
inst = scope.post(str, regex)
|
|
39
|
-
inst = scope.post(str, objWithUndefinedValue)
|
|
40
|
-
inst = scope.post(str, str)
|
|
41
|
-
inst = scope.post(str, strings)
|
|
42
|
-
inst = scope.post(str, [num, str, regex])
|
|
43
|
-
inst = scope.post(str, [num, num, num])
|
|
44
|
-
inst = scope.post(str, regex)
|
|
45
|
-
inst = scope.post(str, buffer)
|
|
46
|
-
inst = scope.post(str, true) // $ExpectError
|
|
47
|
-
inst = scope.post(str, null) // $ExpectError
|
|
48
|
-
inst = scope.post(str, num) // $ExpectError
|
|
49
|
-
|
|
50
|
-
inst = scope.put(str)
|
|
51
|
-
inst = scope.put(str, str)
|
|
52
|
-
inst = scope.put(str, str, options)
|
|
53
|
-
inst = scope.put(str, obj)
|
|
54
|
-
inst = scope.put(str, regex)
|
|
55
|
-
|
|
56
|
-
inst = scope.delete(str)
|
|
57
|
-
inst = scope.delete(str, str)
|
|
58
|
-
inst = scope.delete(str, str, options)
|
|
59
|
-
inst = scope.delete(str, obj)
|
|
60
|
-
inst = scope.delete(str, regex)
|
|
61
|
-
|
|
62
|
-
inst = scope.merge(str)
|
|
63
|
-
inst = scope.merge(str, str)
|
|
64
|
-
inst = scope.merge(str, str, options)
|
|
65
|
-
inst = scope.merge(str, obj)
|
|
66
|
-
inst = scope.merge(str, regex)
|
|
67
|
-
|
|
68
|
-
inst = inst.query(true)
|
|
69
|
-
inst = inst.query(obj)
|
|
70
|
-
inst = inst.query(objWithUndefinedValue)
|
|
71
|
-
inst = inst.query({ foo: regex })
|
|
72
|
-
inst = inst.query(strings) // $ExpectError
|
|
73
|
-
inst = inst.query(buffer) // $ExpectError
|
|
74
|
-
inst = inst.query(regex) // $ExpectError
|
|
75
|
-
|
|
76
|
-
inst = scope.intercept(str, str)
|
|
77
|
-
inst = scope.intercept(str, str, str)
|
|
78
|
-
inst = scope.intercept(str, str, obj)
|
|
79
|
-
inst = scope.intercept(str, str, regex)
|
|
80
|
-
inst = scope.intercept(str, str, str, obj)
|
|
81
|
-
inst = scope.intercept(str, str, obj, obj)
|
|
82
|
-
inst = scope.intercept(str, str, regex, obj)
|
|
83
|
-
|
|
84
|
-
scope = inst.reply()
|
|
85
|
-
scope = inst.reply(num)
|
|
86
|
-
scope = inst.reply(num, str)
|
|
87
|
-
|
|
88
|
-
scope = inst.reply(num, str, obj)
|
|
89
|
-
scope = inst.reply(num, obj, obj)
|
|
90
|
-
scope = inst.reply(num, (uri: string, body: string) => str)
|
|
91
|
-
scope = inst.reply(num, async (uri: string, body: string) => str)
|
|
92
|
-
scope = inst.reply(num, (uri: string, body: string) => str, obj)
|
|
93
|
-
scope = inst.reply((uri: string, body) => [num, str] as const)
|
|
94
|
-
scope = inst.reply(async (uri: string, body) => [num] as const)
|
|
95
|
-
scope = inst.reply((uri: string, body) => [num, str, obj])
|
|
96
|
-
scope = inst.replyWithFile(num, str)
|
|
97
|
-
|
|
98
|
-
inst = inst.times(4)
|
|
99
|
-
inst = inst.once()
|
|
100
|
-
inst = inst.twice()
|
|
101
|
-
inst = inst.thrice()
|
|
102
|
-
|
|
103
|
-
inst = inst.optionally()
|
|
104
|
-
|
|
105
|
-
scope = scope.defaultReplyHeaders({ 'X-Foo': 'bar' })
|
|
106
|
-
|
|
107
|
-
scope = scope.matchHeader(str, str)
|
|
108
|
-
scope = scope.matchHeader(str, regex)
|
|
109
|
-
scope = scope.matchHeader(str, (val: string) => true)
|
|
110
|
-
|
|
111
|
-
inst = inst.delay(num)
|
|
112
|
-
inst = inst.delayConnection(num)
|
|
113
|
-
|
|
114
|
-
scope = scope.filteringPath(regex, str)
|
|
115
|
-
scope = scope.filteringPath((path: string) => {
|
|
116
|
-
return str
|
|
117
|
-
})
|
|
118
|
-
scope = scope.filteringRequestBody(regex, str)
|
|
119
|
-
scope = scope.filteringRequestBody((path: string) => {
|
|
120
|
-
return str
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
scope = scope.log(() => {})
|
|
124
|
-
scope = scope.persist()
|
|
125
|
-
scope = scope.persist(false)
|
|
126
|
-
scope = scope.replyContentLength()
|
|
127
|
-
scope = scope.replyDate()
|
|
128
|
-
scope = scope.replyDate(new Date())
|
|
129
|
-
|
|
130
|
-
inst = inst.delay(2000)
|
|
131
|
-
inst = inst.delay({ head: 1000, body: 1000 })
|
|
132
|
-
inst = inst.delayBody(2000)
|
|
133
|
-
inst = inst.delayConnection(2000)
|
|
134
|
-
inst = inst.socketDelay(2000)
|
|
135
|
-
|
|
136
|
-
scope.done() // $ExpectType void
|
|
137
|
-
scope.isDone() // $ExpectType boolean
|
|
138
|
-
scope.restore() // $ExpectType void
|
|
139
|
-
|
|
140
|
-
nock.recorder.rec()
|
|
141
|
-
nock.recorder.rec(true)
|
|
142
|
-
nock.recorder.rec({
|
|
143
|
-
dont_print: true,
|
|
144
|
-
output_objects: true,
|
|
145
|
-
})
|
|
146
|
-
nock.recorder.clear()
|
|
147
|
-
strings = nock.recorder.play() as string[]
|
|
148
|
-
defs = nock.recorder.play() as nock.Definition[]
|
|
149
|
-
|
|
150
|
-
// Usage
|
|
151
|
-
// $ExpectType Scope
|
|
152
|
-
nock('http://example.test')
|
|
153
|
-
.get('/users/1')
|
|
154
|
-
.reply(200, {
|
|
155
|
-
_id: '123ABC',
|
|
156
|
-
_rev: '946B7D1C',
|
|
157
|
-
username: 'foo',
|
|
158
|
-
email: 'foo.bar@example.test',
|
|
159
|
-
})
|
|
160
|
-
|
|
161
|
-
// Using URL as input
|
|
162
|
-
scope = nock(new URL('https://example.test/'))
|
|
163
|
-
.get('/resource')
|
|
164
|
-
.reply(200, 'url matched')
|
|
165
|
-
|
|
166
|
-
// Specifying hostname
|
|
167
|
-
scope = nock('http://example.test')
|
|
168
|
-
.get('/resource')
|
|
169
|
-
.reply(200, 'domain matched')
|
|
170
|
-
scope = nock('http://example.test')
|
|
171
|
-
scope = nock(/example\.com/)
|
|
172
|
-
.get('/resource')
|
|
173
|
-
.reply(200, 'domain regex matched')
|
|
174
|
-
|
|
175
|
-
// Specifying path
|
|
176
|
-
scope = nock('http://example.test')
|
|
177
|
-
.get('/resource')
|
|
178
|
-
.reply(200, 'path matched')
|
|
179
|
-
|
|
180
|
-
scope = nock('http://example.test')
|
|
181
|
-
.get(/source$/)
|
|
182
|
-
.reply(200, 'path using regex matched')
|
|
183
|
-
|
|
184
|
-
scope = nock('http://example.test')
|
|
185
|
-
.get(uri => {
|
|
186
|
-
return uri.indexOf('cats') >= 0
|
|
187
|
-
})
|
|
188
|
-
.reply(200, 'path using function matched')
|
|
189
|
-
|
|
190
|
-
// Specifying request body
|
|
191
|
-
scope = nock('http://example.test')
|
|
192
|
-
.post('/users', {
|
|
193
|
-
username: 'foo',
|
|
194
|
-
email: 'foo.bar@example.test',
|
|
195
|
-
})
|
|
196
|
-
.reply(201, {
|
|
197
|
-
ok: true,
|
|
198
|
-
id: '123ABC',
|
|
199
|
-
rev: '946B7D1C',
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
nock('https://example.test')
|
|
203
|
-
.post('/path', {
|
|
204
|
-
number: 1,
|
|
205
|
-
bool: false,
|
|
206
|
-
empty: null,
|
|
207
|
-
array: ['foo', 2, true, null, { number: 3 }],
|
|
208
|
-
})
|
|
209
|
-
.reply(200)
|
|
210
|
-
|
|
211
|
-
scope = nock('http://example.test')
|
|
212
|
-
.post('/users', /email=.?@example.test/gi)
|
|
213
|
-
.reply(201, {
|
|
214
|
-
ok: true,
|
|
215
|
-
id: '123ABC',
|
|
216
|
-
rev: '946B7D1C',
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
scope = nock('http://example.test')
|
|
220
|
-
.post('/users', {
|
|
221
|
-
username: 'foo',
|
|
222
|
-
password: /a.+/,
|
|
223
|
-
email: 'foo.bar@example.test',
|
|
224
|
-
})
|
|
225
|
-
.reply(201, {
|
|
226
|
-
ok: true,
|
|
227
|
-
id: '123ABC',
|
|
228
|
-
rev: '946B7D1C',
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
scope = nock('http://example.test')
|
|
232
|
-
.post('/users', body => {
|
|
233
|
-
return body.id === '123ABC'
|
|
234
|
-
})
|
|
235
|
-
.reply(201, {
|
|
236
|
-
ok: true,
|
|
237
|
-
id: '123ABC',
|
|
238
|
-
rev: '946B7D1C',
|
|
239
|
-
})
|
|
240
|
-
|
|
241
|
-
// Specifying request query string
|
|
242
|
-
nock('http://example.test')
|
|
243
|
-
.get('/users')
|
|
244
|
-
.query({ name: 'pedro', surname: 'teixeira' })
|
|
245
|
-
.reply(200, { results: [{ id: 'foo' }] })
|
|
246
|
-
|
|
247
|
-
nock('http://example.test')
|
|
248
|
-
.get('/users')
|
|
249
|
-
.query({
|
|
250
|
-
names: ['alice', 'bob'],
|
|
251
|
-
tags: {
|
|
252
|
-
alice: ['admin', 'tester'],
|
|
253
|
-
bob: ['tester'],
|
|
254
|
-
},
|
|
255
|
-
})
|
|
256
|
-
.reply(200, { results: [{ id: 'foo' }] })
|
|
257
|
-
|
|
258
|
-
nock('http://example.test')
|
|
259
|
-
.get('/users')
|
|
260
|
-
.query(actualQueryObject => {
|
|
261
|
-
// do some compare with the actual Query Object
|
|
262
|
-
// return true for matched
|
|
263
|
-
// return false for not matched
|
|
264
|
-
return true
|
|
265
|
-
})
|
|
266
|
-
.reply(200, { results: [{ id: 'foo' }] })
|
|
267
|
-
|
|
268
|
-
nock('http://example.test')
|
|
269
|
-
.get('/users')
|
|
270
|
-
.query(true)
|
|
271
|
-
.reply(200, { results: [{ id: 'foo' }] })
|
|
272
|
-
|
|
273
|
-
nock('http://example.test', { encodedQueryParams: true })
|
|
274
|
-
.get('/users')
|
|
275
|
-
.query('foo%5Bbar%5D%3Dhello%20world%21')
|
|
276
|
-
.reply(200, { results: [{ id: 'foo' }] })
|
|
277
|
-
|
|
278
|
-
nock('http://example.test')
|
|
279
|
-
.get('/')
|
|
280
|
-
.query(new URLSearchParams([['foo', 'one'], ['foo', 'two']]))
|
|
281
|
-
.reply()
|
|
282
|
-
|
|
283
|
-
// Specifying replies
|
|
284
|
-
scope = nock('http://example.test')
|
|
285
|
-
.get('/users/1')
|
|
286
|
-
.reply(404)
|
|
287
|
-
|
|
288
|
-
scope = nock('http://example.test')
|
|
289
|
-
.get('/')
|
|
290
|
-
.reply(200, 'Hello from Google!')
|
|
291
|
-
|
|
292
|
-
scope = nock('http://example.test')
|
|
293
|
-
.get('/')
|
|
294
|
-
.reply(200, {
|
|
295
|
-
username: 'foo',
|
|
296
|
-
email: 'foo.bar@example.test',
|
|
297
|
-
_id: '4324243fsd',
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
scope = nock('http://example.test')
|
|
301
|
-
.get('/resource')
|
|
302
|
-
.reply(async () => [500, ''] as const)
|
|
303
|
-
|
|
304
|
-
scope = nock('http://example.test')
|
|
305
|
-
.get('/resource')
|
|
306
|
-
.reply(() => Promise.resolve([500, '']))
|
|
307
|
-
|
|
308
|
-
scope = nock('http://example.test')
|
|
309
|
-
.get('/')
|
|
310
|
-
.replyWithFile(200, __dirname + '/replies/user.json')
|
|
311
|
-
|
|
312
|
-
scope = nock('http://example.test')
|
|
313
|
-
.filteringRequestBody(/.*/, '*')
|
|
314
|
-
.post('/echo', '*')
|
|
315
|
-
.reply(201, (uri: string, requestBody) => {
|
|
316
|
-
return requestBody
|
|
317
|
-
})
|
|
318
|
-
|
|
319
|
-
scope = nock('http://example.test')
|
|
320
|
-
.filteringRequestBody(/.*/, '*')
|
|
321
|
-
.post('/echo', '*')
|
|
322
|
-
.reply((uri, requestBody, cb) => {
|
|
323
|
-
fs.readFile('cat-poems.txt', cb as any) // Error-first callback
|
|
324
|
-
})
|
|
325
|
-
|
|
326
|
-
scope = nock('http://example.test')
|
|
327
|
-
.filteringRequestBody(/.*/, '*')
|
|
328
|
-
.post('/echo', '*')
|
|
329
|
-
.reply((uri, requestBody) => {
|
|
330
|
-
str = uri
|
|
331
|
-
return [
|
|
332
|
-
201,
|
|
333
|
-
'THIS IS THE REPLY BODY',
|
|
334
|
-
{ header: 'value' }, // optional headers
|
|
335
|
-
]
|
|
336
|
-
})
|
|
337
|
-
|
|
338
|
-
scope = nock('http://example.test')
|
|
339
|
-
.filteringRequestBody(/.*/, '*')
|
|
340
|
-
.post('/echo', '*')
|
|
341
|
-
.reply((uri, requestBody, cb) => {
|
|
342
|
-
setTimeout(() => {
|
|
343
|
-
cb(null, [201, 'THIS IS THE REPLY BODY'])
|
|
344
|
-
}, 1e3)
|
|
345
|
-
})
|
|
346
|
-
|
|
347
|
-
scope = nock('http://example.test')
|
|
348
|
-
.get('/cat-poems')
|
|
349
|
-
.reply(200, (uri: string, requestBody) => {
|
|
350
|
-
return fs.createReadStream('cat-poems.txt')
|
|
351
|
-
})
|
|
352
|
-
|
|
353
|
-
/// Access original request and headers
|
|
354
|
-
scope = nock('http://example.test')
|
|
355
|
-
.get('/cat-poems')
|
|
356
|
-
.reply(function(uri, requestBody) {
|
|
357
|
-
str = this.req.path
|
|
358
|
-
console.log('path:', this.req.path)
|
|
359
|
-
console.log('headers:', this.req.headers)
|
|
360
|
-
// ...
|
|
361
|
-
})
|
|
362
|
-
|
|
363
|
-
// Replying with errors
|
|
364
|
-
nock('http://example.test')
|
|
365
|
-
.get('/cat-poems')
|
|
366
|
-
.replyWithError('something awful happened')
|
|
367
|
-
|
|
368
|
-
nock('http://example.test')
|
|
369
|
-
.get('/cat-poems')
|
|
370
|
-
.replyWithError({ message: 'something awful happened', code: 'AWFUL_ERROR' })
|
|
371
|
-
|
|
372
|
-
nock('http://example.test')
|
|
373
|
-
.get('/cat-poems')
|
|
374
|
-
.replyWithError(Error('something awful happened'))
|
|
375
|
-
|
|
376
|
-
// Specifying headers
|
|
377
|
-
|
|
378
|
-
/// Specifying Request Headers
|
|
379
|
-
scope = nock('http://example.test', {
|
|
380
|
-
reqheaders: {
|
|
381
|
-
authorization: 'Basic Auth',
|
|
382
|
-
},
|
|
383
|
-
})
|
|
384
|
-
.get('/')
|
|
385
|
-
.reply(200)
|
|
386
|
-
|
|
387
|
-
scope = nock('http://example.test', {
|
|
388
|
-
reqheaders: {
|
|
389
|
-
'X-My-Headers': headerValue => {
|
|
390
|
-
if (headerValue) {
|
|
391
|
-
return true
|
|
392
|
-
}
|
|
393
|
-
return false
|
|
394
|
-
},
|
|
395
|
-
'X-My-Awesome-Header': /Awesome/i,
|
|
396
|
-
},
|
|
397
|
-
})
|
|
398
|
-
.get('/')
|
|
399
|
-
.reply(200)
|
|
400
|
-
|
|
401
|
-
scope = nock('http://example.test', {
|
|
402
|
-
badheaders: ['cookie', 'x-forwarded-for'],
|
|
403
|
-
})
|
|
404
|
-
.get('/')
|
|
405
|
-
.reply(200)
|
|
406
|
-
|
|
407
|
-
scope = nock('http://example.test')
|
|
408
|
-
.get('/')
|
|
409
|
-
.basicAuth({
|
|
410
|
-
user: 'john',
|
|
411
|
-
pass: 'doe',
|
|
412
|
-
})
|
|
413
|
-
.reply(200)
|
|
414
|
-
|
|
415
|
-
/// Specifying Reply Headers
|
|
416
|
-
scope = nock('http://example.test')
|
|
417
|
-
.get('/')
|
|
418
|
-
.reply(200, 'Hello World!', {
|
|
419
|
-
'X-My-Headers': 'My Header value',
|
|
420
|
-
})
|
|
421
|
-
|
|
422
|
-
scope = nock('http://example.test')
|
|
423
|
-
.get('/')
|
|
424
|
-
.reply(200, 'Hello World!', {
|
|
425
|
-
'X-My-Headers': ['My Header value 1', 'My Header value 2'],
|
|
426
|
-
})
|
|
427
|
-
|
|
428
|
-
scope = nock('http://example.test')
|
|
429
|
-
.get('/')
|
|
430
|
-
.reply(200, 'Hello World!', new Map([['X-Header-One', 'foo']]))
|
|
431
|
-
|
|
432
|
-
scope = nock('http://example.test')
|
|
433
|
-
.get('/')
|
|
434
|
-
.reply(200, 'Hello World!', {
|
|
435
|
-
'X-My-Headers': (req, res, body) => {
|
|
436
|
-
return body.toString()
|
|
437
|
-
},
|
|
438
|
-
})
|
|
439
|
-
|
|
440
|
-
// Default Reply Headers
|
|
441
|
-
scope = nock('http://example.test')
|
|
442
|
-
.defaultReplyHeaders({
|
|
443
|
-
'X-Powered-By': 'Rails',
|
|
444
|
-
'Content-Type': 'application/json',
|
|
445
|
-
})
|
|
446
|
-
.get('/')
|
|
447
|
-
.reply(200, 'The default headers should come too')
|
|
448
|
-
|
|
449
|
-
scope = nock('http://example.test')
|
|
450
|
-
.defaultReplyHeaders({
|
|
451
|
-
'Content-Length': (req, res, body) => {
|
|
452
|
-
return body.length.toString()
|
|
453
|
-
},
|
|
454
|
-
})
|
|
455
|
-
.get('/')
|
|
456
|
-
.reply(200, 'The default headers should come too')
|
|
457
|
-
|
|
458
|
-
// Including Content-Length Header Automatically
|
|
459
|
-
scope = nock('http://example.test')
|
|
460
|
-
.replyContentLength()
|
|
461
|
-
.get('/')
|
|
462
|
-
.reply(200, { hello: 'world' })
|
|
463
|
-
|
|
464
|
-
// Including Date Header Automatically
|
|
465
|
-
scope = nock('http://example.test')
|
|
466
|
-
.replyDate(new Date(2015, 0, 1)) // defaults to now, must use a Date object
|
|
467
|
-
.get('/')
|
|
468
|
-
.reply(200, { hello: 'world' })
|
|
469
|
-
|
|
470
|
-
// HTTP Verbs
|
|
471
|
-
nock('http://example.test')
|
|
472
|
-
.intercept('/path', 'PATCH')
|
|
473
|
-
.reply(304)
|
|
474
|
-
|
|
475
|
-
// Support for HTTP and HTTPS
|
|
476
|
-
scope = nock('https://secure.example.test')
|
|
477
|
-
|
|
478
|
-
// Non-standard ports
|
|
479
|
-
scope = nock('http://example.test:8081')
|
|
480
|
-
|
|
481
|
-
// Repeat response n times
|
|
482
|
-
nock('http://example.test')
|
|
483
|
-
.get('/')
|
|
484
|
-
.times(4)
|
|
485
|
-
.reply(200, 'Ok')
|
|
486
|
-
nock('http://example.test')
|
|
487
|
-
.get('/')
|
|
488
|
-
.once()
|
|
489
|
-
.reply(200, 'Ok')
|
|
490
|
-
nock('http://example.test')
|
|
491
|
-
.get('/')
|
|
492
|
-
.twice()
|
|
493
|
-
.reply(200, 'Ok')
|
|
494
|
-
nock('http://example.test')
|
|
495
|
-
.get('/')
|
|
496
|
-
.thrice()
|
|
497
|
-
.reply(200, 'Ok')
|
|
498
|
-
|
|
499
|
-
// Make responding optional
|
|
500
|
-
nock('http://example.test')
|
|
501
|
-
.get('/')
|
|
502
|
-
.optionally()
|
|
503
|
-
.reply(200, 'Ok')
|
|
504
|
-
|
|
505
|
-
// Delay the response body
|
|
506
|
-
nock('http://example.test')
|
|
507
|
-
.get('/')
|
|
508
|
-
.delayBody(2000) // 2 seconds
|
|
509
|
-
.reply(200, '<html></html>')
|
|
510
|
-
|
|
511
|
-
// Delay the response
|
|
512
|
-
nock('http://example.test')
|
|
513
|
-
.get('/')
|
|
514
|
-
.delay(2000) // 2 seconds delay will be applied to the response header.
|
|
515
|
-
.reply(200, '<html></html>')
|
|
516
|
-
|
|
517
|
-
nock('http://example.test')
|
|
518
|
-
.get('/')
|
|
519
|
-
.delay({
|
|
520
|
-
head: 2000, // header will be delayed for 2 seconds, i.e. the whole response will be delayed for 2 seconds.
|
|
521
|
-
body: 3000, // body will be delayed for another 3 seconds after header is sent out.
|
|
522
|
-
})
|
|
523
|
-
.reply(200, '<html></html>')
|
|
524
|
-
|
|
525
|
-
// Delay the connection
|
|
526
|
-
nock('http://example.test')
|
|
527
|
-
.get('/')
|
|
528
|
-
.socketDelay(2000) // 2 seconds
|
|
529
|
-
.delayConnection(1000)
|
|
530
|
-
.reply(200, '<html></html>')
|
|
531
|
-
|
|
532
|
-
// Chaining
|
|
533
|
-
scope = nock('http://example.test')
|
|
534
|
-
.get('/users/1')
|
|
535
|
-
.reply(404)
|
|
536
|
-
.post('/users', {
|
|
537
|
-
username: 'foo',
|
|
538
|
-
email: 'foo.bar@example.test',
|
|
539
|
-
})
|
|
540
|
-
.reply(201, {
|
|
541
|
-
ok: true,
|
|
542
|
-
id: '123ABC',
|
|
543
|
-
rev: '946B7D1C',
|
|
544
|
-
})
|
|
545
|
-
.get('/users/123ABC')
|
|
546
|
-
.reply(200, {
|
|
547
|
-
_id: '123ABC',
|
|
548
|
-
_rev: '946B7D1C',
|
|
549
|
-
username: 'foo',
|
|
550
|
-
email: 'foo.bar@example.test',
|
|
551
|
-
})
|
|
552
|
-
|
|
553
|
-
// Scope filtering
|
|
554
|
-
scope = nock('https://api.example.test', {
|
|
555
|
-
filteringScope: (scope: string) => {
|
|
556
|
-
return /^https:\/\/api[0-9]*.example.test/.test(scope)
|
|
557
|
-
},
|
|
558
|
-
})
|
|
559
|
-
.get('/1/metadata/auto/Photos?include_deleted=false&list=true')
|
|
560
|
-
.reply(200)
|
|
561
|
-
|
|
562
|
-
// Path filtering
|
|
563
|
-
scope = nock('http://example.test')
|
|
564
|
-
.filteringPath(/password=[^&]*/g, 'password=XXX')
|
|
565
|
-
.get('/users/1?password=XXX')
|
|
566
|
-
.reply(200, 'user')
|
|
567
|
-
|
|
568
|
-
scope = nock('http://example.test')
|
|
569
|
-
.filteringPath(path => {
|
|
570
|
-
return '/ABC'
|
|
571
|
-
})
|
|
572
|
-
.get('/ABC')
|
|
573
|
-
.reply(200, 'user')
|
|
574
|
-
|
|
575
|
-
// Request Body filtering
|
|
576
|
-
scope = nock('http://example.test')
|
|
577
|
-
.filteringRequestBody(/password=[^&]*/g, 'password=XXX')
|
|
578
|
-
.post('/users/1', 'data=ABC&password=XXX')
|
|
579
|
-
.reply(201, 'OK')
|
|
580
|
-
|
|
581
|
-
scope = nock('http://example.test')
|
|
582
|
-
.filteringRequestBody(body => {
|
|
583
|
-
return 'ABC'
|
|
584
|
-
})
|
|
585
|
-
.post('/', 'ABC')
|
|
586
|
-
.reply(201, 'OK')
|
|
587
|
-
|
|
588
|
-
// Request Headers Matching on the Scope Level
|
|
589
|
-
scope = nock('http://example.test')
|
|
590
|
-
.matchHeader('accept', 'application/json')
|
|
591
|
-
.get('/')
|
|
592
|
-
.reply(200, {
|
|
593
|
-
data: 'hello world',
|
|
594
|
-
})
|
|
595
|
-
|
|
596
|
-
scope = nock('http://example.test')
|
|
597
|
-
.matchHeader('User-Agent', /Mozilla\/.*/)
|
|
598
|
-
.get('/')
|
|
599
|
-
.reply(200, {
|
|
600
|
-
data: 'hello world',
|
|
601
|
-
})
|
|
602
|
-
|
|
603
|
-
scope = nock('http://example.test')
|
|
604
|
-
.matchHeader('content-length', val => {
|
|
605
|
-
return Number(val) >= 1000
|
|
606
|
-
})
|
|
607
|
-
.get('/')
|
|
608
|
-
.reply(200, {
|
|
609
|
-
data: 'hello world',
|
|
610
|
-
})
|
|
611
|
-
|
|
612
|
-
// Request Headers Matching on the Interceptor Level
|
|
613
|
-
scope = nock('http://example.test')
|
|
614
|
-
.get('/')
|
|
615
|
-
.matchHeader('accept', 'application/json')
|
|
616
|
-
.reply(200, {
|
|
617
|
-
data: 'hello world',
|
|
618
|
-
})
|
|
619
|
-
|
|
620
|
-
scope = nock('http://example.test')
|
|
621
|
-
.get('/')
|
|
622
|
-
.matchHeader('User-Agent', /Mozilla\/.*/)
|
|
623
|
-
.reply(200, {
|
|
624
|
-
data: 'hello world',
|
|
625
|
-
})
|
|
626
|
-
|
|
627
|
-
scope = nock('http://example.test')
|
|
628
|
-
.get('/')
|
|
629
|
-
.matchHeader('content-length', val => {
|
|
630
|
-
return Number(val) >= 1000
|
|
631
|
-
})
|
|
632
|
-
.reply(200, {
|
|
633
|
-
data: 'hello world',
|
|
634
|
-
})
|
|
635
|
-
|
|
636
|
-
// Allow unmocked requests on a mocked hostname
|
|
637
|
-
options = { allowUnmocked: true }
|
|
638
|
-
scope = nock('http://example.test', options)
|
|
639
|
-
.get('/my/url')
|
|
640
|
-
.reply(200, 'OK!')
|
|
641
|
-
|
|
642
|
-
// Expectations
|
|
643
|
-
let google = nock('http://example.test')
|
|
644
|
-
.get('/')
|
|
645
|
-
.reply(200, 'Hello from Google!')
|
|
646
|
-
setTimeout(() => {
|
|
647
|
-
google.done() // will throw an assertion error if meanwhile a "GET http://example.test" was not performed.
|
|
648
|
-
}, 5000)
|
|
649
|
-
|
|
650
|
-
/// .isDone()
|
|
651
|
-
scope = nock('http://example.test')
|
|
652
|
-
.get('/')
|
|
653
|
-
.reply(200)
|
|
654
|
-
scope.isDone() // will return false
|
|
655
|
-
|
|
656
|
-
nock.isDone()
|
|
657
|
-
|
|
658
|
-
/// .cleanAll()
|
|
659
|
-
nock.cleanAll()
|
|
660
|
-
|
|
661
|
-
/// .persist()
|
|
662
|
-
scope = nock('http://example.test')
|
|
663
|
-
.persist()
|
|
664
|
-
.get('/')
|
|
665
|
-
.reply(200, 'Persisting all the way')
|
|
666
|
-
|
|
667
|
-
/// .pendingMocks()
|
|
668
|
-
strings = scope.pendingMocks()
|
|
669
|
-
strings = nock.pendingMocks()
|
|
670
|
-
if (!scope.isDone()) {
|
|
671
|
-
console.error('pending mocks: %j', scope.pendingMocks())
|
|
672
|
-
}
|
|
673
|
-
console.error('pending mocks: %j', nock.pendingMocks())
|
|
674
|
-
|
|
675
|
-
/// .activeMocks()
|
|
676
|
-
nock.activeMocks() // $ExpectType string[]
|
|
677
|
-
nock('http://example.test').activeMocks() // $ExpectType string[]
|
|
678
|
-
|
|
679
|
-
// Logging
|
|
680
|
-
google = nock('http://example.test').log(console.log)
|
|
681
|
-
|
|
682
|
-
// Restoring
|
|
683
|
-
nock.restore()
|
|
684
|
-
|
|
685
|
-
// Enable/Disable real HTTP request
|
|
686
|
-
nock.disableNetConnect()
|
|
687
|
-
nock.enableNetConnect()
|
|
688
|
-
|
|
689
|
-
// using a string
|
|
690
|
-
nock.enableNetConnect('example.test')
|
|
691
|
-
|
|
692
|
-
// or a RegExp
|
|
693
|
-
nock.enableNetConnect(/example\.(com|test)/)
|
|
694
|
-
|
|
695
|
-
nock.disableNetConnect()
|
|
696
|
-
nock.enableNetConnect('127.0.0.1') // Allow localhost connections so we can test local routes and mock servers.
|
|
697
|
-
|
|
698
|
-
nock.cleanAll()
|
|
699
|
-
nock.enableNetConnect()
|
|
700
|
-
|
|
701
|
-
// Recording
|
|
702
|
-
nock.recorder.rec()
|
|
703
|
-
|
|
704
|
-
/// dont_print option
|
|
705
|
-
nock.recorder.rec({
|
|
706
|
-
dont_print: true,
|
|
707
|
-
})
|
|
708
|
-
// ... some HTTP calls
|
|
709
|
-
const nockCalls = nock.recorder.play()
|
|
710
|
-
|
|
711
|
-
/// output_objects option
|
|
712
|
-
nock.recorder.rec({
|
|
713
|
-
output_objects: true,
|
|
714
|
-
})
|
|
715
|
-
// ... some HTTP calls
|
|
716
|
-
const nockCallObjects = nock.recorder.play()
|
|
717
|
-
|
|
718
|
-
let nocks = nock.load(str)
|
|
719
|
-
nocks.forEach(nock => {
|
|
720
|
-
nock = nock.filteringRequestBody((body: string) => {
|
|
721
|
-
return body
|
|
722
|
-
})
|
|
723
|
-
})
|
|
724
|
-
|
|
725
|
-
// Pre-process the nock definitions as scope filtering has to be defined before the nocks are defined (due to its very hacky nature).
|
|
726
|
-
const nockDefs = nock.loadDefs(str)
|
|
727
|
-
nockDefs.forEach(def => {
|
|
728
|
-
// Do something with the definition object e.g. scope filtering.
|
|
729
|
-
def.options = def.options || {}
|
|
730
|
-
def.options.filteringScope = (scope: string) => {
|
|
731
|
-
return /^https:\/\/api[0-9]*.example.test/.test(scope)
|
|
732
|
-
}
|
|
733
|
-
})
|
|
734
|
-
// Load the nocks from pre-processed definitions.
|
|
735
|
-
nocks = nock.define(nockDefs)
|
|
736
|
-
|
|
737
|
-
/// enable_reqheaders_recording option
|
|
738
|
-
nock.recorder.rec({
|
|
739
|
-
dont_print: true,
|
|
740
|
-
output_objects: true,
|
|
741
|
-
enable_reqheaders_recording: true,
|
|
742
|
-
})
|
|
743
|
-
|
|
744
|
-
/// logging option
|
|
745
|
-
const nullAppender = (content: string) => {}
|
|
746
|
-
nock.recorder.rec({
|
|
747
|
-
logging: nullAppender,
|
|
748
|
-
})
|
|
749
|
-
|
|
750
|
-
/// use_separator option
|
|
751
|
-
nock.recorder.rec({
|
|
752
|
-
use_separator: false,
|
|
753
|
-
})
|
|
754
|
-
|
|
755
|
-
// .removeInterceptor()
|
|
756
|
-
nock.removeInterceptor({
|
|
757
|
-
hostname: 'localhost',
|
|
758
|
-
path: '/mockedResource',
|
|
759
|
-
})
|
|
760
|
-
nock.removeInterceptor({
|
|
761
|
-
hostname: 'localhost',
|
|
762
|
-
path: '/login',
|
|
763
|
-
method: 'POST',
|
|
764
|
-
proto: 'https',
|
|
765
|
-
})
|
|
766
|
-
|
|
767
|
-
const interceptor = nock('http://example.test').get('somePath')
|
|
768
|
-
nock.removeInterceptor(interceptor)
|
|
769
|
-
|
|
770
|
-
// Events
|
|
771
|
-
/// Global no match event
|
|
772
|
-
nock.emitter.on('no match', (req: any) => {})
|
|
773
|
-
|
|
774
|
-
// Nock Back
|
|
775
|
-
/// Setup
|
|
776
|
-
nock.back.fixtures = '/path/to/fixtures/'
|
|
777
|
-
nock.back.setMode('record')
|
|
778
|
-
|
|
779
|
-
/// Usage
|
|
780
|
-
const before = (def: nock.Definition) => {
|
|
781
|
-
def.options = def.options || {}
|
|
782
|
-
def.options.filteringScope = (scope: string) => {
|
|
783
|
-
return /^https:\/\/api[0-9]*.example.test/.test(scope)
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
const after = (scope: nock.Scope) => {
|
|
787
|
-
scope = scope.filteringRequestBody((body: string): string => {
|
|
788
|
-
return body
|
|
789
|
-
})
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
// recording of the fixture
|
|
793
|
-
declare var request: any
|
|
794
|
-
nock.back('fixture.json', { before, after }, (nockDone: () => void) => {
|
|
795
|
-
request.get('http://example.test', (err: any, res: any, body: string) => {
|
|
796
|
-
nockDone()
|
|
797
|
-
// usage of the created fixture
|
|
798
|
-
nock.back('fixture.json', (nockDone: () => void) => {
|
|
799
|
-
nockDone() // never gets here
|
|
800
|
-
})
|
|
801
|
-
})
|
|
802
|
-
})
|
|
803
|
-
|
|
804
|
-
// in promise mode
|
|
805
|
-
nock.back('promisedFixture.json').then(({ nockDone, context }) => {
|
|
806
|
-
context.assertScopesFinished()
|
|
807
|
-
|
|
808
|
-
// do your tests returning a promise and chain it with
|
|
809
|
-
Promise.resolve('foo').then(nockDone)
|
|
810
|
-
})
|