mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
a4a089a568
On some platforms the `'end'` event might not be emitted because the socket could be destroyed by the other peer while the client is still sending the data triggering an error. Use the `'close'` event instead. PR-URL: https://github.com/nodejs/node/pull/30360 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
const { mustCall } = require('../common');
|
|
|
|
const fs = require('fs');
|
|
const http = require('http');
|
|
const { strictEqual } = require('assert');
|
|
|
|
const server = http.createServer(mustCall(function(req, res) {
|
|
strictEqual(req.socket.listenerCount('data'), 1);
|
|
req.socket.once('data', mustCall(function() {
|
|
// Ensure that a chunk of data is received before calling `res.end()`.
|
|
res.end('hello world');
|
|
}));
|
|
// This checks if the request gets dumped
|
|
// resume will be triggered by res.end().
|
|
req.on('resume', mustCall(function() {
|
|
// There is no 'data' event handler anymore
|
|
// it gets automatically removed when dumping the request.
|
|
strictEqual(req.listenerCount('data'), 0);
|
|
req.on('data', mustCall());
|
|
}));
|
|
|
|
// We explicitly pause the stream
|
|
// so that the following on('data') does not cause
|
|
// a resume.
|
|
req.pause();
|
|
req.on('data', function() {});
|
|
|
|
// Start sending the response.
|
|
res.flushHeaders();
|
|
}));
|
|
|
|
server.listen(0, mustCall(function() {
|
|
const req = http.request({
|
|
method: 'POST',
|
|
port: server.address().port
|
|
});
|
|
|
|
// Send the http request without waiting
|
|
// for the body.
|
|
req.flushHeaders();
|
|
|
|
req.on('response', mustCall(function(res) {
|
|
// Pipe the body as soon as we get the headers of the
|
|
// response back.
|
|
fs.createReadStream(__filename).pipe(req);
|
|
|
|
res.resume();
|
|
|
|
// On some platforms the `'end'` event might not be emitted because the
|
|
// socket could be destroyed by the other peer while data is still being
|
|
// sent. In this case the 'aborted'` event is emitted instead of `'end'`.
|
|
// `'close'` is used here because it is always emitted and does not
|
|
// invalidate the test.
|
|
res.on('close', function() {
|
|
server.close();
|
|
});
|
|
}));
|
|
|
|
req.on('error', function() {
|
|
// An error can happen if there is some data still
|
|
// being sent, as the other side is calling .destroy()
|
|
// this is safe to ignore.
|
|
});
|
|
}));
|