Webmock Simulate Failing API (No Internet, Timeout ++)

WebMock simulate failing API (no internet, timeout ++)

After some digging I found some solutions to this.

Apparently you can change the to_return(...) to to_timeout, which will throw a timeout error. You can also have to_raise(StandardError). For full reference, see https://github.com/bblimke/webmock#raising-timeout-errors.

Timeout, or server not found, example:

stubbed_request = stub_request(:get, "#{host}/api/something.json").
with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_timeout

Raise StandardError, or no internet/other exception, example:

stubbed_request = stub_request(:get, "#{host}/api/something.json").
with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_raise(StandardError)

#Error example 2:
stubbed_request = stub_request(:get, "#{host}/api/something.json").
with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}).
to_raise("My special error")

There you go, not too hard after all.


I have no idea how I didn't find this the first time. Anyway, hope this helps someone out one day.

Simulate empty response if server is down

You could set the timeout of your client socket to a very short value to simulate timing out trying to connect to the server.

Here's a question showing how to set a socket timeout. Most HTTP clients let you provide config properties for the underlying TCP socket.

There are also test utilities to help with this, such as WebMock, explained in this question.

Does webmock auto detects `?` in the url

You've changed the URL that you're stubbing. You should stub using the message displayed with the query params specified in the with method arguments!

Rails webmock stubbing out localhost api call

In this snippet:

stub_request(:any, "localhost:3001") 

:any refers to the http method to call like GET or POST. So you are stubbing GET/POST/whatever on exactly that URL and only that URL. My guess is that what you are sending requests to is not exactly localhost:3001.

Try extracting Rails.application.secrets[:platform_receiver_url][:base_url] + response_body["application_id"].to_s out into a variable and log it when you run your specs. My guess is you'll need to change your stub to be that URL which is likely something like localhost:3001/some_resource/1.

To Stub all paths on localhost:3001

Webmock also supports matching urls by regex:

stub_request(:any, /localhost:3001*/)

Ruby on Rails: Best way to test a failed call to a third party API

I would recommend using something like webmock to mock all of your http requests (not just to mock a failed request); it'll greatly speed up your test suite instead of having to actually hit the third party service every time you run the tests.

Webmock supports Rest Client and Test::Unit. Just put this code in your test/test_helper.rb file:

require 'webmock/test_unit' 

As an example, to test a network timeout:

stub_request(:any, 'www.example.net').to_timeout
RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout

Stubing HTTPS call using WebMock

Take a look at the answer for this question: WebMock: Rspec - Test Facebook Validation by using JSON response

Your code will do http request with port 443 vs doing really https

Net::HTTP.post_form(URI.parse('https://some_gateway.com'), {})

And here you can find the answer how to do https request using Net::HTTP
Using Net::HTTP.get for an https url



Related Topics



Leave a reply



Submit