0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-http-server-client-error.js
Brian White 2bc7841d0f
test: use random ports where possible
This helps to prevent issues where a failed test can keep a bound
socket open long enough to cause other tests to fail with EADDRINUSE
because the same port number is used.

PR-URL: https://github.com/nodejs/node/pull/7045
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2016-06-10 22:30:55 -04:00

40 lines
929 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');
const server = http.createServer(common.mustCall(function(req, res) {
res.end();
}));
server.on('clientError', common.mustCall(function(err, socket) {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
server.close();
}));
server.listen(0, function() {
function next() {
// Invalid request
const client = net.connect(server.address().port);
client.end('Oopsie-doopsie\r\n');
var chunks = '';
client.on('data', function(chunk) {
chunks += chunk;
});
client.once('end', function() {
assert.equal(chunks, 'HTTP/1.1 400 Bad Request\r\n\r\n');
});
}
// Normal request
http.get({ port: this.address().port, path: '/' }, function(res) {
assert.equal(res.statusCode, 200);
res.resume();
res.once('end', next);
});
});