/
test_request_overrider.js
829 lines (692 loc) · 22.1 KB
/
test_request_overrider.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
'use strict'
// Tests of the RequestOverrider, which mocks http.ClientRequest and
// https.ClientRequest. The goal is to provide parity of behavior, both
// documented and undocumented, with the real version.
//
// While most of nock's tests are functional tests which invoke Nock's public
// API and make assertions about requests, usually with got, the tests of the
// request overrider tend to use http directly, and make lower-level
// assertions about how the mock client responds. Here the code under test is
// the part of Nock that must interface with all http clients.
const http = require('http')
const https = require('https')
const { URL } = require('url')
const { expect } = require('chai')
const sinon = require('sinon')
const nock = require('..')
const FormData = require('form-data')
const got = require('./got_client')
const servers = require('./servers')
describe('Request Overrider', () => {
it('response is an http.IncomingMessage instance', done => {
const responseText = 'incoming message!'
const scope = nock('http://example.test')
.get('/somepath')
.reply(200, responseText)
http
.request(
{
host: 'example.test',
path: '/somepath',
},
res => {
res.resume()
expect(res).to.be.an.instanceof(http.IncomingMessage)
scope.done()
done()
}
)
.end()
})
it('emits the response event', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.get('http://example.test')
req.on('response', () => {
scope.done()
done()
})
})
it('write callback called', done => {
const scope = nock('http://example.test')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply()
const reqWriteCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
},
res => {
expect(reqWriteCallback).to.have.been.calledOnce()
expect(res.statusCode).to.equal(200)
res.on('end', () => {
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.write('mamma mia', null, () => {
reqWriteCallback()
req.end()
})
})
it('write callback is not called if the provided chunk is undefined', done => {
const scope = nock('http://example.test').post('/').reply()
const reqWriteCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
},
res => {
expect(res.statusCode).to.equal(200)
res.on('end', () => {
expect(reqWriteCallback).to.not.have.been.called()
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.write(undefined, null, reqWriteCallback)
req.end()
})
it('end callback called', done => {
const scope = nock('http://example.test')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply()
const reqEndCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
},
res => {
expect(reqEndCallback).to.have.been.calledOnce()
expect(res.statusCode).to.equal(200)
res.on('end', () => {
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.end('mamma mia', null, reqEndCallback)
})
// https://github.com/nock/nock/issues/1509
it('end callback called when end has callback, but no buffer', done => {
const scope = nock('http://example.test').post('/').reply()
const reqEndCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
},
res => {
expect(reqEndCallback).to.have.been.calledOnce()
expect(res.statusCode).to.equal(200)
res.on('end', () => {
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.end(reqEndCallback)
})
it('request.end called with all three arguments', done => {
const scope = nock('http://example.test').post('/', 'foobar').reply()
const reqEndCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
},
res => {
expect(reqEndCallback).to.have.been.calledOnce()
res.on('end', () => {
scope.done()
done()
})
res.resume()
}
)
// hex(foobar) == 666F6F626172
req.end('666F6F626172', 'hex', reqEndCallback)
})
it('request.end called with only data and encoding', done => {
const scope = nock('http://example.test').post('/', 'foobar').reply()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
},
res => {
res.on('end', () => {
scope.done()
done()
})
res.resume()
}
)
// hex(foobar) == 666F6F626172
req.end('666F6F626172', 'hex')
})
it('request.end called with only data and a callback', done => {
const scope = nock('http://example.test').post('/', 'foobar').reply()
const reqEndCallback = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
},
res => {
expect(reqEndCallback).to.have.been.calledOnce()
res.on('end', () => {
scope.done()
done()
})
res.resume()
}
)
req.end('foobar', reqEndCallback)
})
// https://github.com/nock/nock/issues/2112
it('request.end can be called multiple times without a chunk and not error', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request(
{
host: 'example.test',
method: 'GET',
path: '/',
},
res => {
res.on('end', () => {
scope.done()
done()
})
res.resume()
}
)
req.end()
req.end()
req.end()
})
it('should emit an error if `write` is called after `end`', done => {
nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
req.on('error', err => {
expect(err.message).to.equal('write after end')
expect(err.code).to.equal('ERR_STREAM_WRITE_AFTER_END')
done()
})
req.end()
req.write('foo')
})
// http://github.com/nock/nock/issues/139
it('should emit "finish" on the request before emitting "end" on the response', done => {
const scope = nock('http://example.test').post('/').reply()
const onFinish = sinon.spy()
const req = http.request(
{
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
},
res => {
expect(onFinish).to.have.been.calledOnce()
expect(res.statusCode).to.equal(200)
res.on('end', () => {
expect(onFinish).to.have.been.calledOnce()
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.on('finish', onFinish)
req.end('mamma mia')
})
it('should update the writable attributes before emitting the "finish" event', done => {
nock('http://example.test').post('/').reply()
const req = http.request({
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
})
// `writableEnded` was added in v12.9.0 to rename `finished` which was deprecated in v13.4.0. it's just an alias,
// but it only denotes that `end` was called on the request not that the socket has finished flushing (hence the rename).
expect(req.finished).to.be.false()
const hasWriteable = 'writableEnded' in req // to support v10
expect(req.writableEnded).to.equal(hasWriteable ? false : undefined)
// `writableFinished` denotes all data has been flushed to the underlying system, immediately before
// the 'finish' event is emitted. Nock's "socket" is instantaneous so these attributes never differ.
expect(req.writableFinished).to.equal(hasWriteable ? false : undefined)
req.on('finish', () => {
expect(req.finished).to.be.true()
expect(req.writableEnded).to.equal(hasWriteable ? true : undefined)
expect(req.writableFinished).to.equal(hasWriteable ? true : undefined)
done()
})
req.end('mamma mia')
})
// TODO Convert to async / got.
it('pause response before data', done => {
const scope = nock('http://example.test')
.get('/pauser')
.reply(200, 'nobody')
const req = http.request({
host: 'example.test',
path: '/pauser',
})
const didTimeout = sinon.spy()
const onData = sinon.spy()
req.on('response', res => {
res.pause()
setTimeout(() => {
didTimeout()
res.resume()
}, 500)
res.on('data', () => {
onData()
expect(didTimeout).to.have.been.calledOnce()
})
res.on('end', () => {
expect(onData).to.have.been.calledOnce()
scope.done()
done()
})
})
req.end()
})
it('accept URL as request target', done => {
const onData = sinon.spy()
const scope = nock('http://example.test')
.get('/')
.reply(200, 'Hello World!')
http.get(new URL('http://example.test'), res => {
expect(res.statusCode).to.equal(200)
res.on('data', data => {
onData()
expect(data).to.be.an.instanceof(Buffer)
expect(data.toString()).to.equal('Hello World!')
})
res.on('end', () => {
expect(onData).to.have.been.calledOnce()
scope.done()
done()
})
})
})
it('request has path', done => {
const scope = nock('http://example.test')
.get('/the/path/to/infinity')
.reply(200)
const req = http.request(
{
hostname: 'example.test',
port: 80,
method: 'GET',
path: '/the/path/to/infinity',
},
res => {
scope.done()
expect(req.path).to.equal('/the/path/to/infinity')
done()
}
)
req.end()
})
it('has a req property on the response', done => {
const scope = nock('http://example.test').get('/like-wtf').reply(200)
const req = http.request('http://example.test/like-wtf', res => {
res.on('end', () => {
expect(res.req).to.be.an.instanceof(http.ClientRequest)
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
})
req.end()
})
// Hopefully address https://github.com/nock/nock/issues/146, at least in spirit.
it('request with a large buffer', async () => {
const replyLength = 1024 * 1024
const responseBody = Buffer.from(new Array(replyLength + 1).join('.'))
// Confidence check.
expect(responseBody.length).to.equal(replyLength)
const scope = nock('http://example.test')
.get('/')
.reply(200, responseBody, { 'Content-Encoding': 'gzip' })
const { body } = await got('http://example.test', {
responseType: 'buffer',
decompress: false,
})
expect(body).to.deep.equal(responseBody)
scope.done()
})
it('.setNoDelay', done => {
nock('http://example.test').get('/yay').reply(200, 'Hi')
const req = http.request(
{
host: 'example.test',
path: '/yay',
port: 80,
},
res => {
expect(res.statusCode).to.equal(200)
res.on('end', done)
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.setNoDelay(true)
req.end()
})
it('request emits socket', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
// Using `this`, so can't use arrow function.
req.once('socket', function (socket) {
// https://github.com/nock/nock/pull/769
// https://github.com/nock/nock/pull/779
expect(this).to.equal(req)
expect(socket).to.be.an.instanceof(Object)
done()
})
})
it('socket is shared and aliased correctly', done => {
nock('http://example.test').get('/').reply()
const req = http.get('http://example.test')
req.once('response', res => {
expect(req.socket).to.equal(req.connection)
expect(req.socket).to.equal(res.socket)
expect(res.socket).to.equal(res.client)
expect(res.socket).to.equal(res.connection)
done()
})
})
it('socket emits connect and secureConnect', done => {
nock('https://example.test').post('/').reply(200, 'hey')
const req = https.request({
host: 'example.test',
path: '/',
method: 'POST',
})
const onConnect = sinon.spy()
const onSecureConnect = sinon.spy()
req.on('socket', socket => {
socket.once('connect', () => {
onConnect()
req.end()
})
socket.once('secureConnect', onSecureConnect)
})
req.once('response', res => {
res.setEncoding('utf8')
res.on('data', data => {
expect(data).to.equal('hey')
expect(onConnect).to.have.been.calledOnce()
expect(onSecureConnect).to.have.been.calledOnce()
done()
})
})
})
it('socket has address() method', done => {
nock('http://example.test').get('/').reply()
const req = http.get('http://example.test')
req.once('socket', socket => {
expect(socket.address()).to.deep.equal({
port: 80,
family: 'IPv4',
address: '127.0.0.1',
})
done()
})
})
it('socket has address() method, https/IPv6', done => {
nock('https://example.test').get('/').reply()
const req = https.get('https://example.test', { family: 6 })
req.once('socket', socket => {
expect(socket.address()).to.deep.equal({
port: 443,
family: 'IPv6',
address: '::1',
})
done()
})
})
it('socket has setKeepAlive() method', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
req.once('socket', socket => {
socket.setKeepAlive(true)
done()
})
})
it('socket has ref() and unref() method', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
req.once('socket', socket => {
expect(socket).to.respondTo('ref').and.to.to.respondTo('unref')
// FIXME: These functions, and many of the other Socket functions, should
// actually return `this`.
// https://github.com/nock/nock/pull/1770#discussion_r343425097
expect(socket.ref()).to.be.undefined()
expect(socket.unref()).to.be.undefined()
done()
})
})
it('socket has destroy() method', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
req.on('error', () => {}) // listen for error so it doesn't bubble
req.once('socket', socket => {
socket.destroy()
done()
})
})
it('calling Socket#destroy() multiple times only emits a single `close` event', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
req.on('error', () => {}) // listen for error so it doesn't bubble
req.once('socket', socket => {
const closeSpy = sinon.spy()
socket.on('close', closeSpy)
socket.destroy().destroy().destroy()
setTimeout(() => {
expect(closeSpy).to.have.been.calledOnce()
done()
}, 10)
})
})
it('socket has getPeerCertificate() method which returns a random base64 string', done => {
nock('http://example.test').get('/').reply()
const req = http.get('http://example.test')
req.once('socket', socket => {
const first = socket.getPeerCertificate()
const second = socket.getPeerCertificate()
expect(first).to.be.a('string')
expect(second).to.be.a('string').and.not.equal(first)
done()
})
})
it('abort destroys socket', done => {
nock('http://example.test').get('/').reply(200, 'hey')
const req = http.get('http://example.test')
// Ignore errors.
req.once('error', () => {})
req.once('socket', socket => {
req.abort()
expect(socket.destroyed).to.be.true()
done()
})
})
it('should throw expected error when creating request with missing options', done => {
expect(() => http.request()).to.throw(
Error,
'Making a request with empty `options` is not supported in Nock'
)
done()
})
// https://github.com/nock/nock/issues/1558
it("mocked requests have 'method' property", done => {
const scope = nock('http://example.test').get('/somepath').reply(200, {})
const req = http.request({
host: 'example.test',
path: '/somepath',
method: 'GET',
port: 80,
})
expect(req.method).to.equal('GET')
req.on('response', res => {
expect(res.req.method).to.equal('GET')
scope.done()
done()
})
req.end()
})
// https://github.com/nock/nock/issues/1493
it("response has 'complete' property and it's true after end", done => {
const scope = nock('http://example.test')
.get('/')
.reply(200, 'Hello World!')
const req = http.request(
{
host: 'example.test',
method: 'GET',
path: '/',
port: 80,
},
res => {
res.on('end', () => {
expect(res.complete).to.be.true()
scope.done()
done()
})
// Streams start in 'paused' mode and must be started.
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
res.resume()
}
)
req.end()
})
it('Request with `Expect: 100-continue` triggers continue event', done => {
// This is a replacement for a wide-bracket regression test that was added
// for https://github.com/nock/nock/issues/256.
//
// The behavior was subsequently changed so 'continue' is emitted only when
// the `Expect: 100-continue` header is present.
//
// This test was adapted from this test from Node:
// https://github.com/nodejs/node/blob/1b2d3f7ae7f0391908b70b0333a5adef3c8cb79d/test/parallel/test-http-expect-continue.js#L35
//
// Related:
// https://tools.ietf.org/html/rfc2616#section-8.2.3
// https://github.com/nodejs/node/issues/10487
const exampleRequestBody = 'this is the full request body'
const scope = nock('http://example.test')
.post('/', exampleRequestBody)
.reply()
const req = http.request({
host: 'example.test',
method: 'POST',
path: '/',
port: 80,
headers: { Expect: '100-continue' },
})
const onData = sinon.spy()
req.on('response', res => {
expect(res.statusCode).to.equal(200)
// The `end` event will not fire without a `data` listener, though it
// will never fire since the body is empty. This is consistent with
// the Node docs:
// https://nodejs.org/api/http.html#http_class_http_clientrequest
res.on('data', onData)
res.on('end', () => {
expect(onData).not.to.have.been.called()
scope.done()
done()
})
})
req.on('continue', () => {
req.end(exampleRequestBody)
})
})
// https://github.com/nock/nock/issues/1836
it('when http.get and http.request have been overridden before nock overrides them, http.get calls through to the expected method', async () => {
// Obtain the original `http.request()` and stub it out, as a library might.
nock.restore()
const overriddenRequest = sinon.stub(http, 'request').callThrough()
const overriddenGet = sinon.stub(http, 'get').callThrough()
// Let Nock override them again.
nock.activate()
const { origin } = await servers.startHttpServer((request, response) => {
response.writeHead(200)
response.end()
})
const req = http.get(origin)
expect(overriddenGet).to.have.been.calledOnce()
expect(overriddenRequest).not.to.have.been.called()
req.abort()
})
// https://github.com/nock/nock/issues/1836
it('when http.get and http.request have been overridden before nock overrides them, http.request calls through to the expected method', async () => {
// Obtain the original `http.request()` and stub it out, as a library might.
nock.restore()
const overriddenRequest = sinon.stub(http, 'request').callThrough()
const overriddenGet = sinon.stub(http, 'get').callThrough()
// Let Nock override them again.
nock.activate()
const req = http.request({
host: 'localhost',
path: '/',
port: 1234,
})
expect(overriddenRequest).to.have.been.calledOnce()
expect(overriddenGet).not.to.have.been.called()
req.abort()
})
// https://github.com/nock/nock/issues/2231
it('mocking a request which sends an empty buffer should finalize', async () => {
const prefixUrl = 'http://www.test.com'
const bufferEndpoint = 'upload/buffer/'
nock(prefixUrl).post(`/${bufferEndpoint}`).reply(200, 'BUFFER_SENT')
const formData = new FormData()
formData.append('fileData', Buffer.alloc(0), 'chunk')
const options = {
prefixUrl,
body: formData,
}
const { body: response } = await got.post(bufferEndpoint, options)
expect(response).to.equal('BUFFER_SENT')
})
})