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

Commit

Permalink
waits now until headless browser is started before calling callback o…
Browse files Browse the repository at this point in the history
…f run, needed for buster-ci
  • Loading branch information
dkl-ppi committed Mar 12, 2015
1 parent efac14d commit 6cf0b3f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 34 deletions.
25 changes: 16 additions & 9 deletions lib/server-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ module.exports = ServerCli.prototype = {
}

var serverUrl = "http://localhost:" + port;
if(options['--capture-headless'].isSet) {
this.captureHeadlessBrowser(serverUrl);
}
callback(null, server);
this.logger.log(this.binary + " running on " + serverUrl);
if (options['--capture-headless'].isSet) {
this.captureHeadlessBrowser(serverUrl, function () {
callback(null, server);
}.bind(this));
} else {
callback(null, server);
}
}.bind(this));
} catch (e) {
this.handleError(e);
Expand All @@ -82,15 +85,18 @@ module.exports = ServerCli.prototype = {
return httpServer;
},

captureHeadlessBrowser: function(serverUrl) {
captureHeadlessBrowser: function (serverUrl, cb) {
this.logger.log('Starting headless browser...');
this.phantom.create(function(proxy) {
proxy.page.open(serverUrl + '/capture', function(success) {
this.phantom.create(function (proxy) {
proxy.page.open(serverUrl + '/capture', function (success) {
if (success) {
this.logger.log('Browser was captured.');
} else {
this.logger.log('Browser was not captured. Something went wrong :-(')
this.logger.log(
'Browser was not captured. Something went wrong :-('
);
}
cb();
}.bind(this));
}.bind(this));
},
Expand All @@ -109,7 +115,8 @@ module.exports = ServerCli.prototype = {
});

this.cli.opt(['-c', '--capture-headless'], {
description: "Captures a headless webkit browser after the server was started."
description: "Captures a headless webkit browser " +
"after the server was started."
});
},

Expand Down
87 changes: 62 additions & 25 deletions test/server-cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var assert = buster.assert;
var refute = buster.refute;
var serverCli = require("../lib/server-cli");
var testServer = require("../lib/middleware");
var createServerFunc = function(port, binding, callback){
var createServerFunc = function (port, binding, callback) {
callback(null);
};

Expand Down Expand Up @@ -45,15 +45,17 @@ buster.testCase("buster-server binary", {
},

"starts server on default port": function (done) {
var createServer = this.stub(this.cli, "createServer", createServerFunc);
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", createServerFunc);
var createServer = this.stub(this.cli, "createServer",
createServerFunc);
helper.run(this, ["-p", "3200"], done(function () {
assert.calledOnce(createServer);
assert.calledWith(createServer, 3200);
Expand All @@ -72,7 +74,7 @@ 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(function(port, binding, callback){
server.listen = this.spy(function (port, binding, callback) {
callback(error);
});
this.stub(http, "createServer").returns(server);
Expand All @@ -85,48 +87,83 @@ buster.testCase("buster-server binary", {
},

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

"binds to undefined when address not specified": function (done) {
var createServer = this.stub(this.cli, "createServer", createServerFunc);
var createServer = this.stub(this.cli, "createServer",
createServerFunc);
helper.run(this, [], done(function () {
assert.calledOnce(createServer);
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", createServerFunc);
var captureHeadlessBrowser = this.stub(this.cli, "captureHeadlessBrowser");
"captures headless browser if -c was passed":
function () {
var createServer = this.stub(this.cli, "createServer",
createServerFunc);
var captureHeadlessBrowser = this.stub(this.cli,
"captureHeadlessBrowser");

helper.run(this, ["-c"]);

helper.run(this, ["-c"], done(function () {
assert.calledOnce(captureHeadlessBrowser);
assert.calledWithExactly(captureHeadlessBrowser, "http://localhost:1111");
}));
},
assert.calledWithExactly(captureHeadlessBrowser,
"http://localhost:1111", buster.sinon.match.func);
},

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

helper.run(this, ["--capture-headless"]);

helper.run(this, ["--capture-headless"], done(function () {
assert.calledOnce(captureHeadlessBrowser);
assert.calledWithExactly(captureHeadlessBrowser, "http://localhost:1111");
}));
},
assert.calledWithExactly(captureHeadlessBrowser,
"http://localhost:1111", buster.sinon.match.func);
},

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

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

helper.run(this, ["-c"], done(function() {
assert.calledOnce(createPhantom);
}));
},

"waits until capture page responses": function () {
var createServer = this.stub(this.cli, "createServer",
createServerFunc);
var responseCallback;
var createPhantom = this.stub(this.cli.phantom, "create",
function (cb) {
var proxy = {
page: {
open: function (url, cb) {
responseCallback = cb;
}
}
};
cb(proxy);
});
var cb = this.stub();

helper.run(this, ["-c"], cb);
refute.called(cb);
responseCallback();
assert.calledOnce(cb);
}
},

Expand Down

0 comments on commit 6cf0b3f

Please sign in to comment.