Testing host-limited routes through assert_routing in Rails

I have a route in which I use restrictions to verify the host, and then a route that is essentially the same, but without a host restriction (these are really namespaces, but to simplify this example):

match "/(:page_key)" => "namespace_one/pages#show", :constraints => proc {|env| env['SERVER_NAME'] == 'test.mysite.local' } match "/(:page_key)" => "namespace_two/pages#show" 

They work exactly as expected when accessing through the browser and in integration tests when determining the host and executing get "/page_key" , etc.

However, I want to write tests that ensure that these routes still work. I was not very lucky since the following test (which is currently in ActionController::IntegrationTest so that I can set the host) matched one without limitation:

 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 to reset env in the proc constraints, all I get is --- :controller .

If I get rid of assert_routing and just make a call to get :show and dump @controller , it will allow the correct controller (as expected, all these routes work fine through HTTP requests).

+4
source share
1 answer

It was just this problem itself. This has been fixed by the Rails patch, allowing full URLs to be specified in routing tests.

Change your test to

 assert_routing 'http://test.mysite.loacl', { :controller => 'namespace_one/pages', :action => 'show' } 

and it will work fine.

You must include ": //" in the full url. b / c rails uses a regular expression to search for% r {: //} in the path or automatically breaks off part of the host URL and the test will fail.

Here is the relative patch http://tinyurl.com/28erq86

+6
source

Source: https://habr.com/ru/post/1303559/


All Articles