-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(fetch): prevent crash when
fetch
is aborted with null
as the …
…`AbortSignal's` `reason`
- Loading branch information
1 parent
8db8d9d
commit 5df9e00
Showing
3 changed files
with
47 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { beforeEach, describe, it } = require('node:test') | ||
const assert = require('node:assert') | ||
const { fetch } = require('../..') | ||
const nodeFetch = require('../../index-fetch') | ||
|
||
test('fetch with signal already aborted', async () => { | ||
await assert.rejects( | ||
fetch('http://localhost', { | ||
signal: AbortSignal.abort('Already aborted') | ||
}), | ||
/Already aborted/ | ||
describe('Issue #2242', () => { | ||
['Already aborted', null, false, true, 123, Symbol('Some reason')].forEach( | ||
(reason) => | ||
describe(`when an already-aborted signal's reason is \`${String( | ||
reason | ||
)}\``, () => { | ||
let signal | ||
beforeEach(() => { | ||
signal = AbortSignal.abort(reason) | ||
}) | ||
it('rejects with that reason ', async () => { | ||
await assert.rejects(fetch('http://localhost', { signal }), (err) => { | ||
assert.strictEqual(err, reason) | ||
return true | ||
}) | ||
}) | ||
it('rejects with that reason (from index-fetch)', async () => { | ||
await assert.rejects( | ||
nodeFetch.fetch('http://localhost', { signal }), | ||
(err) => { | ||
assert.strictEqual(err, reason) | ||
return true | ||
} | ||
) | ||
}) | ||
}) | ||
) | ||
}) | ||
|
||
test('fetch with signal already aborted (from index-fetch)', async () => { | ||
await assert.rejects( | ||
nodeFetch.fetch('http://localhost', { | ||
signal: AbortSignal.abort('Already aborted') | ||
}), | ||
/Already aborted/ | ||
) | ||
describe("when an already-aborted signal's reason is `undefined`", () => { | ||
let signal | ||
beforeEach(() => { | ||
signal = AbortSignal.abort(undefined) | ||
}) | ||
it('rejects with an `AbortError`', async () => { | ||
await assert.rejects( | ||
fetch('http://localhost', { signal }), | ||
new DOMException('This operation was aborted', 'AbortError') | ||
) | ||
}) | ||
it('rejects with an `AbortError` (from index-fetch)', async () => { | ||
await assert.rejects( | ||
nodeFetch.fetch('http://localhost', { signal }), | ||
new DOMException('This operation was aborted', 'AbortError') | ||
) | ||
}) | ||
}) | ||
}) |