2020-03-08 12:39:50 +01:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const http = require('http');
|
|
|
|
const assert = require('assert');
|
2021-03-12 20:24:00 +02:00
|
|
|
const { getEventListeners } = require('events');
|
2020-03-08 12:39:50 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
// abort
|
|
|
|
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
|
|
res.end('Hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const options = { port: server.address().port };
|
|
|
|
const req = http.get(options, common.mustCall((res) => {
|
|
|
|
res.on('data', (data) => {
|
|
|
|
req.abort();
|
|
|
|
assert.strictEqual(req.aborted, true);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
req.on('error', common.mustNotCall());
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, false);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// destroy + res
|
|
|
|
|
|
|
|
const server = http.createServer(common.mustCall((req, res) => {
|
|
|
|
res.end('Hello');
|
|
|
|
}));
|
|
|
|
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const options = { port: server.address().port };
|
|
|
|
const req = http.get(options, common.mustCall((res) => {
|
|
|
|
res.on('data', (data) => {
|
|
|
|
req.destroy();
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
req.on('error', common.mustNotCall());
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, false);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// destroy
|
|
|
|
|
2020-11-09 12:51:14 +02:00
|
|
|
const server = http.createServer(common.mustNotCall());
|
2020-03-08 12:39:50 +01:00
|
|
|
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const options = { port: server.address().port };
|
|
|
|
const req = http.get(options, common.mustNotCall());
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err.code, 'ECONNRESET');
|
|
|
|
server.close();
|
|
|
|
}));
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, false);
|
|
|
|
req.destroy();
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
}));
|
|
|
|
}
|
2020-11-09 12:51:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
{
|
2021-03-12 20:24:00 +02:00
|
|
|
// Destroy post-abort sync with AbortSignal
|
2020-11-09 12:51:14 +02:00
|
|
|
|
|
|
|
const server = http.createServer(common.mustNotCall());
|
|
|
|
const controller = new AbortController();
|
2021-03-12 20:24:00 +02:00
|
|
|
const { signal } = controller;
|
2020-11-09 12:51:14 +02:00
|
|
|
server.listen(0, common.mustCall(() => {
|
2021-03-12 20:24:00 +02:00
|
|
|
const options = { port: server.address().port, signal };
|
2020-11-09 12:51:14 +02:00
|
|
|
const req = http.get(options, common.mustNotCall());
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err.code, 'ABORT_ERR');
|
|
|
|
assert.strictEqual(err.name, 'AbortError');
|
|
|
|
server.close();
|
|
|
|
}));
|
2021-03-12 20:24:00 +02:00
|
|
|
assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
|
2020-11-09 12:51:14 +02:00
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, false);
|
|
|
|
controller.abort();
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
}));
|
|
|
|
}
|
2021-03-12 20:24:00 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
// Use post-abort async AbortSignal
|
|
|
|
const server = http.createServer(common.mustNotCall());
|
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
const options = { port: server.address().port, signal };
|
|
|
|
const req = http.get(options, common.mustNotCall());
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err.code, 'ABORT_ERR');
|
|
|
|
assert.strictEqual(err.name, 'AbortError');
|
|
|
|
}));
|
|
|
|
|
|
|
|
req.on('close', common.mustCall(() => {
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
server.close();
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert.strictEqual(getEventListeners(signal, 'abort').length, 1);
|
|
|
|
process.nextTick(() => controller.abort());
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Use pre-aborted AbortSignal
|
|
|
|
const server = http.createServer(common.mustNotCall());
|
|
|
|
const controller = new AbortController();
|
|
|
|
const { signal } = controller;
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
|
|
controller.abort();
|
|
|
|
const options = { port: server.address().port, signal };
|
|
|
|
const req = http.get(options, common.mustNotCall());
|
|
|
|
assert.strictEqual(getEventListeners(signal, 'abort').length, 0);
|
|
|
|
req.on('error', common.mustCall((err) => {
|
|
|
|
assert.strictEqual(err.code, 'ABORT_ERR');
|
|
|
|
assert.strictEqual(err.name, 'AbortError');
|
|
|
|
server.close();
|
|
|
|
}));
|
|
|
|
assert.strictEqual(req.aborted, false);
|
|
|
|
assert.strictEqual(req.destroyed, true);
|
|
|
|
}));
|
|
|
|
}
|