Capybara Poltergeist getting forced over https when using path helpers

465 views Asked by At

Poltergeist is unable to connect to the server. Here's the error I'm getting:

 Failure/Error: visit root_path

 Capybara::Poltergeist::StatusFailError:
   Request to 'http://127.0.0.1:58638/' failed to reach server, check DNS and/or server status

I've got the phantomjs debug output on, and here's what looks like the relevant lines to me:

2016-12-14T14:24:47 [DEBUG] WebpageCallbacks - getJsConfirmCallback
{"command_id":"5eab3091-26bd-47b7-b779-95ec9523fb5b","response":true}
{"id":"1069af41-1b7c-4f5b-a9c7-258185aa8c73","name":"visit","args":["http://127.0.0.1:58638/"]}
2016-12-14T14:24:47 [DEBUG] WebPage - updateLoadingProgress: 10
2016-12-14T14:24:48 [DEBUG] Network - Resource request error: QNetworkReply::NetworkError(ConnectionRefusedError) ( "Connection refused" ) URL: "https://127.0.0.1/"
2016-12-14T14:24:48 [DEBUG] WebPage - updateLoadingProgress: 100
2016-12-14T14:24:48 [DEBUG] WebPage - setupFrame ""
2016-12-14T14:24:48 [DEBUG] WebPage - setupFrame ""
2016-12-14T14:24:48 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function () {\n      return typeof __poltergeist;\n    })(); })()"
2016-12-14T14:24:48 [DEBUG] WebPage - evaluateJavaScript result QVariant(QString, "undefined")
{"command_id":"1069af41-1b7c-4f5b-a9c7-258185aa8c73","error":{"name":"Poltergeist.StatusFailError","args":["http://127.0.0.1:58638/",null]}}
{"id":"cbe42cdc-58db-49c5-a230-4a1f4634d830","name":"reset","args":[]}

This bit seemed like a clue Network - Resource request error: QNetworkReply::NetworkError(ConnectionRefusedError) ( "Connection refused" ) URL: "https://127.0.0.1/" and that it could be an ssl error, so I added the phantomjs option --ignore-ssl-errors=true, but it makes no difference to the response.

I switched over to capybara-webkit to see if that would provide a bit more info as some people have recommended. I get a very similar error:

"Visit(http://127.0.0.1:64341/)" started page load
Started request to "http://127.0.0.1:64341/"
Finished "Visit(http://127.0.0.1:64341/)" with response "Success()"
Started request to "https://127.0.0.1/"
Received 301 from "http://127.0.0.1:64341/"
Received 0 from "https://127.0.0.1/"
Page finished with false
Load finished
Page load from command finished
Wrote response false "{"class":"InvalidResponseError","message":"Unable to load URL: http://127.0.0.1:64341/ because of error loading https://127.0.0.1/: Unknown error"}"
Received "Reset()"
Started "Reset()"
undefined|0|SECURITY_ERR: DOM Exception 18: An attempt was made to break through the security policy of the user agent.

With the help of @thomas-walpole I realised that if I specific a non-https url to visit in capybara, it works. E.g visit "http://localhost:3000" it works. Using visit root_url works, but using visit root_path I get directed to https and the error above.

config.force_ssl is not true. I've set config.force_ssl = false in config/environments/test.rb and also used pry to check the Application.config in the context of the test.

Any ideas for why the path helpers are getting sent over https would be greatly appreciated.

1

There are 1 answers

6
Thomas Walpole On BEST ANSWER

Your app is redirecting the request to https, but Capybara doesn't run the app with https support. If it's a rails app you've probably got the config.force_ssl option enabled in your app - set it to false in the test environment. If that doesn't fix it then you'll have to look through your app to see why it's redirecting and turn off that behavior in test mode.