开发者

How can I specify https protocol in routing spec with rspec?

开发者 https://www.devze.com 2023-04-02 12:07 出处:网络
In my routes file I have: resources :subscription, :only => [:show], :constraints => {:protocol =>开发者_如何学JAVA \"https\"}

In my routes file I have:

resources :subscription, :only => [:show], :constraints => {:protocol =>开发者_如何学JAVA "https"}

I'm trying to add a spec for this route like this:

it "recognizes and generates #show" do
  { :get => "/subscription", :protocol => 'https' }.should route_to(:controller => "subscriptions", :action => "show")
end

However, the spec still fails. If I remove the :protocol => 'https', the spec also fails:

ActionController::RoutingError:    
  No route matches "/subscription" 


The (undocumented?) solution is to simply include an entire dummy url, like so:

it "recognizes and generates #show" do
  { :get => "https://test.host/subscription" }.should route_to(:controller => "subscriptions", :action => "show")
end

I figured it out from this ticket and this changeset.


I'm not sure, but I think routes should be declared as plural — see Rails Routing from the Outside In. So it would be

resources :subscriptions

and in the spec

{:get => '/subscriptions', :protocol => 'https'}

Try if that passes without :protocol. If it does, skip the spec with HTTPS. That should not be tested on a unit-test level, but in an integration test.


Does it pass if you change to spec to this?:

{:get => '/subscription'}.should_not route_to(:controller => …)

That would at least give you the confidence that HTTP is not routed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号