Commit 1d22ab37 1d22ab375edb2695172ce75d4d1116836f72edb0 by Nicolas Perriault

refs #188 - local response handling

1 parent 33361c8c
......@@ -190,6 +190,7 @@ Casper.prototype.back = function back() {
this.evaluate(function _evaluate() {
history.back();
});
this.currentResponse = this.resources.pop();
});
};
......@@ -1722,8 +1723,11 @@ function createPage(casper) {
return casper.filter('page.prompt', message, value);
};
page.onResourceReceived = function onResourceReceived(resource) {
require('http').augmentResponse(resource);
if (utils.isHTTPResource(resource)) {
require('http').augmentResponse(resource);
} else {
casper.log(f('Non-HTTP resource received from %s', resource.url), "debug");
}
casper.emit('resource.received', resource);
if (utils.isFunction(casper.options.onResourceReceived)) {
casper.options.onResourceReceived.call(casper, casper, resource);
......@@ -1732,13 +1736,17 @@ function createPage(casper) {
casper.resources.push(resource);
}
if (resource.url === casper.requestUrl && resource.stage === "end") {
casper.currentResponse = resource;
casper.currentHTTPStatus = /^http/i.test(resource.url) ? resource.status : null;
casper.emit('http.status.' + resource.status, resource);
if (utils.isObject(casper.options.httpStatusHandlers) &&
resource.status in casper.options.httpStatusHandlers &&
utils.isFunction(casper.options.httpStatusHandlers[resource.status])) {
casper.options.httpStatusHandlers[resource.status].call(casper, casper, resource);
casper.currentHTTPStatus = null;
casper.currentResponse = undefined;
if (utils.isHTTPResource(resource)) {
casper.currentResponse = resource;
casper.currentHTTPStatus = resource.status;
casper.emit('http.status.' + resource.status, resource);
if (utils.isObject(casper.options.httpStatusHandlers) &&
resource.status in casper.options.httpStatusHandlers &&
utils.isFunction(casper.options.httpStatusHandlers[resource.status])) {
casper.options.httpStatusHandlers[resource.status].call(casper, casper, resource);
}
}
casper.currentUrl = resource.url;
casper.emit('location.changed', resource.url);
......
......@@ -32,7 +32,7 @@
* Building an Array subclass
*/
function responseHeaders(){}
responseHeaders.prototype = new Array;
responseHeaders.prototype = [];
/**
* Retrieves a given header based on its name
......@@ -42,17 +42,14 @@ responseHeaders.prototype = new Array;
*/
responseHeaders.prototype.get = function get(name){
"use strict";
var headerValue = null;
name = name.toLowerCase();
this.some(function(header){
if (header.name.toLowerCase() === name){
headerValue = header.value;
return true;
}
});
return headerValue;
};
......@@ -62,7 +59,8 @@ responseHeaders.prototype.get = function get(name){
* @param mixed response Phantom response or undefined (generally with local files)
*/
exports.augmentResponse = function(response) {
if (response === undefined) return;
if (response === undefined) {
return;
}
response.headers.__proto__ = responseHeaders.prototype;
};
\ No newline at end of file
};
......
......@@ -250,6 +250,17 @@
exports.isFunction = isFunction;
/**
* Checks if passed resource involves an HTTP url.
*
* @param Object resource The PhantomJS HTTP resource object
* @return Boolean
*/
function isHTTPResource(resource) {
return isObject(resource) && /^http/i.test(resource.url);
}
exports.isHTTPResource = isHTTPResource;
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
* @param String file Path to the file to test
......
casper.test.comment('Casper.headers.get()');
var server = require('webserver').create();
var service = server.listen(8090, function (request, response) {
var service = server.listen(8090, function(request, response) {
response.statusCode = 200;
response.headers = {
'Content-Language': 'en',
......@@ -12,19 +12,20 @@ var service = server.listen(8090, function (request, response) {
response.close();
});
function dumpHeaders () {
function dumpHeaders() {
casper.test.comment('Dumping current response headers');
casper.currentResponse.headers.forEach(function (header) {
casper.currentResponse.headers.forEach(function(header) {
casper.test.comment('- ' + header.name + ': ' + header.value);
});
}
casper.start('tests/site/index.html', function thenLocalPage (response) {
// local file:// url
casper.start('file://' + phantom.casperPath + 'tests/site/index.html', function thenLocalPage(response) {
this.test.assertEquals(response, undefined, 'No response available on local page');
});
casper.thenOpen('http://localhost:8090/', function thenLocalhost (response) {
casper.thenOpen('http://localhost:8090/', function thenLocalhost(response) {
var headers = response.headers;
this.test.assertEquals(headers.get('Content-Language'), 'en', 'Checking existing header (case sensitive)');
......@@ -34,11 +35,11 @@ casper.thenOpen('http://localhost:8090/', function thenLocalhost (response) {
casper.back();
casper.then(function(response){
casper.then(function(response) {
this.test.assertEquals(response, undefined, 'Response should match the one of the previous step');
});
casper.run(function () {
casper.run(function() {
server.close();
this.test.done();
});
......
......@@ -6,7 +6,8 @@ var nsteps = casper.steps.length;
casper.then(function(response) {
this.test.assertTitle('CasperJS test index', 'Casper.then() added a new step');
this.test.assert(response === undefined, 'Casper.then() response is undefined in local mode');
require('utils').dump(response);
this.test.assertEquals(response, undefined, 'Casper.then() response is undefined in local mode');
});
casper.test.assertEquals(casper.steps.length, nsteps + 1, 'Casper.then() can add a new step');
......
......@@ -118,6 +118,22 @@ t.comment('isClipRect()');
});
})();
t.comment('isHTTPResource()');
(function() {
testCases = [
[{}, false],
[{url: 'file:///var/www/i.html'}, false],
[{url: 'mailto:plop@plop.com'}, false],
[{url: 'ftp://ftp.plop.com'}, false],
[{url: 'HTTP://plop.com/'}, true],
[{url: 'https://plop.com/'}, true]
];
testCases.forEach(function(testCase) {
t.assertEquals(utils.isHTTPResource(testCase[0]), testCase[1], 'isHTTPResource() checks for an HTTP resource');
});
})();
t.comment('isObject()');
(function() {
t.assertEquals(utils.isObject({}), true, 'isObject() checks for an Object');
......