From 48b356ce7a8f2461b4d5fa4db905312d4c8f5260 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 13 May 2024 06:16:46 +0200 Subject: [PATCH] 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 * Update test/fetch/exiting.js Co-authored-by: elf Pavlik --------- Signed-off-by: Matteo Collina Co-authored-by: elf Pavlik --- test/fetch/exiting.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 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..0fb007be513 --- /dev/null +++ b/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 +})