Skip to content

Commit 48b356c

Browse files
mcollinaelf-pavlik
andauthored
Add test to verify if the connection is correctly aborted on cancel (#3219)
* Add test to verify if the connection is correctly aborted on cancel Signed-off-by: Matteo Collina <[email protected]> * Update test/fetch/exiting.js Co-authored-by: elf Pavlik <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]> Co-authored-by: elf Pavlik <[email protected]>
1 parent 033530d commit 48b356c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test/fetch/exiting.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const { fetch } = require('../..')
5+
const { createServer } = require('node:http')
6+
const { closeServerAsPromise } = require('../utils/node-http')
7+
const tspl = require('@matteo.collina/tspl')
8+
9+
test('abort the request on the other side if the stream is canceled', async (t) => {
10+
const p = tspl(t, { plan: 1 })
11+
const server = createServer((req, res) => {
12+
res.writeHead(200)
13+
res.write('hello')
14+
req.on('aborted', () => {
15+
p.ok('aborted')
16+
})
17+
// Let's not end the response on purpose
18+
})
19+
t.after(closeServerAsPromise(server))
20+
21+
await new Promise((resolve) => {
22+
server.listen(0, resolve)
23+
})
24+
25+
const url = new URL(`http://127.0.0.1:${server.address().port}`)
26+
27+
const response = await fetch(url)
28+
29+
const reader = response.body.getReader()
30+
31+
try {
32+
await reader.read()
33+
} finally {
34+
reader.releaseLock()
35+
await response.body.cancel()
36+
}
37+
38+
await p.completed
39+
})

0 commit comments

Comments
 (0)