2011-11-15 13:44:47 +01:00
|
|
|
var page = require('webpage').create();
|
|
|
|
|
var server = require('webserver').create();
|
2012-01-15 00:13:26 +09:00
|
|
|
var system = require('system');
|
2011-11-15 13:44:47 +01:00
|
|
|
var host, port;
|
|
|
|
|
|
2012-01-15 00:13:26 +09:00
|
|
|
if (system.args.length !== 2) {
|
2011-11-15 13:44:47 +01:00
|
|
|
console.log('Usage: server.js <some port>');
|
2012-05-16 09:37:26 +02:00
|
|
|
phantom.exit(1);
|
2011-11-15 13:44:47 +01:00
|
|
|
} else {
|
2012-01-15 00:13:26 +09:00
|
|
|
port = system.args[1];
|
2011-11-07 18:35:35 +01:00
|
|
|
var listening = server.listen(port, function (request, response) {
|
2011-11-07 16:16:54 +01:00
|
|
|
console.log("GOT HTTP REQUEST");
|
2012-02-15 13:52:57 +01:00
|
|
|
console.log(JSON.stringify(request, null, 4));
|
2011-11-07 16:16:54 +01:00
|
|
|
|
2011-11-07 17:33:35 +01: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-19 18:18:08 -08:00
|
|
|
response.write("<html><head><title>YES!</title></head>");
|
2011-11-07 17:33:35 +01:00
|
|
|
// note: writeBody can be called multiple times
|
2011-12-19 18:18:08 -08:00
|
|
|
response.write("<body><p>pretty cool :)</body></html>");
|
2012-02-10 13:42:38 -05:00
|
|
|
response.close();
|
2011-11-15 13:44:47 +01:00
|
|
|
});
|
2011-11-07 18:35:35 +01:00
|
|
|
if (!listening) {
|
|
|
|
|
console.log("could not create web server listening on port " + port);
|
|
|
|
|
phantom.exit();
|
|
|
|
|
}
|
2011-11-07 16:16:54 +01:00
|
|
|
var url = "http://localhost:" + port + "/foo/bar.php?asdf=true";
|
2012-02-15 13:52:57 +01:00
|
|
|
console.log("SENDING REQUEST TO:");
|
2011-11-07 16:16:54 +01:00
|
|
|
console.log(url);
|
|
|
|
|
page.open(url, function (status) {
|
2011-11-15 13:44:47 +01:00
|
|
|
if (status !== 'success') {
|
|
|
|
|
console.log('FAIL to load the address');
|
|
|
|
|
} else {
|
2012-02-15 13:52:57 +01:00
|
|
|
console.log("GOT REPLY FROM SERVER:");
|
2011-11-05 16:38:08 +01:00
|
|
|
console.log(page.content);
|
2011-11-15 13:44:47 +01:00
|
|
|
}
|
|
|
|
|
phantom.exit();
|
|
|
|
|
});
|
2011-12-19 18:18:08 -08:00
|
|
|
}
|