How do I check the HTTP response code?

Showing 1-11 of 12 messages
How do I check the HTTP response code? Andreas Krüger 1/22/10 6:21 AM
Hello,

I'm new to Capybara, actually testing it out. To do so, I'm trying to
convert an existing integration test from Webrat to Capybara. It
involves verifiying that a certain transaction fails.

To be specific, upon clicking a certain button (which results in a
form POST), the RoR application is required to answer with a 403 HTTP
response code (as in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4
) . We're trying to be RESTful and good citizens of HTTP-land
here ;-) .

I can ask Capybara to click the button all right. That done, how do I
check the HTTP response code?

I guess I can probably be done by "Using the sessions manually", as
described in http://github.com/jnicklas/capybara . I' don't think I
want to follow that path. There *must* be an easier way.

Could you kindly show it to me?

Regards, Andreas

Re: [Capybara] How do I check the HTTP response code? jnicklas 1/23/10 1:38 AM
> There *must* be an easier way

You sound so confident ;) Actually there isn't. It's a pretty big
omission, I'll admit. It's actually harder than I thought it would be
to implement too, I haven't been able to figure out how to get the
status code from Selenium, I'm not sure if it's even possible.

That being said, I personally wouldn't check the response code in an
integration/acceptance test, unless I was testing some kind of REST
interface (in which case Capybara probably isn't your best bet
anyway). The user only ever cares about what they actually can see,
and HTTP status codes can't be seen.

/Jonas

P.S.: The section on using the sessions manually really isn't what you
want. That section describes how to use a Capybara Session object
without using the DSL. It doesn't give you any methods that the DSL
doesn't give you.

2010/1/22 Andreas Krüger <andreas...@dv-ratio.com>:

Re: How do I check the HTTP response code? Drew 9/11/11 9:46 AM
Not sure if this was added in a more recent version, but this works for me:

`visit root_path
page.response_code.should be 200`
Re: How do I check the HTTP response code? adi 9/21/11 8:48 AM

visit('http://www.google.com')
page.response_code.should be '200'

is this correct
Re: How do I check the HTTP response code? adi 9/25/11 1:05 AM
i didnt think it works for me ...
Re: How do I check the HTTP response code? Carlos Eduardo L. Lopes 12/19/12 6:45 AM
Are you sure you want to check the page status code?

I don't think that integration (or feature) specs are the right place to test this.

Maybe you should reconsider where you test this.

On Tuesday, December 18, 2012 7:41:51 PM UTC-2, Greg Tomei wrote:
I don't see a `page.response_code` but I do see a `page.status_code`. However, when I assert against it the value is zero even after making a request, even using a `wait_until` block.
Re: How do I check the HTTP response code? Greg Tomei 12/19/12 6:51 PM
Allow me to explain why I want to verify the HTTP status code..

My marketing dept makes changes frequently and I want my tests to be decoupled from marketing copy. When an error occurs, the test does not catch it when I check something like

page.current_path.should == some_path

That always passes, even when the action resulted in a 500 error. Not good! Is there some other way to have meaningful assertions without relying on page text?

Greg
Re: [Capybara] Re: How do I check the HTTP response code? benlovell 12/20/12 12:23 AM
On 20 December 2012 02:51, Greg Tomei <greg...@gmail.com> wrote:
Allow me to explain why I want to verify the HTTP status code..

My marketing dept makes changes frequently and I want my tests to be decoupled from marketing copy. When an error occurs, the test does not catch it when I check something like

page.current_path.should == some_path

That always passes, even when the action resulted in a 500 error. Not good! Is there some other way to have meaningful assertions without relying on page text?


Check instead for the error text or elements that would usually be displayed to your users. Try to think of it from their point of view when you're deciding on which assertions to make. I doubt many users typically check for HTTP status codes!

Cheers,
Ben
 
Greg


On Wednesday, December 19, 2012 6:45:24 AM UTC-8, Carlos Eduardo L. Lopes wrote:
Are you sure you want to check the page status code?

I don't think that integration (or feature) specs are the right place to test this.

Maybe you should reconsider where you test this.

On Tuesday, December 18, 2012 7:41:51 PM UTC-2, Greg Tomei wrote:
I don't see a `page.response_code` but I do see a `page.status_code`. However, when I assert against it the value is zero even after making a request, even using a `wait_until` block.


On Sunday, September 11, 2011 9:46:08 AM UTC-7, Drew wrote:
Not sure if this was added in a more recent version, but this works for me:

`visit root_path
page.response_code.should be 200`

Re: How do I check the HTTP response code? Rodrigo Martin 12/20/12 7:18 AM
you could use httparty, request the url and check the status code there, as a method call. then your integrations tests could continue as usual, with that added validation.
Re: [Capybara] Re: How do I check the HTTP response code? Greg Tomei 12/20/12 10:54 AM
Of course users check HTTP response codes -- they just do it implicitly. People can recognize a regular response page from an error response page.

Ben, I may have to resort to what you suggest. But I'd prefer to use the HTTP status code, its just simpler and more reliable. I see there's a method for it on the driver, why doesn't it work?? There's also a method on the driver to get the HTTP headers which would also give me access to the HTTP status code. Is it really expected that these exist but do not work?
Re: [Capybara] Re: How do I check the HTTP response code? Carlos Eduardo L. Lopes 12/20/12 11:08 AM
Greg Tomei wrote:

Of course users check HTTP response codes -- they just do it
implicitly.


You are right



People can recognize a regular response page from an error
response page.


Yes, but not using status code. They recognize that some error happened seeing a message for example.

I think that when your application has a server error, it shows a message to the user or redirect him, am i right?

So, you should test what happen on your client's perspective.

The status code you should test in controllers specs (if you are using Rails)




Ben, I may have to resort to what you suggest. But I'd prefer to use
the HTTP status code, its just simpler and more reliable. I see
there's a method for it on the driver, why doesn't it work?? There's
also a method on the driver to get the HTTP headers which would also
give me access to the HTTP status code. Is it really expected that
these exist but do not work?



On Thursday, December 20, 2012 12:23:06 AM UTC-8, benlovell wrote:

    Check instead for the error text or elements that would usually be
    displayed to your users. Try to think of it from their point of
    view when you're deciding on which assertions to make. I doubt
    many users typically check for HTTP status codes!

    Cheers,
    Ben