Skip to content

Commit

Permalink
fix: check signal.aborted
Browse files Browse the repository at this point in the history
Fixes: #475
  • Loading branch information
ronag committed Nov 13, 2020
1 parent 5ab13dc commit b3a5c35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/abort-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function addSignal (self, signal) {
} else {
self[kSignal].addListener('abort', self[kListener])
}

if (signal.aborted) {
self[kListener]()
}
}

function removeSignal (self) {
Expand Down
21 changes: 21 additions & 0 deletions test/abort-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ if (global.AbortController) {
})
}
for (const { AbortControllerImpl, controllerName } of controllers) {
test(`Abort ${controllerName} before creating request`, (t) => {
t.plan(1)

const server = createServer((req, res) => {
t.fail()
})
t.teardown(server.close.bind(server))

server.listen(0, () => {
const client = new Client(`https://localhost:${server.address().port}`)
const abortController = new AbortControllerImpl()
t.teardown(client.destroy.bind(client))

abortController.abort()

client.request({ path: '/', method: 'GET', signal: abortController.signal }, (err, response) => {
t.ok(err instanceof errors.RequestAbortedError)
})
})
})

test(`Abort ${controllerName} before sending request (no body)`, (t) => {
t.plan(3)

Expand Down

0 comments on commit b3a5c35

Please sign in to comment.