http: fix test where aborted should not be emitted
PR-URL: https://github.com/nodejs/node/pull/20077 Fixes: https://github.com/nodejs/node/issues/20107 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
2550ddb049
commit
461bf36d70
@ -541,7 +541,8 @@ added: v0.3.8
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
Marks the request as aborting. Calling this will cause remaining data
|
Marks the request as aborting. Calling this will cause remaining data
|
||||||
in the response to be dropped and the socket to be destroyed.
|
in the response to be dropped and the socket to be destroyed. After
|
||||||
|
calling this method no further errors will be emitted.
|
||||||
|
|
||||||
### request.aborted
|
### request.aborted
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
@ -2142,8 +2143,6 @@ will be emitted in the following order:
|
|||||||
* `'socket'`
|
* `'socket'`
|
||||||
* (`req.abort()` called here)
|
* (`req.abort()` called here)
|
||||||
* `'abort'`
|
* `'abort'`
|
||||||
* `'error'` with an error with message `'Error: socket hang up'` and code
|
|
||||||
`'ECONNRESET'`
|
|
||||||
* `'close'`
|
* `'close'`
|
||||||
|
|
||||||
If `req.abort()` is called after the response is received, the following events
|
If `req.abort()` is called after the response is received, the following events
|
||||||
|
@ -374,7 +374,9 @@ function socketCloseListener() {
|
|||||||
// receive a response. The error needs to
|
// receive a response. The error needs to
|
||||||
// fire on the request.
|
// fire on the request.
|
||||||
req.socket._hadError = true;
|
req.socket._hadError = true;
|
||||||
req.emit('error', connResetException('socket hang up'));
|
if (!req.aborted) {
|
||||||
|
req.emit('error', connResetException('socket hang up'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
req.emit('close');
|
req.emit('close');
|
||||||
}
|
}
|
||||||
@ -400,7 +402,9 @@ function socketErrorListener(err) {
|
|||||||
// For Safety. Some additional errors might fire later on
|
// For Safety. Some additional errors might fire later on
|
||||||
// and we need to make sure we don't double-fire the error event.
|
// and we need to make sure we don't double-fire the error event.
|
||||||
req.socket._hadError = true;
|
req.socket._hadError = true;
|
||||||
req.emit('error', err);
|
if (!req.aborted) {
|
||||||
|
req.emit('error', err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle any pending data
|
// Handle any pending data
|
||||||
@ -434,7 +438,9 @@ function socketOnEnd() {
|
|||||||
// If we don't have a response then we know that the socket
|
// If we don't have a response then we know that the socket
|
||||||
// ended prematurely and we need to emit an error on the request.
|
// ended prematurely and we need to emit an error on the request.
|
||||||
req.socket._hadError = true;
|
req.socket._hadError = true;
|
||||||
req.emit('error', connResetException('socket hang up'));
|
if (!req.aborted) {
|
||||||
|
req.emit('error', connResetException('socket hang up'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (parser) {
|
if (parser) {
|
||||||
parser.finish();
|
parser.finish();
|
||||||
@ -457,7 +463,9 @@ function socketOnData(d) {
|
|||||||
freeParser(parser, req, socket);
|
freeParser(parser, req, socket);
|
||||||
socket.destroy();
|
socket.destroy();
|
||||||
req.socket._hadError = true;
|
req.socket._hadError = true;
|
||||||
req.emit('error', ret);
|
if (!req.aborted) {
|
||||||
|
req.emit('error', ret);
|
||||||
|
}
|
||||||
} else if (parser.incoming && parser.incoming.upgrade) {
|
} else if (parser.incoming && parser.incoming.upgrade) {
|
||||||
// Upgrade (if status code 101) or CONNECT
|
// Upgrade (if status code 101) or CONNECT
|
||||||
var bytesParsed = ret;
|
var bytesParsed = ret;
|
||||||
|
23
test/parallel/test-http-client-aborted.js
Normal file
23
test/parallel/test-http-client-aborted.js
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
const http = require('http');
|
||||||
|
const assert = require('assert');
|
||||||
|
|
||||||
|
const server = http.createServer(common.mustCall(function(req, res) {
|
||||||
|
req.on('aborted', common.mustCall(function() {
|
||||||
|
assert.strictEqual(this.aborted, true);
|
||||||
|
server.close();
|
||||||
|
}));
|
||||||
|
assert.strictEqual(req.aborted, false);
|
||||||
|
res.write('hello');
|
||||||
|
}));
|
||||||
|
|
||||||
|
server.listen(0, common.mustCall(() => {
|
||||||
|
const req = http.get({
|
||||||
|
port: server.address().port,
|
||||||
|
headers: { connection: 'keep-alive' }
|
||||||
|
}, common.mustCall((res) => {
|
||||||
|
req.abort();
|
||||||
|
}));
|
||||||
|
}));
|
21
test/parallel/test-http-client-no-error-after-aborted.js
Normal file
21
test/parallel/test-http-client-no-error-after-aborted.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
const http = require('http');
|
||||||
|
|
||||||
|
const server = http.createServer(common.mustCall((req, res) => {
|
||||||
|
res.write('hello');
|
||||||
|
}));
|
||||||
|
|
||||||
|
server.listen(0, common.mustCall(() => {
|
||||||
|
const req = http.get({
|
||||||
|
port: server.address().port
|
||||||
|
}, common.mustCall((res) => {
|
||||||
|
req.on('error', common.mustNotCall());
|
||||||
|
req.abort();
|
||||||
|
req.socket.destroy(new Error());
|
||||||
|
req.on('close', common.mustCall(() => {
|
||||||
|
server.close();
|
||||||
|
}));
|
||||||
|
}));
|
||||||
|
}));
|
@ -23,8 +23,7 @@ server.listen(0, common.localhostIPv4, common.mustCall(() => {
|
|||||||
}));
|
}));
|
||||||
}));
|
}));
|
||||||
req.on('timeout', common.mustCall(() => req.abort()));
|
req.on('timeout', common.mustCall(() => req.abort()));
|
||||||
req.on('error', common.mustCall((err) => {
|
req.on('abort', common.mustCall(() => {
|
||||||
assert.strictEqual(err.message, 'socket hang up');
|
|
||||||
server.close();
|
server.close();
|
||||||
}));
|
}));
|
||||||
}));
|
}));
|
||||||
|
@ -34,7 +34,7 @@ const server = createServer(common.mustCall((req, res) => {
|
|||||||
}));
|
}));
|
||||||
}).listen(0, () => {
|
}).listen(0, () => {
|
||||||
external = get(`http://127.0.0.1:${server.address().port}`);
|
external = get(`http://127.0.0.1:${server.address().port}`);
|
||||||
external.on('error', common.mustCall(() => {
|
external.on('abort', common.mustCall(() => {
|
||||||
server.close();
|
server.close();
|
||||||
internal.close();
|
internal.close();
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user