Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
May 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions test/fetch/exiting.js
Original file line number Diff line number Diff line change
@@ -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
})
Loading