Skip to content

Commit

Permalink
Use random ports for servers used in tests
Browse files Browse the repository at this point in the history
Not only does this avoid problems with the fixed port already
being used on the local system, but it could also avoid cascading
test failures and potential race conditions between tests.
  • Loading branch information
mscdex committed Oct 14, 2016
1 parent 3c074df commit 53c9b98
Show file tree
Hide file tree
Showing 56 changed files with 966 additions and 914 deletions.
8 changes: 4 additions & 4 deletions tests/browser/start.js
Expand Up @@ -4,8 +4,6 @@ var https = require('https')
var fs = require('fs')
var path = require('path')

var port = 6767

var server = https.createServer({
key: fs.readFileSync(path.join(__dirname, '/ssl/server.key')),
cert: fs.readFileSync(path.join(__dirname, '/ssl/server.crt')),
Expand All @@ -18,12 +16,14 @@ var server = https.createServer({
res.writeHead(200)
res.end('Can you hear the sound of an enormous door slamming in the depths of hell?\n')
})
server.listen(port, function() {
server.listen(0, function() {
var port = this.address().port
console.log('Started https server for karma tests on port ' + port)
// Spawn process for karma.
var c = spawn('karma', [
'start',
path.join(__dirname, '/karma.conf.js')
path.join(__dirname, '/karma.conf.js'),
'https://localhost:' + port
])
c.stdout.pipe(process.stdout)
c.stderr.pipe(process.stderr)
Expand Down
2 changes: 1 addition & 1 deletion tests/browser/test.js
Expand Up @@ -26,7 +26,7 @@ tape('returns on error', function(t) {
tape('succeeds on valid URLs (with https and CORS)', function(t) {
t.plan(1)
request({
uri: 'https://localhost:6767',
uri: process.argv[2],
withCredentials: false
}, function (error, response) {
t.equal(response.statusCode, 200)
Expand Down
34 changes: 18 additions & 16 deletions tests/server.js
Expand Up @@ -7,22 +7,20 @@ var fs = require('fs')
, stream = require('stream')
, assert = require('assert')

exports.port = 6767
exports.portSSL = 16167

exports.createServer = function (port) {
port = port || exports.port
exports.createServer = function () {
var s = http.createServer(function (req, resp) {
s.emit(req.url.replace(/(\?.*)/, ''), req, resp)
})
s.port = port
s.url = 'http://localhost:' + port
s.on('listening', function () {
s.port = this.address().port
s.url = 'http://localhost:' + s.port
})
s.port = 0
s.protocol = 'http'
return s
}

exports.createEchoServer = function (port) {
port = port || exports.port
exports.createEchoServer = function () {
var s = http.createServer(function (req, resp) {
var b = ''
req.on('data', function (chunk) {b += chunk})
Expand All @@ -37,15 +35,16 @@ exports.createEchoServer = function (port) {
resp.end()
})
})
s.port = port
s.url = 'http://localhost:' + port
s.on('listening', function () {
s.port = this.address().port
s.url = 'http://localhost:' + s.port
})
s.port = 0
s.protocol = 'http'
return s
}

exports.createSSLServer = function(port, opts) {
port = port || exports.portSSL

exports.createSSLServer = function(opts) {
var i
, options = { 'key' : path.join(__dirname, 'ssl', 'test.key')
, 'cert': path.join(__dirname, 'ssl', 'test.crt')
Expand All @@ -65,8 +64,11 @@ exports.createSSLServer = function(port, opts) {
var s = https.createServer(options, function (req, resp) {
s.emit(req.url, req, resp)
})
s.port = port
s.url = 'https://localhost:' + port
s.on('listening', function () {
s.port = this.address().port
s.url = 'https://localhost:' + s.port
})
s.port = 0
s.protocol = 'https'
return s
}
Expand Down
36 changes: 20 additions & 16 deletions tests/ssl/ca/localhost.js
Expand Up @@ -10,20 +10,24 @@ var server = https.createServer(options, function (req, res) {
res.end()
server.close()
})
server.listen(1337)
server.listen(0, function() {
var ca = fs.readFileSync('./ca.crt')
var agent = new https.Agent({
host: 'localhost',
port: this.address().port,
ca: ca
})

var ca = fs.readFileSync('./ca.crt')
var agent = new https.Agent({ host: 'localhost', port: 1337, ca: ca })

https.request({ host: 'localhost'
, method: 'HEAD'
, port: 1337
, agent: agent
, ca: [ ca ]
, path: '/' }, function (res) {
if (res.socket.authorized) {
console.log('node test: OK')
} else {
throw new Error(res.socket.authorizationError)
}
}).end()
https.request({ host: 'localhost'
, method: 'HEAD'
, port: this.address().port
, agent: agent
, ca: [ ca ]
, path: '/' }, function (res) {
if (res.socket.authorized) {
console.log('node test: OK')
} else {
throw new Error(res.socket.authorizationError)
}
}).end()
})
38 changes: 21 additions & 17 deletions tests/ssl/ca/server.js
Expand Up @@ -10,21 +10,25 @@ var server = https.createServer(options, function (req, res) {
res.end()
server.close()
})
server.listen(1337)
server.listen(0, function() {
var ca = fs.readFileSync('./ca.crt')
var agent = new https.Agent({
host: 'localhost',
port: this.address().port,
ca: ca
})

var ca = fs.readFileSync('./ca.crt')
var agent = new https.Agent({ host: 'localhost', port: 1337, ca: ca })

https.request({ host: 'localhost'
, method: 'HEAD'
, port: 1337
, headers: { host: 'testing.request.mikealrogers.com' }
, agent: agent
, ca: [ ca ]
, path: '/' }, function (res) {
if (res.socket.authorized) {
console.log('node test: OK')
} else {
throw new Error(res.socket.authorizationError)
}
}).end()
https.request({ host: 'localhost'
, method: 'HEAD'
, port: this.address().port
, headers: { host: 'testing.request.mikealrogers.com' }
, agent: agent
, ca: [ ca ]
, path: '/' }, function (res) {
if (res.socket.authorized) {
console.log('node test: OK')
} else {
throw new Error(res.socket.authorizationError)
}
}).end()
})
18 changes: 10 additions & 8 deletions tests/test-agent.js
Expand Up @@ -12,7 +12,9 @@ var s = http.createServer(function (req, res) {
})

tape('setup', function (t) {
s.listen(6767, function() {
s.listen(0, function() {
s.port = this.address().port
s.url = 'http://localhost:' + s.port
t.end()
})
})
Expand All @@ -25,8 +27,8 @@ function httpAgent (t, options, req) {
t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name')

var name = (typeof r.agent.getName === 'function')
? r.agent.getName({port:6767})
: 'localhost:6767' // node 0.10-
? r.agent.getName({port:s.port})
: 'localhost:' + s.port // node 0.10-
t.equal(r.agent.sockets[name].length, 1, '1 open socket')

var socket = r.agent.sockets[name][0]
Expand All @@ -44,7 +46,7 @@ function foreverAgent (t, options, req) {
t.ok(r.agent instanceof ForeverAgent, 'is ForeverAgent')
t.equal(Object.keys(r.agent.sockets).length, 1, '1 socket name')

var name = 'localhost:6767' // node 0.10-
var name = 'localhost:' + s.port // node 0.10-
t.equal(r.agent.sockets[name].length, 1, '1 open socket')

var socket = r.agent.sockets[name][0]
Expand All @@ -60,14 +62,14 @@ function foreverAgent (t, options, req) {

tape('options.agent', function (t) {
httpAgent(t, {
uri: 'http://localhost:6767',
uri: s.url,
agent: new http.Agent({keepAlive: true})
})
})

tape('options.agentClass + options.agentOptions', function (t) {
httpAgent(t, {
uri: 'http://localhost:6767',
uri: s.url,
agentClass: http.Agent,
agentOptions: {keepAlive: true}
})
Expand All @@ -78,7 +80,7 @@ tape('options.agentClass + options.agentOptions', function (t) {
tape('options.forever = true', function (t) {
var v = version()
var options = {
uri: 'http://localhost:6767',
uri: s.url,
forever: true
}

Expand All @@ -89,7 +91,7 @@ tape('options.forever = true', function (t) {
tape('forever() method', function (t) {
var v = version()
var options = {
uri: 'http://localhost:6767'
uri: s.url
}
var r = request.forever({maxSockets: 1})

Expand Down
20 changes: 10 additions & 10 deletions tests/test-agentOptions.js
Expand Up @@ -11,22 +11,23 @@ var request = require('../index')
, server = require('./server')
, tape = require('tape')

var s = server.createServer(function (req, resp) {
var s = server.createServer()

s.on('/', function (req, resp) {
resp.statusCode = 200
resp.end('')
})

tape('setup', function(t) {
s.listen(s.port, function() {
s.listen(0, function() {
t.end()
})
})

tape('without agentOptions should use global agent', function(t) {
var r = request(s.url, function(/*err, res, body*/) {
// TODO: figure out why err.code === 'ECONNREFUSED' on Travis?
//if (err) console.log(err)
//t.equal(err, null)
var r = request(s.url, function(err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.deepEqual(r.agent, http.globalAgent)
t.equal(Object.keys(r.pool).length, 0)
t.end()
Expand All @@ -36,10 +37,9 @@ tape('without agentOptions should use global agent', function(t) {
tape('with agentOptions should apply to new agent in pool', function(t) {
var r = request(s.url, {
agentOptions: { foo: 'bar' }
}, function(/*err, res, body*/) {
// TODO: figure out why err.code === 'ECONNREFUSED' on Travis?
//if (err) console.log(err)
//t.equal(err, null)
}, function(err, res, body) {
t.equal(err, null)
t.equal(res.statusCode, 200)
t.equal(r.agent.options.foo, 'bar')
t.equal(Object.keys(r.pool).length, 1)
t.end()
Expand Down
7 changes: 5 additions & 2 deletions tests/test-api.js
Expand Up @@ -12,12 +12,15 @@ tape('setup', function (t) {
res.writeHead(202)
req.pipe(res)
})
server.listen(6767, t.end)
server.listen(0, function() {
server.url = 'http://localhost:' + this.address().port
t.end()
})
})

tape('callback option', function (t) {
request({
url: 'http://localhost:6767',
url: server.url,
callback: function (err, res, body) {
t.equal(res.statusCode, 202)
t.end()
Expand Down
2 changes: 1 addition & 1 deletion tests/test-aws.js
Expand Up @@ -16,7 +16,7 @@ s.on(path, function(req, res) {
})

tape('setup', function(t) {
s.listen(s.port, function() {
s.listen(0, function() {
t.end()
})
})
Expand Down

0 comments on commit 53c9b98

Please sign in to comment.