mirror of https://github.com/vitalif/phantomjs
basic unit testing for WebServer.listen
parent
5eaf705955
commit
bd2052631f
|
@ -7,7 +7,7 @@ if (phantom.args.length !== 1) {
|
|||
phantom.exit();
|
||||
} else {
|
||||
port = phantom.args[0];
|
||||
server.listen(port, function (request, response) {
|
||||
var listening = server.listen(port, function (request, response) {
|
||||
console.log("GOT HTTP REQUEST");
|
||||
console.log("request.url = " + request.url);
|
||||
console.log("request.queryString = " + request.queryString);
|
||||
|
@ -35,6 +35,10 @@ if (phantom.args.length !== 1) {
|
|||
// note: writeBody can be called multiple times
|
||||
response.writeBody("<body><p>pretty cool :)</body></html>");
|
||||
});
|
||||
if (!listening) {
|
||||
console.log("could not create web server listening on port " + port);
|
||||
phantom.exit();
|
||||
}
|
||||
var url = "http://localhost:" + port + "/foo/bar.php?asdf=true";
|
||||
console.log(url);
|
||||
page.open(url, function (status) {
|
||||
|
|
|
@ -116,8 +116,7 @@ exports.create = function (opts) {
|
|||
if (arguments.length === 2 && typeof handler === 'function') {
|
||||
this.onNewRequest = handler;
|
||||
//TODO: settings?
|
||||
this.listenOnPort(port);
|
||||
return;
|
||||
return this.listenOnPort(port);
|
||||
}
|
||||
throw "Wrong use of WebServer#listen";
|
||||
};
|
||||
|
|
|
@ -73,23 +73,24 @@ WebServer::~WebServer()
|
|||
close();
|
||||
}
|
||||
|
||||
void WebServer::listenOnPort(const QString& port)
|
||||
bool WebServer::listenOnPort(const QString& port)
|
||||
{
|
||||
///TODO: listen on multiple ports?
|
||||
close();
|
||||
m_port = port;
|
||||
|
||||
const char *options[] = {"listening_ports", qstrdup(qPrintable(port)), NULL};
|
||||
///TODO: more options from m_config?
|
||||
m_ctx = mg_start(&callback, this, options);
|
||||
if (!m_ctx) {
|
||||
qWarning() << "could not create web server connection on port" << port;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_port = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
QString WebServer::port() const
|
||||
{
|
||||
qWarning() << "port = " << m_port;
|
||||
return m_port;
|
||||
}
|
||||
|
||||
|
|
|
@ -64,9 +64,11 @@ public slots:
|
|||
* For each new request @c handleRequest() will be called which
|
||||
* in turn emits @c newRequest() where appropriate.
|
||||
*
|
||||
* @return true if we can listen on @p port, false otherwise.
|
||||
*
|
||||
* WARNING: must not be the same name as in the javascript api...
|
||||
*/
|
||||
void listenOnPort(const QString &port);
|
||||
bool listenOnPort(const QString &port);
|
||||
/**
|
||||
* @return the port this server is listening on
|
||||
* or an empty string if the server is closed.
|
||||
|
|
|
@ -39,4 +39,15 @@ describe("WebServer object", function() {
|
|||
expectHasFunction(server, 'listenOnPort');
|
||||
expectHasFunction(server, 'newRequest');
|
||||
expectHasFunction(server, 'close');
|
||||
|
||||
it("should fail to listen to blocked ports", function() {
|
||||
//NOTE: is this really blocked everywhere?
|
||||
expect(server.listen(1, function(){})).toEqual(false);
|
||||
expect(server.port).toEqual("");
|
||||
});
|
||||
it("should be able to listen to some port", function() {
|
||||
//NOTE: this can fail if the port is already being listend on...
|
||||
expect(server.listen(12345, function() {})).toEqual(true);
|
||||
expect(server.port).toEqual("12345");
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue