Commit 29b6c486 29b6c486a68f4d4d8561c8dd1e02141d8c6ed87e by Nicolas Perriault

fixed #274 - some headers couldn't be set

1 parent 6e24bd94
......@@ -936,6 +936,7 @@ Casper.prototype.handleReceivedResource = function(resource) {
this.currentHTTPStatus = null;
this.currentResponse = undefined;
if (utils.isHTTPResource(resource)) {
this.emit('page.resource.received', resource);
this.currentResponse = resource;
this.currentHTTPStatus = resource.status;
this.emit('http.status.' + resource.status, resource);
......@@ -1100,7 +1101,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
*
* - String method: The HTTP method to use
* - Object data: The data to use to perform the request, eg. {foo: 'bar'}
* - Array headers: An array of request headers, eg. [{'Cache-Control': 'max-age=0'}]
* - Object headers: Custom request headers object, eg. {'Cache-Control': 'max-age=0'}
*
* @param String location The url to open
* @param Object settings The request settings (optional)
......@@ -1108,8 +1109,11 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
*/
Casper.prototype.open = function open(location, settings) {
"use strict";
var baseCustomHeaders = this.page.customHeaders,
customHeaders = settings && settings.headers || {};
this.checkStarted();
settings = utils.isObject(settings) ? settings : { method: "get" };
settings = utils.isObject(settings) ? settings : {};
settings.method = settings.method || "get";
// http method
// taken from https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L302
var methods = ["get", "head", "put", "post", "delete"];
......@@ -1131,12 +1135,17 @@ Casper.prototype.open = function open(location, settings) {
this.requestUrl = this.filter('open.location', location) || location;
this.emit('open', this.requestUrl, settings);
this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug");
// reset resources
this.resources = [];
// custom headers
this.page.customHeaders = utils.mergeObjects(utils.clone(baseCustomHeaders), customHeaders);
// perfom request
this.page.openUrl(this.requestUrl, {
operation: settings.method,
data: settings.data,
headers: settings.headers
data: settings.data
}, this.page.settings);
this.resources = [];
// revert base custom headers
this.page.customHeaders = baseCustomHeaders;
return this;
};
......@@ -1299,13 +1308,8 @@ Casper.prototype.start = function start(location, then) {
this.log(f("Unknown log level '%d', defaulting to 'warning'", this.options.logLevel), "warning");
this.options.logLevel = "warning";
}
// WebPage
if (!utils.isWebPage(this.page)) {
if (utils.isWebPage(this.options.page)) {
this.page = this.options.page;
} else {
this.page = createPage(this);
}
this.page = utils.isWebPage(this.options.page) ? this.options.page : createPage(this);
}
this.page.settings = utils.mergeObjects(this.page.settings, this.options.pageSettings);
if (utils.isClipRect(this.options.clipRect)) {
......@@ -1869,6 +1873,9 @@ function createPage(casper) {
};
page.onResourceRequested = function onResourceRequested(request) {
casper.emit('resource.requested', request);
if (request.url === casper.requestUrl) {
casper.emit('page.resource.requested', request);
}
if (utils.isFunction(casper.options.onResourceRequested)) {
casper.options.onResourceRequested.call(casper, casper, request);
}
......
......@@ -70,6 +70,18 @@ function cleanUrl(url) {
exports.cleanUrl = cleanUrl;
/**
* Clones an object.
*
* @param Mixed o
* @return Mixed
*/
function clone(o) {
"use strict";
return JSON.parse(JSON.stringify(o));
}
exports.clone = clone;
/**
* Dumps a JSON representation of passed value to the console. Used for
* debugging purpose only.
*
......@@ -456,7 +468,7 @@ exports.mergeObjects = mergeObjects;
*/
function node(name, attributes) {
"use strict";
var _node = document.createElement(name);
var _node = document.createElement(name);
for (var attrName in attributes) {
var value = attributes[attrName];
if (attributes.hasOwnProperty(attrName) && isString(attrName)) {
......
/*global casper*/
/*jshint strict:false*/
function testHeader(header) {
return header.name === 'Accept' && header.value === 'application/json';
}
var t = casper.test, current = 0, tests = [
function(request) {
t.assertNot(request.headers.some(testHeader), "Casper.open() sets no custom header by default");
},
function(request) {
t.assert(request.headers.some(testHeader), "Casper.open() can set a custom header");
},
function(request) {
t.assertNot(request.headers.some(testHeader), "Casper.open() custom headers option is not persistent");
},
];
casper.on('page.resource.requested', function(request) {
tests[current++](request);
});
casper.start();
casper.thenOpen('tests/site/index.html');
casper.thenOpen('tests/site/index.html', {
headers: {
Accept: 'application/json'
}
});
casper.thenOpen('tests/site/index.html');
casper.run(function() {
this.removeAllListeners('page.resource.requested');
t.done();
});
......@@ -23,6 +23,14 @@ t.comment('cleanUrl()');
}
})();
t.comment('clone()');
(function() {
var a = {a: 1, b: 2, c: [1, 2]};
t.assertEquals(utils.clone(a), a);
var b = [1, 2, 3, a];
t.assertEquals(utils.clone(b), b);
})();
t.comment('equals()');
(function() {
t.assert(utils.equals(null, null), 'equals() null equality');
......