2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2015-11-24 20:59:49 -08:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const http = require('http');
|
2012-05-21 00:31:28 +02:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let nchunks = 0;
|
2012-05-21 00:31:28 +02:00
|
|
|
|
2015-11-24 20:59:49 -08:00
|
|
|
const options = {
|
2012-05-21 00:31:28 +02:00
|
|
|
method: 'GET',
|
2016-05-29 03:06:56 -04:00
|
|
|
port: undefined,
|
2012-05-21 00:31:28 +02:00
|
|
|
host: '127.0.0.1',
|
|
|
|
path: '/'
|
|
|
|
};
|
|
|
|
|
2015-11-24 20:59:49 -08:00
|
|
|
const server = http.createServer(function(req, res) {
|
2016-05-04 22:20:27 -07:00
|
|
|
res.writeHead(200, {'Content-Length': '2'});
|
2012-05-21 00:31:28 +02:00
|
|
|
res.write('*');
|
2016-12-23 12:54:32 -08:00
|
|
|
server.once('timeout', common.mustCall(function() { res.end('*'); }));
|
2012-05-21 00:31:28 +02:00
|
|
|
});
|
|
|
|
|
2016-05-29 03:06:56 -04:00
|
|
|
server.listen(0, options.host, function() {
|
|
|
|
options.port = this.address().port;
|
2015-11-24 20:59:49 -08:00
|
|
|
const req = http.request(options, onresponse);
|
2012-05-21 00:31:28 +02:00
|
|
|
req.end();
|
|
|
|
|
|
|
|
function onresponse(res) {
|
2016-12-23 12:54:32 -08:00
|
|
|
req.setTimeout(50, common.mustCall(function() {
|
|
|
|
assert.strictEqual(nchunks, 1); // should have received the first chunk
|
|
|
|
server.emit('timeout');
|
|
|
|
}));
|
2012-05-21 00:31:28 +02:00
|
|
|
|
2016-12-23 12:54:32 -08:00
|
|
|
res.on('data', common.mustCall(function(data) {
|
|
|
|
assert.strictEqual('' + data, '*');
|
2012-05-21 00:31:28 +02:00
|
|
|
nchunks++;
|
2016-12-23 12:54:32 -08:00
|
|
|
}, 2));
|
2012-05-21 00:31:28 +02:00
|
|
|
|
2016-12-23 12:54:32 -08:00
|
|
|
res.on('end', common.mustCall(function() {
|
|
|
|
assert.strictEqual(nchunks, 2);
|
2012-05-21 00:31:28 +02:00
|
|
|
server.close();
|
2016-12-23 12:54:32 -08:00
|
|
|
}));
|
2012-05-21 00:31:28 +02:00
|
|
|
}
|
|
|
|
});
|