Updated abort behavior (#1141)

* Updated abort behavior

- Support for aborting a request with the promise api
- Aborting a request will cause a RequestAbortedError
- Normalized Connection class errors, now every error returned is
wrapped by the client errors constructors

* Updated test

* Updated docs

* Updated code generation script

* Renamed test

* Code coverage

* Avoid calling twice transport.request
This commit is contained in:
Tomas Della Vedova
2020-04-06 11:21:19 +02:00
committed by GitHub
parent 953a8033ab
commit 27a8e2a9bf
16 changed files with 824 additions and 648 deletions

View File

@ -12,7 +12,7 @@ const { Agent } = require('http')
const intoStream = require('into-stream')
const { buildServer } = require('../utils')
const Connection = require('../../lib/Connection')
const { TimeoutError, ConfigurationError } = require('../../lib/errors')
const { TimeoutError, ConfigurationError, RequestAbortedError } = require('../../lib/errors')
test('Basic (http)', t => {
t.plan(4)
@ -855,3 +855,48 @@ test('Should not add agent and ssl to the serialized connection', t => {
t.end()
})
test('Abort a request syncronously', t => {
t.plan(1)
function handler (req, res) {
t.fail('The server should not be contacted')
}
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`)
})
const request = connection.request({
path: '/hello',
method: 'GET'
}, (err, res) => {
t.ok(err instanceof RequestAbortedError)
server.stop()
})
request.abort()
})
})
test('Abort a request asyncronously', t => {
t.plan(1)
function handler (req, res) {
// might be called or not
res.end('ok')
}
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`)
})
const request = connection.request({
path: '/hello',
method: 'GET'
}, (err, res) => {
t.ok(err instanceof RequestAbortedError)
server.stop()
})
setImmediate(() => request.abort())
})
})