mirror of
https://github.com/nodejs/node.git
synced 2025-05-13 10:54:13 +00:00

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>
48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
'use strict';
|
|
require('../common');
|
|
var assert = require('assert');
|
|
var http = require('http');
|
|
|
|
// Simple test of Node's HTTP ServerResponse.statusCode
|
|
// ServerResponse.prototype.statusCode
|
|
|
|
var testsComplete = 0;
|
|
var tests = [200, 202, 300, 404, 451, 500];
|
|
var testIdx = 0;
|
|
|
|
var s = http.createServer(function(req, res) {
|
|
var t = tests[testIdx];
|
|
res.writeHead(t, {'Content-Type': 'text/plain'});
|
|
console.log('--\nserver: statusCode after writeHead: ' + res.statusCode);
|
|
assert.equal(res.statusCode, t);
|
|
res.end('hello world\n');
|
|
});
|
|
|
|
s.listen(0, nextTest);
|
|
|
|
|
|
function nextTest() {
|
|
if (testIdx + 1 === tests.length) {
|
|
return s.close();
|
|
}
|
|
var test = tests[testIdx];
|
|
|
|
http.get({ port: s.address().port }, function(response) {
|
|
console.log('client: expected status: ' + test);
|
|
console.log('client: statusCode: ' + response.statusCode);
|
|
assert.equal(response.statusCode, test);
|
|
response.on('end', function() {
|
|
testsComplete++;
|
|
testIdx += 1;
|
|
nextTest();
|
|
});
|
|
response.resume();
|
|
});
|
|
}
|
|
|
|
|
|
process.on('exit', function() {
|
|
assert.equal(5, testsComplete);
|
|
});
|
|
|