2015-04-10 10:51:00 +03:00
|
|
|
"use strict";
|
2011-11-15 16:44:47 +04:00
|
|
|
var page = require('webpage').create();
|
|
|
|
var server = require('webserver').create();
|
2012-01-14 19:13:26 +04:00
|
|
|
var system = require('system');
|
2011-11-15 16:44:47 +04:00
|
|
|
var host, port;
|
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
if (system.args.length !== 2) {
|
2011-11-15 16:44:47 +04:00
|
|
|
console.log('Usage: server.js <some port>');
|
2012-05-16 11:37:26 +04:00
|
|
|
phantom.exit(1);
|
2011-11-15 16:44:47 +04:00
|
|
|
} else {
|
2012-01-14 19:13:26 +04:00
|
|
|
port = system.args[1];
|
2011-11-07 21:35:35 +04:00
|
|
|
var listening = server.listen(port, function (request, response) {
|
2011-11-07 19:16:54 +04:00
|
|
|
console.log("GOT HTTP REQUEST");
|
2012-02-15 16:52:57 +04:00
|
|
|
console.log(JSON.stringify(request, null, 4));
|
2011-11-07 19:16:54 +04:00
|
|
|
|
2011-11-07 20:33:35 +04:00
|
|
|
// we set the headers here
|
|
|
|
response.statusCode = 200;
|
|
|
|
response.headers = {"Cache": "no-cache", "Content-Type": "text/html"};
|
|
|
|
// this is also possible:
|
|
|
|
response.setHeader("foo", "bar");
|
|
|
|
// now we write the body
|
|
|
|
// note: the headers above will now be sent implictly
|
2011-12-20 06:18:08 +04:00
|
|
|
response.write("<html><head><title>YES!</title></head>");
|
2011-11-07 20:33:35 +04:00
|
|
|
// note: writeBody can be called multiple times
|
2011-12-20 06:18:08 +04:00
|
|
|
response.write("<body><p>pretty cool :)</body></html>");
|
2012-02-10 22:42:38 +04:00
|
|
|
response.close();
|
2011-11-15 16:44:47 +04:00
|
|
|
});
|
2011-11-07 21:35:35 +04:00
|
|
|
if (!listening) {
|
|
|
|
console.log("could not create web server listening on port " + port);
|
|
|
|
phantom.exit();
|
|
|
|
}
|
2011-11-07 19:16:54 +04:00
|
|
|
var url = "http://localhost:" + port + "/foo/bar.php?asdf=true";
|
2012-02-15 16:52:57 +04:00
|
|
|
console.log("SENDING REQUEST TO:");
|
2011-11-07 19:16:54 +04:00
|
|
|
console.log(url);
|
|
|
|
page.open(url, function (status) {
|
2011-11-15 16:44:47 +04:00
|
|
|
if (status !== 'success') {
|
|
|
|
console.log('FAIL to load the address');
|
|
|
|
} else {
|
2012-02-15 16:52:57 +04:00
|
|
|
console.log("GOT REPLY FROM SERVER:");
|
2011-11-05 19:38:08 +04:00
|
|
|
console.log(page.content);
|
2011-11-15 16:44:47 +04:00
|
|
|
}
|
|
|
|
phantom.exit();
|
|
|
|
});
|
2011-12-20 06:18:08 +04:00
|
|
|
}
|