Merge pull request #402 from cbosco/request-changeurl
(#401) Support for request.abort() and request.changeUrl() redirection
Showing
4 changed files
with
105 additions
and
7 deletions
... | @@ -2405,13 +2405,13 @@ function createPage(casper) { | ... | @@ -2405,13 +2405,13 @@ function createPage(casper) { |
2405 | } | 2405 | } |
2406 | casper.handleReceivedResource(resource); | 2406 | casper.handleReceivedResource(resource); |
2407 | }; | 2407 | }; |
2408 | page.onResourceRequested = function onResourceRequested(request) { | 2408 | page.onResourceRequested = function onResourceRequested(requestData, request) { |
2409 | casper.emit('resource.requested', request); | 2409 | casper.emit('resource.requested', requestData, request); |
2410 | if (request.url === casper.requestUrl) { | 2410 | if (requestData.url === casper.requestUrl) { |
2411 | casper.emit('page.resource.requested', request); | 2411 | casper.emit('page.resource.requested', request); |
2412 | } | 2412 | } |
2413 | if (utils.isFunction(casper.options.onResourceRequested)) { | 2413 | if (utils.isFunction(casper.options.onResourceRequested)) { |
2414 | casper.options.onResourceRequested.call(casper, casper, request); | 2414 | casper.options.onResourceRequested.call(casper, casper, requestData, request); |
2415 | } | 2415 | } |
2416 | }; | 2416 | }; |
2417 | page.onUrlChanged = function onUrlChanged(url) { | 2417 | page.onUrlChanged = function onUrlChanged(url) { | ... | ... |
... | @@ -14,7 +14,7 @@ | ... | @@ -14,7 +14,7 @@ |
14 | } | 14 | } |
15 | ], | 15 | ], |
16 | "dependencies": { | 16 | "dependencies": { |
17 | "http://www.phantomjs.org/": "1.7" | 17 | "http://www.phantomjs.org/": "master" |
18 | }, | 18 | }, |
19 | "bugs": { | 19 | "bugs": { |
20 | "url": "https://github.com/n1k0/casperjs/issues" | 20 | "url": "https://github.com/n1k0/casperjs/issues" | ... | ... |
... | @@ -6,8 +6,8 @@ function testUA(ua, match) { | ... | @@ -6,8 +6,8 @@ function testUA(ua, match) { |
6 | ); | 6 | ); |
7 | } | 7 | } |
8 | 8 | ||
9 | function fetchUA(request) { | 9 | function fetchUA(requestData, request) { |
10 | var headers = request.headers.filter(function(header) { | 10 | var headers = requestData.headers.filter(function(header) { |
11 | return header.name === "User-Agent"; | 11 | return header.name === "User-Agent"; |
12 | }); | 12 | }); |
13 | casper.test.assert(headers.length > 0); | 13 | casper.test.assert(headers.length > 0); | ... | ... |
tests/suites/casper/request.coffee
0 → 100644
1 | #global casper | ||
2 | |||
3 | #jshint strict:false | ||
4 | |||
5 | SERVER = 'http://localhost:54321/' | ||
6 | ORIGINAL_URL = "tests/site/index.html" | ||
7 | CHANGED_URL = "tests/site/index.html?foo=bar" | ||
8 | |||
9 | setToTrueOnResourceRequested = false | ||
10 | setToTrueOnResourceReceived = false | ||
11 | requestURLRequested = '' | ||
12 | requestURLReceived = '' | ||
13 | |||
14 | onResourceRequested = (casper, requestData, request) -> | ||
15 | if requestData.url == (SERVER + ORIGINAL_URL) | ||
16 | setToTrueOnResourceRequested = true | ||
17 | requestURLRequested = requestData.url | ||
18 | |||
19 | onResourceRequestedWithAbort = (casper, requestData, request) -> | ||
20 | if requestData.url == (SERVER + ORIGINAL_URL) | ||
21 | request.abort() | ||
22 | |||
23 | onResourceRequestedWithChangeURL = (casper, requestData, request) -> | ||
24 | if requestData.url == (SERVER + ORIGINAL_URL) | ||
25 | request.changeUrl(SERVER + CHANGED_URL) | ||
26 | |||
27 | onResourceReceived = (casper, response) -> | ||
28 | if response.url == (SERVER + ORIGINAL_URL) | ||
29 | setToTrueOnResourceReceived = true | ||
30 | requestURLReceived = response.url | ||
31 | |||
32 | onResourceReceivedWithChangeURL = (casper, response) -> | ||
33 | if response.url == (SERVER + CHANGED_URL) | ||
34 | requestURLReceived = response.url | ||
35 | |||
36 | setUp = (test) -> | ||
37 | casper.options.onResourceRequested = onResourceRequested | ||
38 | casper.options.onResourceReceived = onResourceReceived | ||
39 | casper.start() | ||
40 | |||
41 | setUpWithAbort = (test) -> | ||
42 | casper.options.onResourceRequested = onResourceRequestedWithAbort | ||
43 | casper.options.onResourceReceived = onResourceReceived | ||
44 | casper.start() | ||
45 | |||
46 | setUpWithChangeURL = (test) -> | ||
47 | casper.options.onResourceRequested = onResourceRequestedWithChangeURL | ||
48 | casper.options.onResourceReceived = onResourceReceivedWithChangeURL | ||
49 | casper.start() | ||
50 | |||
51 | tearDown = (test) -> | ||
52 | setToTrueOnResourceRequested = false | ||
53 | setToTrueOnResourceReceived = false | ||
54 | casper.options.onResourceRequested = null | ||
55 | casper.options.onResourceReceived = null | ||
56 | |||
57 | |||
58 | casper.test.begin "onResourceRequested tests without abort/override", 4, | ||
59 | setUp: setUp | ||
60 | tearDown: tearDown | ||
61 | test: (test) -> | ||
62 | casper.open(ORIGINAL_URL).then -> | ||
63 | |||
64 | casper.wait 3000, -> | ||
65 | test.assertEquals setToTrueOnResourceRequested, true, "Casper.options.onResourceRequested called successfully" | ||
66 | test.assertEquals requestURLRequested, SERVER+ORIGINAL_URL, "request url successfully recorded" | ||
67 | test.assertEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived called successfully" | ||
68 | test.assertEquals requestURLReceived, SERVER+ORIGINAL_URL, "response url successfully recorded" | ||
69 | |||
70 | casper.run -> | ||
71 | test.done() | ||
72 | |||
73 | |||
74 | casper.test.begin "onResourceRequested tests with request.abort()", 1, | ||
75 | setUp: setUpWithAbort | ||
76 | tearDown: tearDown | ||
77 | test: (test) -> | ||
78 | casper.open(ORIGINAL_URL).then -> | ||
79 | |||
80 | casper.wait 3000, -> | ||
81 | test.assertNotEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived correctly never called" | ||
82 | |||
83 | casper.run -> | ||
84 | test.done() | ||
85 | |||
86 | |||
87 | casper.test.begin "onResourceRequested tests with request.changeUrl()", 1, | ||
88 | setUp: setUpWithChangeURL | ||
89 | tearDown: tearDown | ||
90 | test: (test) -> | ||
91 | casper.open(ORIGINAL_URL).then -> | ||
92 | |||
93 | casper.wait 3000, -> | ||
94 | test.assertEquals requestURLReceived, SERVER+CHANGED_URL, "response url successfully changed" | ||
95 | |||
96 | casper.run -> | ||
97 | test.done() | ||
98 |
-
Please register or sign in to post a comment