{"action":"created","issue":{"url":"https://api.github.com/repos/n1k0/casperjs/issues/1236","labels_url":"https://api.github.com/repos/n1k0/casperjs/issues/1236/labels{/name}","comments_url":"https://api.github.com/repos/n1k0/casperjs/issues/1236/comments","events_url":"https://api.github.com/repos/n1k0/casperjs/issues/1236/events","html_url":"https://github.com/n1k0/casperjs/issues/1236","id":83699034,"number":1236,"title":"[SlimerJS] Handling 302 Redirects - CasperJS hangs","user":{"login":"ngotchac","id":2864519,"avatar_url":"https://avatars.githubusercontent.com/u/2864519?v=3","gravatar_id":"","url":"https://api.github.com/users/ngotchac","html_url":"https://github.com/ngotchac","followers_url":"https://api.github.com/users/ngotchac/followers","following_url":"https://api.github.com/users/ngotchac/following{/other_user}","gists_url":"https://api.github.com/users/ngotchac/gists{/gist_id}","starred_url":"https://api.github.com/users/ngotchac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ngotchac/subscriptions","organizations_url":"https://api.github.com/users/ngotchac/orgs","repos_url":"https://api.github.com/users/ngotchac/repos","events_url":"https://api.github.com/users/ngotchac/events{/privacy}","received_events_url":"https://api.github.com/users/ngotchac/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-01T21:21:14Z","updated_at":"2015-06-01T22:30:51Z","closed_at":null,"body":"Hi,\r\n\r\nI ran into an issue in the following use case:\r\n* Using the **SlimerJS** engine\r\n* I request an URL, receive a 302 to some location\r\n* The next location sends a file where a *Javascript Redirect* occurs\r\n\r\nRunning this with the **SlimerJS** engine makes CasperJS hangs.\r\n\r\nTo reproduce this issue, here is a sample server, a sample index.html file and a sample CasperJS script\r\n* The NodeJS server:\r\n```javascript\r\nvar http = require('http'),\r\n    fileSystem = require('fs'),\r\n    path = require('path');\r\n\r\nconst PORT =  8088; \r\n\r\nfunction handleRequest(request, response){\r\n    if (request.url === '/redirect') {\r\n        response.writeHead(302, { 'Location': '/hello' });\r\n        response.end();\r\n    } else {\r\n        var filePath = path.join(__dirname, 'index.html');\r\n        var stat = fileSystem.statSync(filePath);\r\n\r\n        response.writeHead(200, {\r\n            'Content-Type': 'text/html',\r\n            'Content-Length': stat.size\r\n        });\r\n\r\n        fileSystem.createReadStream(filePath).pipe(response);\r\n    }\r\n}\r\n\r\nhttp.createServer(handleRequest).listen(PORT);\r\n```\r\n* The `index.html` file served:\r\n```html\r\n<!DOCTYPE html>\r\n<html>\r\n<head><meta charset=\"utf-8\"></head>\r\n<body>\r\n    Hello CasperJS\r\n    <script type=\"text/javascript\">\r\n        window.addEventListener('load', function() {\r\n            window.location.hash = '#test';\r\n        });\r\n    </script>\r\n</body>\r\n</html>\r\n```\r\n* The CasperJS script:\r\n```javascript\r\nvar casper = require('casper').create({\r\n    verbose: true,\r\n    logLevel: 'debug'\r\n});\r\n\r\ncasper\r\n    .start('http://localhost:8088/redirect')\r\n    .run();\r\n```\r\n\r\nCasperJS freezes because here's what SlimerJS sends (events):\r\n* `NavigationRequest('/redirect')`\r\n* `URLChanged('/hello')`\r\n* `NavigationRequest('/hello#test')`\r\n\r\nHowever, CasperJS changes it's inner `casper.requestURL` only on the `NavigationRequested` events. Thus, when SlimerJS sends the `URLChanged` event with `/hello`, CasperJS still has the previous `/redirect` URL in `casper.requestURL`. Then, on `NavigationRequest('/hello#test')`, the flag `casper.navigationRequested` is set to `true`, waiting for an `URLChanged` event that never occurs.\r\n\r\nHere's what PhantomJS sends (events) for the same script:\r\n* `NavigationRequest('/redirect')`\r\n* `NavigationRequest('/hello')`\r\n* `URLChanged('/hello')`\r\n* `NavigationRequest('/hello#test')`\r\n* `URLChanged('/hello#test')`\r\n\r\n## Finally\r\n\r\nI'm not sure if that's a CasperJS or a SlimerJS issue, but I managed to fix this in CasperJS. In the `modules/casper.js` file, [line 2612](https://github.com/n1k0/casperjs/blob/master/modules/casper.js#L2612), in the `page.onUrlChanged` handler, just add:\r\n```javascript\r\ncasper.requestUrl = url;\r\n```\r\n\r\nWhy is that `requestUrl` only modified in the `page.onNavigationRequested` method? I think handling this in the `page.onUrlChanged` handler will work just fine. Waiting on feedbacks.\r\n\r\nThanks!\r\n"},"comment":{"url":"https://api.github.com/repos/n1k0/casperjs/issues/comments/107735950","html_url":"https://github.com/n1k0/casperjs/issues/1236#issuecomment-107735950","issue_url":"https://api.github.com/repos/n1k0/casperjs/issues/1236","id":107735950,"user":{"login":"sprak3000","id":5545555,"avatar_url":"https://avatars.githubusercontent.com/u/5545555?v=3","gravatar_id":"","url":"https://api.github.com/users/sprak3000","html_url":"https://github.com/sprak3000","followers_url":"https://api.github.com/users/sprak3000/followers","following_url":"https://api.github.com/users/sprak3000/following{/other_user}","gists_url":"https://api.github.com/users/sprak3000/gists{/gist_id}","starred_url":"https://api.github.com/users/sprak3000/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sprak3000/subscriptions","organizations_url":"https://api.github.com/users/sprak3000/orgs","repos_url":"https://api.github.com/users/sprak3000/repos","events_url":"https://api.github.com/users/sprak3000/events{/privacy}","received_events_url":"https://api.github.com/users/sprak3000/received_events","type":"User","site_admin":false},"created_at":"2015-06-01T22:30:51Z","updated_at":"2015-06-01T22:30:51Z","body":"+1"}}