2011-11-07 21:25:57 +04:00
|
|
|
describe("WebServer constructor", function() {
|
2011-12-23 09:27:00 +04:00
|
|
|
it("should not exist in window", function() {
|
|
|
|
expect(window.hasOwnProperty('WebServer')).toBeFalsy();
|
2011-11-07 21:25:57 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be a function", function() {
|
2011-12-23 09:27:00 +04:00
|
|
|
var WebServer = require('webserver').create;
|
|
|
|
expect(typeof WebServer).toEqual('function');
|
2011-11-07 21:25:57 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-02-15 16:52:57 +04:00
|
|
|
var expectedPostData = false;
|
|
|
|
|
2011-11-15 15:28:22 +04:00
|
|
|
function checkRequest(request, response) {
|
|
|
|
expect(typeof request).toEqual('object');
|
2012-02-15 16:52:57 +04:00
|
|
|
expect(request.hasOwnProperty('url')).toBeTruthy();
|
|
|
|
expect(request.hasOwnProperty('method')).toBeTruthy();
|
|
|
|
expect(request.hasOwnProperty('httpVersion')).toBeTruthy();
|
|
|
|
expect(request.hasOwnProperty('headers')).toBeTruthy();
|
|
|
|
expect(typeof request.headers).toEqual('object');
|
2011-11-15 15:28:22 +04:00
|
|
|
|
|
|
|
expect(typeof response).toEqual('object');
|
2012-02-15 16:52:57 +04:00
|
|
|
expect(response.hasOwnProperty('statusCode')).toBeTruthy();
|
|
|
|
expect(response.hasOwnProperty('headers')).toBeTruthy();
|
|
|
|
expect(typeof response['setHeaders']).toEqual('function');
|
|
|
|
expect(typeof response['setHeader']).toEqual('function');
|
|
|
|
expect(typeof response['header']).toEqual('function');
|
|
|
|
expect(typeof response['write']).toEqual('function');
|
|
|
|
expect(typeof response['writeHead']).toEqual('function');
|
|
|
|
|
|
|
|
if (expectedPostData !== false) {
|
|
|
|
expect(request.method).toEqual("POST");
|
|
|
|
expect(request.hasOwnProperty('post')).toBeTruthy();
|
2012-03-23 14:56:25 +04:00
|
|
|
console.log("request.post => " + JSON.stringify(request.post, null, 4));
|
|
|
|
console.log("expectedPostData => " + JSON.stringify(expectedPostData, null, 4));
|
|
|
|
console.log("request.headers => " + JSON.stringify(request.headers, null, 4));
|
|
|
|
if (request.headers["Content-Type"] && request.headers["Content-Type"] === "application/x-www-form-urlencoded") {
|
2012-03-23 15:48:16 +04:00
|
|
|
expect(typeof request.post).toEqual('object');
|
|
|
|
expect(request.post).toEqual(expectedPostData);
|
2012-03-23 14:56:25 +04:00
|
|
|
expect(request.hasOwnProperty('postRaw')).toBeTruthy();
|
|
|
|
expect(typeof request.postRaw).toEqual('string');
|
2012-03-23 15:48:16 +04:00
|
|
|
} else {
|
|
|
|
expect(typeof request.post).toEqual('string');
|
|
|
|
expect(request.post).toNotEqual(expectedPostData);
|
|
|
|
expect(request.hasOwnProperty('postRaw')).toBeFalsy();
|
2012-03-23 14:56:25 +04:00
|
|
|
}
|
2012-02-15 16:52:57 +04:00
|
|
|
expectedPostData = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.write("request handled");
|
|
|
|
response.close();
|
2011-11-15 15:28:22 +04:00
|
|
|
}
|
|
|
|
|
2011-11-07 21:25:57 +04:00
|
|
|
describe("WebServer object", function() {
|
2011-12-23 09:27:00 +04:00
|
|
|
var server = require('webserver').create();
|
2011-11-07 21:25:57 +04:00
|
|
|
|
|
|
|
it("should be creatable", function() {
|
|
|
|
expect(typeof server).toEqual('object');
|
|
|
|
expect(server).toNotEqual(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should have objectName as 'WebServer'", function() {
|
|
|
|
expect(server.objectName).toEqual('WebServer');
|
|
|
|
});
|
|
|
|
|
|
|
|
expectHasProperty(server, 'port');
|
|
|
|
it("should have port as string", function() {
|
|
|
|
expect(typeof server.port).toEqual('string');
|
|
|
|
});
|
|
|
|
it("should not listen to any port by default", function() {
|
|
|
|
expect(server.port).toEqual("");
|
|
|
|
});
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
expectHasProperty(page, 'settings');
|
|
|
|
it("should have non-empty settings", function() {
|
|
|
|
expect(page.settings).toNotEqual(null);
|
|
|
|
expect(page.settings).toNotEqual({});
|
|
|
|
});
|
|
|
|
*/
|
|
|
|
expectHasFunction(server, 'listenOnPort');
|
|
|
|
expectHasFunction(server, 'newRequest');
|
|
|
|
expectHasFunction(server, 'close');
|
2011-11-07 21:35:35 +04:00
|
|
|
|
|
|
|
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...
|
2012-03-23 14:56:25 +04:00
|
|
|
expect(server.listen("12345", checkRequest)).toEqual(true);
|
2011-11-07 21:35:35 +04:00
|
|
|
expect(server.port).toEqual("12345");
|
|
|
|
});
|
2011-11-15 15:28:22 +04:00
|
|
|
|
|
|
|
it("should handle requests", function() {
|
|
|
|
var page = require('webpage').create();
|
|
|
|
var url = "http://localhost:12345/foo/bar.php?asdf=true";
|
2012-02-15 16:52:57 +04:00
|
|
|
var handled = false;
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(false);
|
|
|
|
page.open(url, function (status) {
|
|
|
|
expect(status == 'success').toEqual(true);
|
|
|
|
expect(page.plainText).toEqual("request handled");
|
|
|
|
handled = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waits(50);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2012-03-23 15:48:16 +04:00
|
|
|
it("should handle post requests ('Content-Type' = 'application/x-www-form-urlencoded')", function() {
|
2012-02-15 16:52:57 +04:00
|
|
|
var page = require('webpage').create();
|
|
|
|
var url = "http://localhost:12345/foo/bar.txt?asdf=true";
|
|
|
|
//note: sorted by key (map)
|
|
|
|
expectedPostData = {'answer' : "42", 'universe' : "expanding"};
|
|
|
|
var handled = false;
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(false);
|
2012-03-23 15:48:16 +04:00
|
|
|
page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/x-www-form-urlencoded" }, function (status) {
|
|
|
|
expect(status == 'success').toEqual(true);
|
|
|
|
expect(page.plainText).toEqual("request handled");
|
|
|
|
handled = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waits(50);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should handle post requests ('Content-Type' = 'ANY')", function() {
|
|
|
|
var page = require('webpage').create();
|
|
|
|
var url = "http://localhost:12345/foo/bar.txt?asdf=true";
|
|
|
|
//note: sorted by key (map)
|
|
|
|
expectedPostData = {'answer' : "42", 'universe' : "expanding"};
|
|
|
|
var handled = false;
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(false);
|
|
|
|
page.open(url, 'post', "universe=expanding&answer=42", { "Content-Type" : "application/json;charset=UTF-8" }, function (status) {
|
2012-02-15 16:52:57 +04:00
|
|
|
expect(status == 'success').toEqual(true);
|
|
|
|
expect(page.plainText).toEqual("request handled");
|
|
|
|
handled = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
waits(50);
|
|
|
|
|
|
|
|
runs(function() {
|
|
|
|
expect(handled).toEqual(true);
|
2011-11-15 15:28:22 +04:00
|
|
|
});
|
|
|
|
});
|
2011-11-07 21:25:57 +04:00
|
|
|
});
|