Commit bd282b7f bd282b7f2c9726bde1490734a8b41169c2715b97 by Nicolas Perriault

Merge pull request #402 from cbosco/request-changeurl

(#401) Support for request.abort() and request.changeUrl() redirection
2 parents bf75fa1e 718a574d
......@@ -2405,13 +2405,13 @@ function createPage(casper) {
}
casper.handleReceivedResource(resource);
};
page.onResourceRequested = function onResourceRequested(request) {
casper.emit('resource.requested', request);
if (request.url === casper.requestUrl) {
page.onResourceRequested = function onResourceRequested(requestData, request) {
casper.emit('resource.requested', requestData, request);
if (requestData.url === casper.requestUrl) {
casper.emit('page.resource.requested', request);
}
if (utils.isFunction(casper.options.onResourceRequested)) {
casper.options.onResourceRequested.call(casper, casper, request);
casper.options.onResourceRequested.call(casper, casper, requestData, request);
}
};
page.onUrlChanged = function onUrlChanged(url) {
......
......@@ -14,7 +14,7 @@
}
],
"dependencies": {
"http://www.phantomjs.org/": "1.7"
"http://www.phantomjs.org/": "master"
},
"bugs": {
"url": "https://github.com/n1k0/casperjs/issues"
......
......@@ -6,8 +6,8 @@ function testUA(ua, match) {
);
}
function fetchUA(request) {
var headers = request.headers.filter(function(header) {
function fetchUA(requestData, request) {
var headers = requestData.headers.filter(function(header) {
return header.name === "User-Agent";
});
casper.test.assert(headers.length > 0);
......
#global casper
#jshint strict:false
SERVER = 'http://localhost:54321/'
ORIGINAL_URL = "tests/site/index.html"
CHANGED_URL = "tests/site/index.html?foo=bar"
setToTrueOnResourceRequested = false
setToTrueOnResourceReceived = false
requestURLRequested = ''
requestURLReceived = ''
onResourceRequested = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
setToTrueOnResourceRequested = true
requestURLRequested = requestData.url
onResourceRequestedWithAbort = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
request.abort()
onResourceRequestedWithChangeURL = (casper, requestData, request) ->
if requestData.url == (SERVER + ORIGINAL_URL)
request.changeUrl(SERVER + CHANGED_URL)
onResourceReceived = (casper, response) ->
if response.url == (SERVER + ORIGINAL_URL)
setToTrueOnResourceReceived = true
requestURLReceived = response.url
onResourceReceivedWithChangeURL = (casper, response) ->
if response.url == (SERVER + CHANGED_URL)
requestURLReceived = response.url
setUp = (test) ->
casper.options.onResourceRequested = onResourceRequested
casper.options.onResourceReceived = onResourceReceived
casper.start()
setUpWithAbort = (test) ->
casper.options.onResourceRequested = onResourceRequestedWithAbort
casper.options.onResourceReceived = onResourceReceived
casper.start()
setUpWithChangeURL = (test) ->
casper.options.onResourceRequested = onResourceRequestedWithChangeURL
casper.options.onResourceReceived = onResourceReceivedWithChangeURL
casper.start()
tearDown = (test) ->
setToTrueOnResourceRequested = false
setToTrueOnResourceReceived = false
casper.options.onResourceRequested = null
casper.options.onResourceReceived = null
casper.test.begin "onResourceRequested tests without abort/override", 4,
setUp: setUp
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 3000, ->
test.assertEquals setToTrueOnResourceRequested, true, "Casper.options.onResourceRequested called successfully"
test.assertEquals requestURLRequested, SERVER+ORIGINAL_URL, "request url successfully recorded"
test.assertEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived called successfully"
test.assertEquals requestURLReceived, SERVER+ORIGINAL_URL, "response url successfully recorded"
casper.run ->
test.done()
casper.test.begin "onResourceRequested tests with request.abort()", 1,
setUp: setUpWithAbort
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 3000, ->
test.assertNotEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived correctly never called"
casper.run ->
test.done()
casper.test.begin "onResourceRequested tests with request.changeUrl()", 1,
setUp: setUpWithChangeURL
tearDown: tearDown
test: (test) ->
casper.open(ORIGINAL_URL).then ->
casper.wait 3000, ->
test.assertEquals requestURLReceived, SERVER+CHANGED_URL, "response url successfully changed"
casper.run ->
test.done()