views:

19

answers:

1

I need to test two things:

  1. that certain old paths are correctly redirected to certain new paths
  2. that the redirect is a 301, not a 302.

I'm using Capybara for my acceptance tests, but that can't handle #2. I can test that the redirect happens, but it happens silently, so I can't see that it was a 301.

Controller tests can't handle #1. The "get", "post" etc. verbs that rspec provides for controller tests only allow you to pass in an action, not a specific path, and the redirect is implemented in a single action based on the path, as below:

# controller
class ExampleController
  def redirect301
    redirect_to case request.path
    when '/old_a'
      '/new_a'
    when '/old_b'
      '/new_b'
    end, :status => 301
  end
end

# routes.rb
['old_a', 'old_b'].each do |p| 
  map.connect p, :controller => :example, :action => :redirect301
end

So, what should I do?

+1  A: 

Try this:

it "should redirect with 301" do
  get :action
  response.code.should == 301
end
Eimantas
OK, that tests that the action exists and returns a 301, but this would still pass if "old_a" got redirected to "new_b" and vice versa.
lawrence
so do you need to check redirect status or redirection url?
Eimantas
I need to check both.
lawrence
then check for response.location value. It will have domain + path. Or response.fullpath for only path part (rails 3 only).
Eimantas
The correct response varies depending on the contents of the url though (see my code snippet), and I can't change that if "get" only lets me define an action.
lawrence