forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabort.js
More file actions
81 lines (68 loc) · 2.08 KB
/
abort.js
File metadata and controls
81 lines (68 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const { test } = require('node:test')
const assert = require('node:assert')
const { tspl } = require('@matteo.collina/tspl')
const { fetch } = require('../..')
const { createServer } = require('http')
const { once } = require('events')
const { AbortController: NPMAbortController } = require('abort-controller')
test('Allow the usage of custom implementation of AbortController', async (t) => {
const body = {
fixes: 1605
}
const server = createServer((req, res) => {
res.statusCode = 200
res.end(JSON.stringify(body))
})
t.after(server.close.bind(server))
server.listen(0)
await once(server, 'listening')
const controller = new NPMAbortController()
const signal = controller.signal
controller.abort()
try {
await fetch(`http://localhost:${server.address().port}`, {
signal
})
} catch (e) {
assert.strictEqual(e.code, DOMException.ABORT_ERR)
}
})
test('allows aborting with custom errors', async (t) => {
const server = createServer().listen(0)
t.after(server.close.bind(server))
await once(server, 'listening')
await t.test('Using AbortSignal.timeout with cause', async () => {
const { strictEqual } = tspl(t, { plan: 2 })
try {
await fetch(`http://localhost:${server.address().port}`, {
signal: AbortSignal.timeout(50)
})
assert.fail('should throw')
} catch (err) {
if (err.name === 'TypeError') {
const cause = err.cause
strictEqual(cause.name, 'HeadersTimeoutError')
strictEqual(cause.code, 'UND_ERR_HEADERS_TIMEOUT')
} else if (err.name === 'TimeoutError') {
strictEqual(err.code, DOMException.TIMEOUT_ERR)
strictEqual(err.cause, undefined)
} else {
throw err
}
}
})
t.test('Error defaults to an AbortError DOMException', async () => {
const ac = new AbortController()
ac.abort() // no reason
await assert.rejects(
fetch(`http://localhost:${server.address().port}`, {
signal: ac.signal
}),
{
name: 'AbortError',
code: DOMException.ABORT_ERR
}
)
})
})