Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

buster-server says "running" even if address is already in use #7

Merged
merged 1 commit into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/server-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,20 @@ module.exports = ServerCli.prototype = {
},

run: function (args, callback) {

callback = callback || function () {};
this.loadOptions();
this.cli.parseArgs(args, function (err, options) {
if (this.cli.loggedHelp) { return callback(); }
try {
var port = options["--port"].value;
var binding = options["--binding"].value;
var server = this.createServer(port, binding);
process.nextTick(function () {
var server = this.createServer(port, binding, function (err) {
if (err) {
this.handleError(err);
return callback(err);
}

var serverUrl = "http://localhost:" + port;
if(options['--capture-headless'].isSet) {
this.captureHeadlessBrowser(serverUrl);
Expand All @@ -61,19 +66,19 @@ module.exports = ServerCli.prototype = {
}.bind(this));
},

createServer: function (port, binding) {
createServer: function (port, binding, callback) {
var middleware, documentRoot = this.documentRoot;
var httpServer = http.createServer(function (req, res) {
if (middleware.respond(req, res)) { return; }
servePublicFiles(documentRoot, req, res);
});
httpServer.on("error", this.handleError.bind(this));
httpServer.listen(port, binding);
middleware = testServer.create(httpServer, {
logger: this.logger,
templateRoot: this.templateRoot,
name: this.name
});
httpServer.on("error", this.handleError.bind(this));
httpServer.listen(port, binding, callback);
return httpServer;
},

Expand Down
33 changes: 19 additions & 14 deletions test/server-cli-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* global require, process*/
"use strict";

var helper = require("./test-helper").requestHelperFor("localhost", "9999");
var cliHelper = require("buster-cli/test/test-helper");
var http = require("http");
Expand All @@ -7,7 +10,9 @@ var assert = buster.assert;
var refute = buster.refute;
var serverCli = require("../lib/server-cli");
var testServer = require("../lib/middleware");
var run = helper.runTest;
var createServerFunc = function(port, binding, callback){
callback(null);
};

buster.testCase("buster-server binary", {
setUp: function () {
Expand Down Expand Up @@ -40,15 +45,15 @@ buster.testCase("buster-server binary", {
},

"starts server on default port": function (done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
helper.run(this, [], done(function (err, server) {
assert.calledOnce(createServer);
assert.calledWith(createServer, 1111);
}));
},

"starts server on specified port": function (done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
helper.run(this, ["-p", "3200"], done(function () {
assert.calledOnce(createServer);
assert.calledWith(createServer, 3200);
Expand All @@ -65,38 +70,38 @@ buster.testCase("buster-server binary", {
},

"prints message if address is already in use (async)": function (done) {
var error = new Error("EADDRINUSE, Address already in use");
var server = bane.createEventEmitter();
server.listen = this.spy();
server.listen = this.spy(function(port, binding, callback){
callback(error);
});
this.stub(http, "createServer").returns(server);
this.stub(testServer, "create");

helper.run(this, ["-p", "3200"], done(function () {
assert.stderr("Address already in use. Pick another " +
"port with -p/--port to start buster-server");
}.bind(this)));

var error = new Error("EADDRINUSE, Address already in use");
server.emit("error", error);
},

"binds to specified address": function (done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
helper.run(this, ["-b", "0.0.0.0"], done(function () {
assert.calledOnce(createServer);
assert.calledWithExactly(createServer, 1111, "0.0.0.0");
assert.calledWith(createServer, 1111, "0.0.0.0");
}));
},

"binds to undefined when address not specified": function (done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
helper.run(this, [], done(function () {
assert.calledOnce(createServer);
assert.calledWithExactly(createServer, 1111, undefined);
assert.calledWith(createServer, 1111, undefined);
}));
},

"calls the function for capturing a headless browser if -c was passed": function(done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
var captureHeadlessBrowser = this.stub(this.cli, "captureHeadlessBrowser");

helper.run(this, ["-c"], done(function () {
Expand All @@ -106,7 +111,7 @@ buster.testCase("buster-server binary", {
},

"calls the function for capturing a headless browser if --capture-headless was passed": function(done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
var captureHeadlessBrowser = this.stub(this.cli, "captureHeadlessBrowser");

helper.run(this, ["--capture-headless"], done(function () {
Expand All @@ -116,7 +121,7 @@ buster.testCase("buster-server binary", {
},

"creates a phantom session if relevant parameter was passed": function(done) {
var createServer = this.stub(this.cli, "createServer");
var createServer = this.stub(this.cli, "createServer", createServerFunc);
var createPhantom = this.stub(this.cli.phantom, "create");

helper.run(this, ["-c"], done(function() {
Expand Down
14 changes: 14 additions & 0 deletions test/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
var http = require("http");
var ramp = require("ramp");
var buster = require("buster-node");

var helper = module.exports = {
// Mostly taken from buster-cli/test/test-helper.js
run: function (tc, args, callback) {
var aso = buster.assert.stdout, rso = buster.refute.stdout;

buster.refute.stdout = buster.assert.stdout = function (text) {
this.match(tc.stdout, text);
};

buster.refute.stderr = buster.assert.stderr = function (text) {
this.match(tc.stderr, text);
};

tc.cli.run(args, function (err, server) {
if (server && server.close) { server.close(); }
callback.call(tc, err, server);
buster.assert.stdout = aso;
buster.refute.stdout = rso;
});
},

Expand Down