Need help testing response status codes

997 views
Skip to first unread message

Ray

unread,
May 9, 2012, 7:43:48 AM5/9/12
to webmock-users
I have had no luck at all to get any useful behaviour out of WebMock.
Here is what I have in my Specs.

stub_request(:get, "https://example.com/some_name").
to_return(:status => 503, :body => "", :headers => {})

response = HTTParty.get( "https://example.com/some_name" )
=> nil

response = Net::HTTP.get(URI.parse("https://example.com/some_name")
=> ""
response.class
=> String

Environment:
Rails 3.2.3, Ruby 1.9.3-p125, OS X, Webmock '1.8.6'

Pete Higgins

unread,
May 9, 2012, 11:15:17 AM5/9/12
to webmoc...@googlegroups.com
What version of HTTParty are you using? With the latest release of it and Webmock I am not seeing the same behavior:

$ irb
>> require 'webmock'
=> true
>> include WebMock::API
=> Object
>> require 'httparty'
=> true
>> stub_request(:get, "https://example.com/some_name").to_return(:status => 503, :body => "", :headers => {})
=> GET https:examplecomsome_name

>> response = HTTParty.get("https://example.com/some_name")
=> #<HTTParty::Response:0x8 parsed_response=nil, @response=#<Net::HTTPServiceUnavailable 503  readbody=true>, headers{}
>> response.class
=> HTTParty::Response
>> WebMock::VERSION
=> "1.8.6"
>> HTTParty::VERSION
=> "0.8.3"

pete

Ray

unread,
May 9, 2012, 11:32:29 AM5/9/12
to webmock-users
I'm using gem 'httparty', '0.8.3'
gem 'webmock', '1.8.6', :group => :test

It runs fine in IRB, but not in RAILS_ENV=test rails console.

I'm guessing it's either a conflict with another gem or possibly a
load order issue.
I created a blank rails project and I'm trying to home in on the error
source.


On May 9, 4:15 pm, Pete Higgins <p...@peterhiggins.org> wrote:

Ray

unread,
May 9, 2012, 12:35:55 PM5/9/12
to webmock-users
I have figured the culprit, but I don't have a solution yet.

Try running that again and using pry instead of irb.

On May 9, 4:15 pm, Pete Higgins <p...@peterhiggins.org> wrote:

Ray

unread,
May 9, 2012, 1:25:45 PM5/9/12
to webmock-users
Removing pry from the gem file makes it work in the Rails console,
sadly in the specs it still gives me a nil.
For now, I can't spare any more time to dwell on this.
I will come back to this issue later this month because I really want
to test against that. Maybe a better way to start would be to make the
WebMock specs fail, rather than trying to make mine pass.

Ray

unread,
May 9, 2012, 1:58:38 PM5/9/12
to webmock-users
So it seems to be working,
stub_request(:any, "www.example.com").to_return(:status => [500,
"Internal Server Error"])
response = HTTParty.get( "http://www.example.com/" )

response returns nil when you ask it

response
=> nil

However, response.response is still there, it's just a matter of
output, the underlying stuff is still working. Man, not one of my most
productive days. I will put some more time into this when I can, at
least I can use WebMock, so for now I'm happy.

Ray
Reply all
Reply to author
Forward
0 new messages