Skip to content

Commit dce5e4d

Browse files
authored
added new tests for errors and client (#831)
1 parent e596da8 commit dce5e4d

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

test/client-abort.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const { Client, errors } = require('..')
55
const { createServer } = require('http')
66
const { Readable } = require('stream')
77

8+
class OnAbortError extends Error {}
9+
810
test('aborted response errors', (t) => {
911
t.plan(3)
1012

@@ -166,3 +168,46 @@ test('abort pipelined', (t) => {
166168
})
167169
})
168170
})
171+
172+
test('propagate unallowed throws in request.onError', (t) => {
173+
t.plan(2)
174+
175+
const server = createServer((req, res) => {
176+
res.end()
177+
})
178+
t.teardown(server.close.bind(server))
179+
180+
server.listen(0, () => {
181+
const client = new Client(`http://localhost:${server.address().port}`)
182+
t.teardown(client.destroy.bind(client))
183+
184+
client.dispatch({
185+
method: 'GET',
186+
path: '/'
187+
}, {
188+
onConnect (abort) {
189+
setImmediate(abort)
190+
},
191+
onHeaders () {
192+
t.pass()
193+
},
194+
onData () {
195+
t.pass()
196+
},
197+
onComplete () {
198+
t.pass()
199+
},
200+
onError () {
201+
throw new OnAbortError('error')
202+
}
203+
})
204+
205+
client.on('error', (err) => {
206+
t.type(err, OnAbortError)
207+
})
208+
209+
client.on('disconnect', () => {
210+
t.pass()
211+
})
212+
})
213+
})

test/errors.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const scenarios = [
2626
createScenario(errors.ClientDestroyedError, 'The client is destroyed', 'ClientDestroyedError', 'UND_ERR_DESTROYED'),
2727
createScenario(errors.ClientClosedError, 'The client is closed', 'ClientClosedError', 'UND_ERR_CLOSED'),
2828
createScenario(errors.SocketError, 'Socket error', 'SocketError', 'UND_ERR_SOCKET'),
29-
createScenario(errors.NotSupportedError, 'Not supported error', 'NotSupportedError', 'UND_ERR_NOT_SUPPORTED')
29+
createScenario(errors.NotSupportedError, 'Not supported error', 'NotSupportedError', 'UND_ERR_NOT_SUPPORTED'),
30+
createScenario(errors.ResponseContentLengthMismatchError, 'Response body length does not match content-length header', 'ResponseContentLengthMismatchError', 'UND_ERR_RES_CONTENT_LENGTH_MISMATCH')
3031
]
3132

3233
scenarios.forEach(scenario => {
@@ -65,3 +66,16 @@ scenarios.forEach(scenario => {
6566
t.end()
6667
})
6768
})
69+
70+
test('Default HTTPParseError Codes', t => {
71+
test('code and data should be undefined when not set', t => {
72+
t.plan(2)
73+
74+
const error = new errors.HTTPParserError('HTTPParserError')
75+
76+
t.equal(error.code, undefined)
77+
t.equal(error.data, undefined)
78+
})
79+
80+
t.end()
81+
})

0 commit comments

Comments
 (0)