Newer
Older
Lloyd Hilaiel
committed
// a little node webserver designed to run the unit tests herein
Lloyd Hilaiel
committed
var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
express = require("express"),
substitution = require('./libs/substitute.js'),
Lloyd Hilaiel
committed
configuration = require('./libs/configuration.js');
Lloyd Hilaiel
committed
var PRIMARY_HOST = "127.0.0.1";
var boundServers = [ ];
//
// this is the test harness, don't send emails, dump them to stdout
//
var nodemailer = require('nodemailer');
nodemailer.EmailMessage.prototype.send = function(callback) {
Lloyd Hilaiel
committed
this.prepareVariables();
var headers = this.generateHeaders(),
body = this.generateBody();
console.log(headers);
console.log(body);
Lloyd Hilaiel
committed
var subs = undefined;
function substitutionMiddleware(req, resp, next) {
if (!subs) {
subs = { };
for (var i = 0; i < boundServers.length; i++) {
var o = boundServers[i]
var a = o.server.address();
var from = o.name;
var to = "http://" + a.address + ":" + a.port;
subs[from] = to;
// now do another replacement to catch bare hostnames sans http(s)
// and explicit cases where port is appended
var fromWithPort;
if (from.substr(0,5) === 'https') {
Lloyd Hilaiel
committed
from = from.substr(8);
fromWithPort = from + ":443";
Lloyd Hilaiel
committed
} else {
Lloyd Hilaiel
committed
from = from.substr(7);
fromWithPort = from + ":80";
Lloyd Hilaiel
committed
}
to = to.substr(7);
if (o.subPath) to += o.subPath;
subs[fromWithPort] = to;
subs[from] = to;
Lloyd Hilaiel
committed
}
Lloyd Hilaiel
committed
(substitution.substitute(subs))(req, resp, next);
function createServer(obj) {
var app = express.createServer();
app.use(express.logger());
// this file is a *test* harness, to make it go, we'll insert a little
// handler that substitutes output, changing production URLs to
// developement URLs.
app.use(substitutionMiddleware);
// let the specific server interact directly with the express server to
// register their middleware, routes, etc...
if (obj.setup) obj.setup(app);
Lloyd Hilaiel
committed
// now set up the static resource servin'
var p = obj.path, ps = path.join(p, "static");
try { if (fs.statSync(ps).isDirectory()) p = ps; } catch(e) { }
app.use(express.static(p));
// and listen!
app.listen(obj.port, PRIMARY_HOST);
return app;
Lloyd Hilaiel
committed
};
// start up webservers on ephemeral ports for each subdirectory here.
Lloyd Hilaiel
committed
var dirs = [
// the reference verification server. A version is hosted at
// browserid.org and may be used, or the RP may perform their
// own verification.
{
name: "https://browserid.org/verify",
subPath: "/",
path: path.join(__dirname, "verifier")
},
// An example relying party.
{
name: "http://rp.eyedee.me",
path: path.join(__dirname, "rp")
},
// BrowserID: the secondary + ip + more.
{
name: "https://browserid.org",
path: path.join(__dirname, "browserid")
}
Lloyd Hilaiel
committed
];
Lloyd Hilaiel
committed
function formatLink(server, extraPath) {
var addr = server.address();
Lloyd Hilaiel
committed
var url = 'http://' + addr.address + ':' + addr.port;
if (extraPath) {
url += extraPath;
}
return url;
}
console.log("Running test servers:");
Lloyd Hilaiel
committed
dirs.forEach(function(dirObj) {
if (!fs.statSync(dirObj.path).isDirectory()) return;
Lloyd Hilaiel
committed
// does this server have a js handler for custom request handling?
var handlerPath = path.join(dirObj.path, "app.js");
Lloyd Hilaiel
committed
var runJS = {};
Lloyd Hilaiel
committed
try {
Lloyd Hilaiel
committed
var runJSExists = false;
try { runJSExists = fs.statSync(handlerPath).isFile() } catch(e) {};
if (runJSExists) runJS = require(handlerPath);
Lloyd Hilaiel
committed
} catch(e) {
Lloyd Hilaiel
committed
console.log("Error loading " + handlerPath + ": " + e);
process.exit(1);
Lloyd Hilaiel
committed
}
var so = {
Lloyd Hilaiel
committed
path: dirObj.path,
server: undefined,
Lloyd Hilaiel
committed
name: dirObj.name,
Lloyd Hilaiel
committed
handler: runJS.handler,
Lloyd Hilaiel
committed
setup: runJS.setup,
subPath: dirObj.subPath
};
so.server = createServer(so)
boundServers.push(so);
console.log(" " + dirObj.name + ": " + formatLink(so.server));