Commit 7c0343f0 7c0343f08ffb9f11c9309e73d19a01ff28ef3ce4 by Nicolas Perriault

compat with phantomjs 1.9

1 parent b87c8630
......@@ -133,7 +133,7 @@ var Casper = function Casper(options) {
this.popups = pagestack.create();
// properties
this.checker = null;
this.currentResponse = undefined;
this.currentResponse = {};
this.currentUrl = 'about:blank';
this.currentHTTPStatus = null;
this.history = [];
......@@ -1049,7 +1049,7 @@ Casper.prototype.handleReceivedResource = function(resource) {
return;
}
this.currentHTTPStatus = null;
this.currentResponse = undefined;
this.currentResponse = {};
if (utils.isHTTPResource(resource)) {
this.emit('page.resource.received', resource);
this.currentResponse = resource;
......@@ -1503,6 +1503,7 @@ Casper.prototype.start = function start(location, then) {
this.emit('starting');
this.log('Starting...', "info");
this.startTime = new Date().getTime();
this.currentResponse = {};
this.history = [];
this.popups = pagestack.create();
this.steps = [];
......
......@@ -59,9 +59,10 @@ responseHeaders.prototype.get = function get(name){
};
/**
* Augment the response with proper prototypes
* Augments the response with proper prototypes.
*
* @param mixed response Phantom response or undefined (generally with local files)
* @param Mixed response Phantom response or undefined (generally with local files)
* @return Object Augmented response
*/
exports.augmentResponse = function(response) {
"use strict";
......@@ -70,4 +71,5 @@ exports.augmentResponse = function(response) {
return;
}
response.headers.__proto__ = responseHeaders.prototype;
return response;
};
......
......@@ -997,6 +997,7 @@ Tester.prototype.done = function done() {
this.executed = 0;
}
this.emit('test.done');
this.casper.currentHTTPResponse = {};
this.running = this.started = false;
var nextTest = this.queue.shift();
if (nextTest) {
......
......@@ -114,7 +114,7 @@ function equals(v1, v2) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object) {
if (Object.keys(v1).length !== Object.keys(v2).length) {
if (!(v2 instanceof Object) || Object.keys(v1).length !== Object.keys(v2).length) {
return false;
}
for (var k in v1) {
......
......@@ -14,7 +14,7 @@ var service = server.listen(8090, function(request, response) {
casper.test.begin('Casper.headers.get() using file protocol', 1, function(test) {
casper.start('file://' + phantom.casperPath + 'tests/site/index.html', function(response) {
test.assertEquals(response, undefined, 'No response available on local page');
test.assertEquals(response, {data: null}, 'Empty http response on local page');
}).run(function() {
test.done();
})
......
......@@ -6,17 +6,17 @@ casper.test.begin('handling navigation history', 4, function(test) {
casper.thenOpen('tests/site/page3.html');
casper.back();
casper.then(function() {
test.assertMatch(this.getCurrentUrl(), /tests\/site\/page2\.html$/,
test.assertMatch(this.getCurrentUrl(), /page2\.html$/,
'Casper.back() can go back an history step');
});
casper.forward();
casper.then(function() {
test.assertMatch(this.getCurrentUrl(), /tests\/site\/page3\.html$/,
test.assertMatch(this.getCurrentUrl(), /page3\.html$/,
'Casper.forward() can go forward an history step');
});
casper.run(function() {
test.assert(this.history.length > 0, 'Casper.history contains urls');
test.assertMatch(this.history[0], /tests\/site\/page1\.html$/,
test.assertMatch(this.history[0], /page1\.html$/,
'Casper.history has the correct first url');
test.done();
});
......
......@@ -40,10 +40,14 @@ casper.test.begin('eachThen() tests', 1, function(test) {
var received = [];
casper.start().eachThen([1, 2, 3], function(response) {
if (!response) {
test.fail('No response received');
}
received.push(response.data);
});
casper.run(function() {
console.log('PLOP!!!');
test.assertEquals(received, [1, 2, 3],
'Casper.eachThen() passes item to step data');
test.done();
......
......@@ -6,15 +6,36 @@
*/
var fs = require('fs');
var utils = require('utils');
var server = require('webserver').create();
var service = server.listen(8090, function (request, response) {
var code = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10);
response.statusCode = code;
casper.test.begin("HTTP status code handling", 163, {
setUp: function(test) {
this.server = require('webserver').create();
this.server.listen(8090, function (request, response) {
response.statusCode = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10);
response.write("");
response.close();
});
});
this.testCodes = [
100, 101, 102, 118, 200, 201, 202, 203, 204, 205, 206, 207, 210,
300, 301, 302, 303, 304, 305, 307, 310
];
if (utils.ltVersion(phantom.version, '1.9.0')) {
// https://github.com/ariya/phantomjs/issues/11163
this.testCodes = this.testCodes.concat([
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
414, 415, 416, 417, 418, 422, 423, 424, 425, 426, 449, 450,
500, 501, 502, 503, 504, 505, 507, 509
]);
} else {
test.skip(102);
}
},
casper.test.begin("HTTP status code handling", 109, function(test) {
tearDown: function() {
this.server.close();
},
test: function(test) {
casper.start();
// file protocol
......@@ -22,27 +43,22 @@ casper.test.begin("HTTP status code handling", 109, function(test) {
this.test.assertHttpStatus(null, 'file:// protocol does not set a HTTP status');
});
// http protocol
var codes = [100, 101, 102, 118, 200, 201, 202, 203, 204, 205, 206, 207, 210,
300, 301, 302, 303, 304, 305, 307, 310,
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413,
414, 415, 416, 417, 418, 422, 423, 424, 425, 426, 449, 450,
500, 501, 502, 503, 504, 505, 507, 509];
casper.each(codes, function(self, code) {
casper.each(this.testCodes, function(self, code) {
if (code === 100) {
// HTTP 100 is CONTINUE, so don't expect a terminated response
return;
}
this.thenOpen('http://localhost:8090/' + code, function() {
this.test.assertEquals(this.currentHTTPStatus, code,
utils.format('Status is stored in casper.currentHTTPStatus' , code));
this.test.assertHttpStatus(code, utils.format('HTTP %d handled' , code));
this.thenOpen('http://localhost:8090/' + code, function(resource) {
test.assertEquals(resource.status, code,
'Status is stored in resource.status');
test.assertEquals(this.currentHTTPStatus, code,
'Status is stored in casper.currentHTTPStatus');
test.assertHttpStatus(code, utils.format('HTTP %d handled' , code));
});
});
casper.run(function() {
server.close();
this.test.done();
});
}
});
......