Functional test "get" requests and the https protocol
I'm trying to write a functional test for an action that must run over https. I'm not testing the HTTPS redirect - I already know that works from another test.
开发者_StackOverflow社区What I'm trying to do is:
get :new, :protocol => "https://"
assert_redirected_to :root
But this does not issue the request over https. Is there a "get" option that will allow me to change the protocol?
Also, if I try to specify the url (e.g.: get "https:/test.host/do/something") I get a routing error, since there's no route at my rails level for https - it's taken care of at my web server level.
I found a much simpler answer here: http://railspikes.com/2008/9/12/testing-ssl
Which is to put the following line either (a) at the beginning of each functional test where SSL is needed, or (b) in the 'setup' method if every action in a controller uses SSL.
@request.env['HTTPS'] = 'on'
This prepends all requests with https
In functionnal test, there are no routing from HTTP or anything else, it's use directly the controller. So you can't test that become from HTTP or HTTPS.
But you can mock the request.protocole dans define it like 'https'
request.stub(:protocol).and_return("https://")
get :new
精彩评论