forked from nock/nock
/
test_socket.js
98 lines (78 loc) · 2.55 KB
/
test_socket.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
'use strict'
const { expect } = require('chai')
const http = require('http')
const https = require('https')
const { Readable } = require('stream')
const nock = require('..')
it('should expose TLSSocket attributes for HTTPS requests', done => {
nock('https://example.test').get('/').reply()
https.get('https://example.test').on('socket', socket => {
expect(socket.authorized).to.equal(true)
expect(socket.encrypted).to.equal(true)
done()
})
})
it('should not have TLSSocket attributes for HTTP requests', done => {
nock('http://example.test').get('/').reply()
http.get('http://example.test').on('socket', socket => {
expect(socket.authorized).to.equal(undefined)
expect(socket.encrypted).to.equal(undefined)
done()
})
})
describe('`Socket#setTimeout()`', () => {
it('adds callback as a one-time listener for parity with a real socket', done => {
nock('http://example.test').get('/').delayConnection(100).reply()
const onTimeout = () => {
done()
}
http.get('http://example.test').on('socket', socket => {
socket.setTimeout(50, onTimeout)
})
})
it('can be called without a callback', done => {
nock('http://example.test').get('/').delayConnection(100).reply()
http.get('http://example.test').on('socket', socket => {
socket.setTimeout(50)
socket.on('timeout', () => {
done()
})
})
})
})
describe('`Socket#destroy()`', () => {
it('can destroy the socket if stream is not finished', async () => {
const scope = nock('http://example.test')
scope.intercept('/somepath', 'GET').reply(() => {
const buffer = Buffer.allocUnsafe(10000000)
const data = new MemoryReadableStream(buffer, { highWaterMark: 128 })
return [200, data]
})
const req = http.get('http://example.test/somepath')
const stream = await new Promise(resolve => req.on('response', resolve))
// close after first chunk of data
stream.on('data', () => stream.destroy())
await new Promise((resolve, reject) => {
stream.on('error', reject)
stream.on('close', resolve)
stream.on('end', resolve)
})
})
})
class MemoryReadableStream extends Readable {
constructor(content) {
super()
this._content = content
this._currentOffset = 0
}
_read(size) {
if (this._currentOffset >= this._content.length) {
this.push(null)
return
}
const nextOffset = this._currentOffset + size
const content = this._content.slice(this._currentOffset, nextOffset)
this._currentOffset = nextOffset
this.push(content)
}
}