I have a route which I'm using constraints to check the host and then a route which is essentially the same but without the host restriction (these are really namespaces but to make things simple this example will do):
match "/(:page_key)" => "namespace_one/pages#show", :constraints => proc {|env| env['SERVER_NAME'] == 'test.mysite.local' }
match "/(:page_key)" => "namespace_two/pages#show"
These work exactly as expected when accessing via the browser and in integration tests when defining the host and doing get "/page_key"
etc.
However I want to write tests that ensures that these routes work so far I'm not having much luck as the following test (which is currently in an ActionController::IntegrationTest
so I can set the host) is matching the one without the constraint:
assert_routing '', { :controller => 'namespace_one/pages', :action => 'show' }
=> The recognized options <{"action"=>"show", "controller"=>"frontend/pages"}>
did not match <{"action"=>"show", "controller"=>"namespace_two/pages"}>,
difference: <{"controller"=>"namespace_one/pages"}>
If I try dumping the env in the constraints proc all I get is --- :controller
.
If I get rid of the assert_routing and just do a get :show
call and dump the @controller
it does resolve to the correct controller (as expected as 开发者_开发百科these routes all work fine via HTTP requests).
Just had this problem myself. This was fixed by a Rails patch that allows you to specify full urls in routing tests.
Change your test to
assert_routing 'http://test.mysite.local', { :controller => 'namespace_one/pages',
:action => 'show' }
and it will work fine.
You have to include "://" in the full url b/c rails uses a regex to look for %r{://} in the path or it will automatically hack off the host portion of the url, and the test will error out.
精彩评论