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) { ...@@ -936,6 +936,7 @@ Casper.prototype.handleReceivedResource = function(resource) {
936 this.currentHTTPStatus = null; 936 this.currentHTTPStatus = null;
937 this.currentResponse = undefined; 937 this.currentResponse = undefined;
938 if (utils.isHTTPResource(resource)) { 938 if (utils.isHTTPResource(resource)) {
939 this.emit('page.resource.received', resource);
939 this.currentResponse = resource; 940 this.currentResponse = resource;
940 this.currentHTTPStatus = resource.status; 941 this.currentHTTPStatus = resource.status;
941 this.emit('http.status.' + resource.status, resource); 942 this.emit('http.status.' + resource.status, resource);
...@@ -1100,7 +1101,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { ...@@ -1100,7 +1101,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
1100 * 1101 *
1101 * - String method: The HTTP method to use 1102 * - String method: The HTTP method to use
1102 * - Object data: The data to use to perform the request, eg. {foo: 'bar'} 1103 * - Object data: The data to use to perform the request, eg. {foo: 'bar'}
1103 * - Array headers: An array of request headers, eg. [{'Cache-Control': 'max-age=0'}] 1104 * - Object headers: Custom request headers object, eg. {'Cache-Control': 'max-age=0'}
1104 * 1105 *
1105 * @param String location The url to open 1106 * @param String location The url to open
1106 * @param Object settings The request settings (optional) 1107 * @param Object settings The request settings (optional)
...@@ -1108,8 +1109,11 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { ...@@ -1108,8 +1109,11 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
1108 */ 1109 */
1109 Casper.prototype.open = function open(location, settings) { 1110 Casper.prototype.open = function open(location, settings) {
1110 "use strict"; 1111 "use strict";
1112 var baseCustomHeaders = this.page.customHeaders,
1113 customHeaders = settings && settings.headers || {};
1111 this.checkStarted(); 1114 this.checkStarted();
1112 settings = utils.isObject(settings) ? settings : { method: "get" }; 1115 settings = utils.isObject(settings) ? settings : {};
1116 settings.method = settings.method || "get";
1113 // http method 1117 // http method
1114 // taken from https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L302 1118 // taken from https://github.com/ariya/phantomjs/blob/master/src/webpage.cpp#L302
1115 var methods = ["get", "head", "put", "post", "delete"]; 1119 var methods = ["get", "head", "put", "post", "delete"];
...@@ -1131,12 +1135,17 @@ Casper.prototype.open = function open(location, settings) { ...@@ -1131,12 +1135,17 @@ Casper.prototype.open = function open(location, settings) {
1131 this.requestUrl = this.filter('open.location', location) || location; 1135 this.requestUrl = this.filter('open.location', location) || location;
1132 this.emit('open', this.requestUrl, settings); 1136 this.emit('open', this.requestUrl, settings);
1133 this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug"); 1137 this.log(f('opening url: %s, HTTP %s', this.requestUrl, settings.method.toUpperCase()), "debug");
1138 // reset resources
1139 this.resources = [];
1140 // custom headers
1141 this.page.customHeaders = utils.mergeObjects(utils.clone(baseCustomHeaders), customHeaders);
1142 // perfom request
1134 this.page.openUrl(this.requestUrl, { 1143 this.page.openUrl(this.requestUrl, {
1135 operation: settings.method, 1144 operation: settings.method,
1136 data: settings.data, 1145 data: settings.data
1137 headers: settings.headers
1138 }, this.page.settings); 1146 }, this.page.settings);
1139 this.resources = []; 1147 // revert base custom headers
1148 this.page.customHeaders = baseCustomHeaders;
1140 return this; 1149 return this;
1141 }; 1150 };
1142 1151
...@@ -1299,13 +1308,8 @@ Casper.prototype.start = function start(location, then) { ...@@ -1299,13 +1308,8 @@ Casper.prototype.start = function start(location, then) {
1299 this.log(f("Unknown log level '%d', defaulting to 'warning'", this.options.logLevel), "warning"); 1308 this.log(f("Unknown log level '%d', defaulting to 'warning'", this.options.logLevel), "warning");
1300 this.options.logLevel = "warning"; 1309 this.options.logLevel = "warning";
1301 } 1310 }
1302 // WebPage
1303 if (!utils.isWebPage(this.page)) { 1311 if (!utils.isWebPage(this.page)) {
1304 if (utils.isWebPage(this.options.page)) { 1312 this.page = utils.isWebPage(this.options.page) ? this.options.page : createPage(this);
1305 this.page = this.options.page;
1306 } else {
1307 this.page = createPage(this);
1308 }
1309 } 1313 }
1310 this.page.settings = utils.mergeObjects(this.page.settings, this.options.pageSettings); 1314 this.page.settings = utils.mergeObjects(this.page.settings, this.options.pageSettings);
1311 if (utils.isClipRect(this.options.clipRect)) { 1315 if (utils.isClipRect(this.options.clipRect)) {
...@@ -1869,6 +1873,9 @@ function createPage(casper) { ...@@ -1869,6 +1873,9 @@ function createPage(casper) {
1869 }; 1873 };
1870 page.onResourceRequested = function onResourceRequested(request) { 1874 page.onResourceRequested = function onResourceRequested(request) {
1871 casper.emit('resource.requested', request); 1875 casper.emit('resource.requested', request);
1876 if (request.url === casper.requestUrl) {
1877 casper.emit('page.resource.requested', request);
1878 }
1872 if (utils.isFunction(casper.options.onResourceRequested)) { 1879 if (utils.isFunction(casper.options.onResourceRequested)) {
1873 casper.options.onResourceRequested.call(casper, casper, request); 1880 casper.options.onResourceRequested.call(casper, casper, request);
1874 } 1881 }
......
...@@ -70,6 +70,18 @@ function cleanUrl(url) { ...@@ -70,6 +70,18 @@ function cleanUrl(url) {
70 exports.cleanUrl = cleanUrl; 70 exports.cleanUrl = cleanUrl;
71 71
72 /** 72 /**
73 * Clones an object.
74 *
75 * @param Mixed o
76 * @return Mixed
77 */
78 function clone(o) {
79 "use strict";
80 return JSON.parse(JSON.stringify(o));
81 }
82 exports.clone = clone;
83
84 /**
73 * Dumps a JSON representation of passed value to the console. Used for 85 * Dumps a JSON representation of passed value to the console. Used for
74 * debugging purpose only. 86 * debugging purpose only.
75 * 87 *
...@@ -456,7 +468,7 @@ exports.mergeObjects = mergeObjects; ...@@ -456,7 +468,7 @@ exports.mergeObjects = mergeObjects;
456 */ 468 */
457 function node(name, attributes) { 469 function node(name, attributes) {
458 "use strict"; 470 "use strict";
459 var _node = document.createElement(name); 471 var _node = document.createElement(name);
460 for (var attrName in attributes) { 472 for (var attrName in attributes) {
461 var value = attributes[attrName]; 473 var value = attributes[attrName];
462 if (attributes.hasOwnProperty(attrName) && isString(attrName)) { 474 if (attributes.hasOwnProperty(attrName) && isString(attrName)) {
......
1 /*global casper*/
2 /*jshint strict:false*/
3 function testHeader(header) {
4 return header.name === 'Accept' && header.value === 'application/json';
5 }
6
7 var t = casper.test, current = 0, tests = [
8 function(request) {
9 t.assertNot(request.headers.some(testHeader), "Casper.open() sets no custom header by default");
10 },
11 function(request) {
12 t.assert(request.headers.some(testHeader), "Casper.open() can set a custom header");
13 },
14 function(request) {
15 t.assertNot(request.headers.some(testHeader), "Casper.open() custom headers option is not persistent");
16 },
17 ];
18
19 casper.on('page.resource.requested', function(request) {
20 tests[current++](request);
21 });
22
23 casper.start();
24
25 casper.thenOpen('tests/site/index.html');
26 casper.thenOpen('tests/site/index.html', {
27 headers: {
28 Accept: 'application/json'
29 }
30 });
31 casper.thenOpen('tests/site/index.html');
32
33 casper.run(function() {
34 this.removeAllListeners('page.resource.requested');
35 t.done();
36 });
...@@ -23,6 +23,14 @@ t.comment('cleanUrl()'); ...@@ -23,6 +23,14 @@ t.comment('cleanUrl()');
23 } 23 }
24 })(); 24 })();
25 25
26 t.comment('clone()');
27 (function() {
28 var a = {a: 1, b: 2, c: [1, 2]};
29 t.assertEquals(utils.clone(a), a);
30 var b = [1, 2, 3, a];
31 t.assertEquals(utils.clone(b), b);
32 })();
33
26 t.comment('equals()'); 34 t.comment('equals()');
27 (function() { 35 (function() {
28 t.assert(utils.equals(null, null), 'equals() null equality'); 36 t.assert(utils.equals(null, null), 'equals() null equality');
......