Skip to content

Commit

Permalink
make test-server harness more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Jul 4, 2015
1 parent e7f7ac8 commit 3604c0b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
3 changes: 0 additions & 3 deletions test/lib/https-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var node8 = require('semver').lt(process.version, '0.9.0');

function addServerOptions(options) {
options = options || {};

//options.ca = [fs.readFileSync(__dirname + '/TestCA.crt')];
options.cert = fs.readFileSync(__dirname + '/TestServer.crt');
options.key = fs.readFileSync(__dirname + '/TestServer.pem') ;
Expand All @@ -13,7 +12,6 @@ function addServerOptions(options) {

function addClientOptions(options) {
options = options || {};

options.ca = [fs.readFileSync(__dirname + '/TestCA.crt')];
//options.cert = fs.readFileSync(__dirname + '/TestClient.crt');
//options.key = fs.readFileSync(__dirname + '/TestClient.pem') ;
Expand All @@ -33,7 +31,6 @@ function deleteOptions(options) {
delete options.key;
}


function makeRequest(options, cb, res) {
if (options.protocol === 'https:') {
addClientOptions(options);
Expand Down
75 changes: 41 additions & 34 deletions test/lib/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,52 @@
var Promise = require('bluebird');
var http = require('http');
var https = require('https');
var config = require('./https-config');
var assert = require('assert');

var servers = {};
module.exports = function(defaultPorts) {
defaultPorts = defaultPorts || {};
var cache = [];

var serverPorts = {
http: 3600,
https: 3601
};

function start(app, proto, httpsOptions) {
proto = proto || 'http';
if (proto !== 'http' && proto !== 'https') {
throw new Error('proto must be null, http, or https. got: ' + proto);
function start(options) {
return Promise.fromNode(function(callback) {
if (typeof options === 'function') {
options = {
app: options
};
}
assert(typeof options.app, 'function', 'app');
var server, port;
var protocol = options.protocol;
if (!protocol || protocol.match(/^http(:)?$/)) {
server = http.createServer(options.app);
port = options.port || defaultPorts.http;
} else if (protocol.match(/^https(:)?$/)) {
server = https.createServer(options, options.app);
port = options.port || defaultPorts.https;
}
assert(typeof port, 'number', 'port');
addServer(server);
server.listen(port, callback);
});
}
return Promise.fromNode(function(callback) {
if (proto === 'http') {
servers[proto] = http.createServer(app);
} else {
servers[proto] = https.createServer(config.addServerOptions({}), app);
}
servers[proto].listen(serverPorts[proto], callback);
});
}

function stop() {
return Promise.all([_stop('http'), _stop('https')]);
}
function stop() {
return Promise.all(cache.map(function(_stop){return _stop();})).finally(function(){
cache = [];
});
}

function _stop(proto) {
if (!servers[proto]) {
return Promise.resolve(proto + ' not running');
function addServer(server) {
cache.push(function () {
return Promise.fromNode(function(callback) {
server.close(callback);
});
});
return server;
}
return Promise.fromNode(function(callback) {
servers[proto].close(callback);
servers[proto] = null;
});
}

module.exports = {
start: start,
stop: stop
return {
start: start,
stop: stop
};
};
20 changes: 15 additions & 5 deletions test/test-with-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ describe('follow-redirects ', function() {
var express = require('express');
var concat = require('concat-stream');
var assert = require('assert');
var server = require('./lib/test-server');
var server = require('./lib/test-server')({
https: 3601,
http: 3600
});
var url = require('url');
var followRedirects = require('..');
var http = followRedirects.http;
Expand All @@ -14,7 +17,14 @@ describe('follow-redirects ', function() {
var redirectsTo = util.redirectsTo;
var sendsJson = util.sendsJson;
var asPromise = util.asPromise;
var makeRequest = require('./lib/https-config').makeRequest;
var config = require('./lib/https-config');
var makeRequest = config.makeRequest;
function httpsOptions(app) {
return config.addServerOptions({
app: app,
protocol: 'https'
});
}

var app, app2, originalMaxRedirects;

Expand Down Expand Up @@ -91,7 +101,7 @@ describe('follow-redirects ', function() {
app.get('/b', redirectsTo('/c'));
app.get('/c', sendsJson({baz:'quz'}));

server.start(app, 'https')
server.start(httpsOptions(app))
.then(asPromise(function(resolve, reject){
var opts = url.parse('https://localhost:3601/a');
opts.makeRequest = makeRequest;
Expand Down Expand Up @@ -184,7 +194,7 @@ describe('follow-redirects ', function() {
app2.get('/b', redirectsTo('https://localhost:3601/c'));
app.get('/c', sendsJson({yes:'no'}));

Promise.all([server.start(app,'https'), server.start(app2)])
Promise.all([server.start(httpsOptions(app)), server.start(app2)])
.then(asPromise(function(resolve, reject){
var opts = url.parse('https://localhost:3601/a');
opts.makeRequest = makeRequest;
Expand All @@ -201,7 +211,7 @@ describe('follow-redirects ', function() {
app2.get('/b', redirectsTo('http://localhost:3600/c'));
app.get('/c', sendsJson({hello:'goodbye'}));

Promise.all([server.start(app), server.start(app2, 'https')])
Promise.all([server.start(app), server.start(httpsOptions(app2))])
.then(asPromise(function(resolve, reject){
var opts = url.parse('http://localhost:3600/a');
opts.makeRequest = makeRequest;
Expand Down

0 comments on commit 3604c0b

Please sign in to comment.