-
-
Notifications
You must be signed in to change notification settings - Fork 736
/
test_abort.js
211 lines (175 loc) · 6.03 KB
/
test_abort.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
'use strict'
const { expect } = require('chai')
const http = require('http')
const sinon = require('sinon')
const nock = require('..')
// These tests use `setTimeout` before verifying emitted events to ensure any
// number of `nextTicks` or `setImmediate` can process first.
// Node will emit a `prefinish` event after `socket`, but it's an internal,
// undocumented event that Nock does not emulate.
// The order of tests run sequentially through a ClientRequest's lifetime.
// Starting the top by aborting requests early on then aborting later and later.
describe('`ClientRequest.abort()`', () => {
it('Emits the expected event sequence when `write` is called on an aborted request', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.abort()
req.write('foo')
setTimeout(() => {
expect(emitSpy).to.have.been.calledOnceWithExactly('abort')
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
it('Emits the expected event sequence when `end` is called on an aborted request', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.abort()
req.end()
setTimeout(() => {
expect(emitSpy).to.have.been.calledOnceWithExactly('abort')
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
it('Emits the expected event sequence when `flushHeaders` is called on an aborted request', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.abort()
req.flushHeaders()
setTimeout(() => {
expect(emitSpy).to.have.been.calledOnceWithExactly('abort')
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
it('Emits the expected event sequence when aborted immediately after `end`', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.end()
req.abort()
setTimeout(() => {
expect(emitSpy).to.have.been.calledOnceWithExactly('abort')
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
it('Emits the expected event sequence when aborted inside a `socket` event listener', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.on('socket', () => {
req.abort()
})
req.on('error', err => {
expect(err.message).to.equal('socket hang up')
expect(err.code).to.equal('ECONNRESET')
})
req.end()
setTimeout(() => {
const events = emitSpy.args.map(i => i[0])
expect(events).to.deep.equal(['socket', 'abort', 'error', 'close'])
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
it('Emits the expected event sequence when aborted multiple times', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.on('error', () => {}) // listen for error so it doesn't bubble
req.on('socket', () => {
req.abort()
req.abort()
req.abort()
})
req.end()
setTimeout(() => {
const events = emitSpy.args.map(i => i[0])
// important: `abort` and `error` events only fire once and the `close` event still fires at the end
expect(events).to.deep.equal(['socket', 'abort', 'error', 'close'])
expect(scope.isDone()).to.be.false()
done()
}, 10)
})
// The Interceptor is considered consumed just prior to the `finish` event on the request,
// all tests below assert the Scope is done.
it('Emits the expected event sequence when aborted inside a `finish` event listener', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.on('finish', () => {
req.abort()
})
req.on('error', err => {
expect(err.message).to.equal('socket hang up')
expect(err.code).to.equal('ECONNRESET')
})
req.end()
setTimeout(() => {
const events = emitSpy.args.map(i => i[0])
expect(events).to.deep.equal([
'socket',
'finish',
'abort',
'error',
'close',
])
scope.done()
done()
}, 10)
})
it('Emits the expected event sequence when aborted after a delay from the `finish` event', done => {
// use the delay functionality to create a window where the abort is called during the artificial connection wait.
const scope = nock('http://example.test').get('/').delay(100).reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.on('finish', () => {
setTimeout(() => {
req.abort()
}, 10)
})
req.on('error', err => {
expect(err.message).to.equal('socket hang up')
expect(err.code).to.equal('ECONNRESET')
})
req.end()
setTimeout(() => {
const events = emitSpy.args.map(i => i[0])
expect(events).to.deep.equal([
'socket',
'finish',
'abort',
'error',
'close',
])
scope.done()
done()
}, 200)
})
it('Emits the expected event sequence when aborted inside a `response` event listener', done => {
const scope = nock('http://example.test').get('/').reply()
const req = http.request('http://example.test')
const emitSpy = sinon.spy(req, 'emit')
req.on('response', () => {
req.abort()
})
req.end()
setTimeout(() => {
const events = emitSpy.args.map(i => i[0])
expect(events).to.deep.equal([
'socket',
'finish',
'response',
'abort',
'close',
])
scope.done()
done()
}, 10)
})
})