Skip to content

Commit

Permalink
Add test to verify if the connection is correctly aborted on cancel (#…
Browse files Browse the repository at this point in the history
…3219)

* Add test to verify if the connection is correctly aborted on cancel

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* Update test/fetch/exiting.js

Co-authored-by: elf Pavlik <elf-pavlik@hackers4peace.net>

---------

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: elf Pavlik <elf-pavlik@hackers4peace.net>
  • Loading branch information
mcollina and elf-pavlik committed May 13, 2024
1 parent 033530d commit 48b356c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/fetch/exiting.js
@@ -0,0 +1,39 @@
'use strict'

const { test } = require('node:test')
const { fetch } = require('../..')
const { createServer } = require('node:http')
const { closeServerAsPromise } = require('../utils/node-http')
const tspl = require('@matteo.collina/tspl')

test('abort the request on the other side if the stream is canceled', async (t) => {
const p = tspl(t, { plan: 1 })
const server = createServer((req, res) => {
res.writeHead(200)
res.write('hello')
req.on('aborted', () => {
p.ok('aborted')
})
// Let's not end the response on purpose
})
t.after(closeServerAsPromise(server))

await new Promise((resolve) => {
server.listen(0, resolve)
})

const url = new URL(`http://127.0.0.1:${server.address().port}`)

const response = await fetch(url)

const reader = response.body.getReader()

try {
await reader.read()
} finally {
reader.releaseLock()
await response.body.cancel()
}

await p.completed
})

0 comments on commit 48b356c

Please sign in to comment.