Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rspec redirect_to matcher fails if route only exists for a subdomain #16

Open
ajh opened this issue Aug 31, 2010 · 3 comments
Open

rspec redirect_to matcher fails if route only exists for a subdomain #16

ajh opened this issue Aug 31, 2010 · 3 comments

Comments

@ajh
Copy link

ajh commented Aug 31, 2010

I was running into a problem with rspec's redirect_to matcher in my controller specs. If a controller was redirecting to a url that only exists in a subdomain, redirect_to would fail because it ignores the host part of the 302 response. I saw this with rails 2.3.8, rspec-rails 1.3.2 and subdomain-fu 0.5.4. Here's an example, followed by a workaround.

# routes
map.with_options :conditions => {:subdomain => 'mobile'} do |mobile|
  mobile.resources :phone_numbers
end

# a controller
class PhoneNumbersController < ActionController::Base
  def new; end
  def edit; redirect_to new_phone_number_url; end
end

# and a spec
describe '#edit' do
  it "should redirect" do
    request.host = 'mobile.test.host'
    get 'edit'
    puts response.body # prints the correct url of http://mobile.test.host/phone_numbers/new
    response.should redirect_to(edit_phone_number_url) # this raises ActionController::RoutingError
  end
end

I traced the problem to the rspec-rails file lib/spec/rails/matchers/redirect_to.rb where the url from the 302 response gets converted into a route hash. But it ignores the host part of the url so the hash wouldn't have :subdomain => 'mobile'. This would cause the route not to be found, raising the error. I monkey patched rspec-rails like this which solved my problem:

module Spec
  module Rails
    module Matchers
      class RedirectTo  #:nodoc:
        # a monkey patch to make rspec aware of subdomains when using the redirect_to matcher
        def path_hash(url)
          uri = URI.parse(url)
          ActionController::Routing::Routes.recognize_path uri.path, {
            :method => :get,   
            :subdomain => SubdomainFu.subdomain_from(uri.host),
          }     
        end
      end
    end
  end
end

I'm not sure whether subdomain-fu should try to patch rspec like above, or if rspec could be doing something better to avoid the issue. It seems like rspec is taking a shortcut converting the url to a hash which bypasses the route extensions applied by subdomain-fu.

@ajh
Copy link
Author

ajh commented Sep 1, 2010

I found another issue. When passing hash arguments to the redirect_to matcher it will always fail if the redirect url host is different than the request host. My workaround is to pass a string argument instead.

@chhhris
Copy link

chhhris commented Apr 29, 2014

did this ever get fixed? in my app i'm redirecting the host for certain request.paths and i'm finding the same issue -- redirect_to(...) is automatically assuming the host and i can't test against it.

selfie-1

@chhhris
Copy link

chhhris commented Apr 29, 2014

well i found a solution to this narrow problem by specifying the host as a param in the redirect. e.g.

response.should redirect_to(host: site.internal_host)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants