From 3fd089485b540d6c951d53000a98e9d0affd123b Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 8 May 2024 09:34:40 +0200 Subject: [PATCH 1/2] Add test to verify if the connection is correctly aborted on cancel Signed-off-by: Matteo Collina --- test/fetch/exiting.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/fetch/exiting.js diff --git a/test/fetch/exiting.js b/test/fetch/exiting.js new file mode 100644 index 00000000000..ae410b5fdf9 --- /dev/null +++ b/test/fetch/exiting.js @@ -0,0 +1,42 @@ +'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, { + // if added both scripts will also exit on 21 and 22.0 + // dispatcher: new Agent({ bodyTimeout: 1000 }) + }) + + const reader = response.body.getReader() + + try { + await reader.read() + } finally { + reader.releaseLock() + await response.body.cancel() + } + + await p.completed +}) From e6f3ac433cd1eccf79a1d03f0248eab44b06d3e5 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 8 May 2024 17:44:29 +0200 Subject: [PATCH 2/2] Update test/fetch/exiting.js Co-authored-by: elf Pavlik --- test/fetch/exiting.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/fetch/exiting.js b/test/fetch/exiting.js index ae410b5fdf9..0fb007be513 100644 --- a/test/fetch/exiting.js +++ b/test/fetch/exiting.js @@ -24,10 +24,7 @@ test('abort the request on the other side if the stream is canceled', async (t) const url = new URL(`http://127.0.0.1:${server.address().port}`) - const response = await fetch(url, { - // if added both scripts will also exit on 21 and 22.0 - // dispatcher: new Agent({ bodyTimeout: 1000 }) - }) + const response = await fetch(url) const reader = response.body.getReader()