mbleigh / subdomain-fu

A new plugin approach to attempting to solve the usage of subdomains in linking and routing in Rails projects.
http://rdoc.info/projects/mbleigh/subdomain-fu
Other
586 stars 118 forks source link

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

Open ajh opened 14 years ago

ajh commented 14 years ago

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 commented 14 years ago

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 commented 10 years ago

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 commented 10 years ago

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)