Specifying an HTTP status code

Hi all,

I’m just starting to work w/ RSpec, so I hope this question isn’t too
obvious or missing the point somehow: Is there a way in RSpec to
specify that a controller action should use a specific HTTP status
code? Specifically I want to test for the usage of 301 as opposed to
302, for a permanent redirection.

response.should be_redirect

looks like it calls ActionController::TestRequest#redirect?, which is
only testing for a status code from 300…399. In an old-fashioned
Test::Unit Rails test I could do assert_response, but I can’t see
anything matching that in RSpec. Is there a less-documented feature
I’m missing?

Thanks,

Francis H.

On Dec 1, 2007, at 3:45 PM, Francis H. wrote:

looks like it calls ActionController::TestRequest#redirect?, which is
only testing for a status code from 300…399. In an old-fashioned
Test::Unit Rails test I could do assert_response, but I can’t see
anything matching that in RSpec. Is there a less-documented feature
I’m missing?

Thanks,

Francis H.
http://fhwang.net/

Hey Francis - Great to see you’ve finally come over from the dark
side (Test::Unit)! How is rspec treating you?

Anyway - here’s how I’ve done it with a 404:

describe ErrorController, “view” do

it “should render the 404 page in public/” do
get :view
response.should render_template(“#{RAILS_ROOT}/public/404.html”)
end

it “should return an HTTP status code of 404” do
get :view
response.headers[“Status”].should == “404 Not Found”
end

end

I would imagine you could do something similar for 301. I don’t
think there are currently any matchers for these status codes like:
should_be_not_found or should_not_be_a_404, although they could
certainly be written. (Method missing seems to be screaming at me
here…)

Regards,

Scott T.

On Dec 1, 2007 4:27 PM, Scott T. [email protected]
wrote:

Francis H.
it “should render the 404 page in public/” do
get :view
response.should render_template(“#{RAILS_ROOT}/public/404.html”)
end

it “should return an HTTP status code of 404” do
get :view
response.headers[“Status”].should == “404 Not Found”
end

end

You can also just go right for the response_code:

response.response_code.should == 301

On 1.12.2007, at 23.27, Scott T. wrote:

it “should return an HTTP status code of 404” do
get :view
response.headers[“Status”].should == “404 Not Found”

or

response.response_code.should == 404


Jarkko L.

http://www.railsecommerce.com
http://odesign.fi

On Dec 1, 2007, at 5:10 PM, Jarkko L. wrote:

On 1.12.2007, at 23.27, Scott T. wrote:

it “should return an HTTP status code of 404” do
get :view
response.headers[“Status”].should == “404 Not Found”

or

response.response_code.should == 404

Nice. I find out something new every day from this list.

On Dec 1, 2007 5:19 PM, Scott T. [email protected]
wrote:

response.response_code.should == 404

Nice. I find out something new every day from this list.

You might also look at status_codes.rb. For example, if you have rails
in
vendor/rails, it’s path is

vendor/rails/actionpack/lib/action_controller/status_codes.rb

For any status in that file, you should be able to do this in your
examples:

assert_response :not_found

Yes, that’s a Test::Unit assertion. Here’s why I use it:

  • I’m not sure if there is a way to do it in RSpec (haven’t yet
    investigated)
  • I can use Test::Unit assertions in RSpec (really cool)
  • I like the way it reads compared to using the codes, such as 404

Regards,
Craig