node/test/parallel/test-http-client-parse-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
950 B
JavaScript

'use strict';
require('../common');
var assert = require('assert');
var http = require('http');
var net = require('net');
var connects = 0;
var parseErrors = 0;
// Create a TCP server
net.createServer(function(c) {
console.log('connection');
if (++connects === 1) {
c.end('HTTP/1.1 302 Object Moved\r\nContent-Length: 0\r\n\r\nhi world');
} else {
c.end('bad http - should trigger parse error\r\n');
this.close();
}
}).listen(0, '127.0.0.1', function() {
for (var i = 0; i < 2; i++) {
http.request({
host: '127.0.0.1',
port: this.address().port,
method: 'GET',
path: '/'
}).on('error', function(e) {
console.log('got error from client');
assert.ok(e.message.indexOf('Parse Error') >= 0);
assert.equal(e.code, 'HPE_INVALID_CONSTANT');
parseErrors++;
}).end();
}
});
process.on('exit', function() {
assert.equal(connects, 2);
assert.equal(parseErrors, 2);
});