Skip to content

Commit 7f21d0c

Browse files
authored
fix: InterceptedRequestRouter.handleWrite arity issue (#2371)
1 parent e37bf02 commit 7f21d0c

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ rules:
2727
# TODO These are included in standard and should be cleaned up and turned on.
2828
n/no-deprecated-api: 'off' # we still make heavy use of url.parse
2929

30+
# TODO remove once we drop support for Node 10
31+
node/no-deprecated-api: 'off'
32+
3033
# Nock additions.
3134
strict: ['error', 'safe']
3235
no-console: 'error'

lib/intercepted_request_router.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ class InterceptedRequestRouter {
143143

144144
// from docs: When write function is called with empty string or buffer, it does nothing and waits for more input.
145145
// However, actually implementation checks the state of finished and aborted before checking if the first arg is empty.
146-
handleWrite(buffer, encoding, callback) {
146+
handleWrite(...args) {
147147
debug('request write')
148+
149+
let [buffer, encoding] = args
150+
148151
const { req } = this
149152

150153
if (req.finished) {
@@ -171,6 +174,9 @@ class InterceptedRequestRouter {
171174
}
172175
this.requestBodyBuffers.push(buffer)
173176

177+
// writable.write encoding param is optional
178+
// so if callback is present it's the last argument
179+
const callback = args.length > 1 ? args[args.length - 1] : undefined
174180
// can't use instanceof Function because some test runners
175181
// run tests in vm.runInNewContext where Function is not same
176182
// as that in the current context

tests/test_request_overrider.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ describe('Request Overrider', () => {
8989
})
9090
})
9191

92+
it('write callback called when encoding is not supplied', done => {
93+
const scope = nock('http://example.test')
94+
.filteringRequestBody(/mia/, 'nostra')
95+
.post('/', 'mamma nostra')
96+
.reply()
97+
98+
const reqWriteCallback = sinon.spy()
99+
100+
const req = http.request(
101+
{
102+
host: 'example.test',
103+
method: 'POST',
104+
path: '/',
105+
port: 80,
106+
},
107+
res => {
108+
expect(reqWriteCallback).to.have.been.calledOnce()
109+
expect(res.statusCode).to.equal(200)
110+
res.on('end', () => {
111+
scope.done()
112+
done()
113+
})
114+
// Streams start in 'paused' mode and must be started.
115+
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
116+
res.resume()
117+
}
118+
)
119+
120+
req.write('mamma mia', () => {
121+
reqWriteCallback()
122+
req.end()
123+
})
124+
})
125+
92126
it('write callback is not called if the provided chunk is undefined', done => {
93127
const scope = nock('http://example.test').post('/').reply()
94128

@@ -117,6 +151,34 @@ describe('Request Overrider', () => {
117151
req.end()
118152
})
119153

154+
it("write doesn't throw if invoked w/o callback", done => {
155+
const scope = nock('http://example.test').post('/').reply()
156+
157+
const reqWriteCallback = sinon.spy()
158+
159+
const req = http.request(
160+
{
161+
host: 'example.test',
162+
method: 'POST',
163+
path: '/',
164+
},
165+
res => {
166+
expect(res.statusCode).to.equal(200)
167+
res.on('end', () => {
168+
expect(reqWriteCallback).to.not.have.been.called()
169+
scope.done()
170+
done()
171+
})
172+
// Streams start in 'paused' mode and must be started.
173+
// See https://nodejs.org/api/stream.html#stream_class_stream_readable
174+
res.resume()
175+
}
176+
)
177+
178+
req.write(undefined)
179+
req.end()
180+
})
181+
120182
it('end callback called', done => {
121183
const scope = nock('http://example.test')
122184
.filteringRequestBody(/mia/, 'nostra')

0 commit comments

Comments
 (0)