Webmock: Mock a timeout

Created on 1 Mar 2010  路  4Comments  路  Source: bblimke/webmock

Possibly a strange request here but...

Would it be possible to mock an HTTP Timeout? I am testing error handling and the ability to mock a request to timeout would be very useful.

Most helpful comment

Added in master.
You can do the following now:

stub_request(:any, 'www.example.net').to_timeout

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

It will always raise http client specific timeout exception.

All 4 comments

Hi Jeffrey,

I was thinking about this feature. WebMock could have something like stub_request(:get, "www.google.com").to_timeout

Currently you can achieve this functionality by using .to_raise(e) where e is a library specific timeout exception.
The whole point of webmock is to be http client library independent so to_timeout should work with every library.
The problem is that different libraries return different errors, ie Net::HTTP return Ruby Timeout::Error while HTTPClient raises HTTPClient::TimeoutError.
This behaviour can be replicated in WebMock but the error handling code will have to be different any time you change the library.

Ooo I didn't know about to_raise. While a generic to_timeout would of course be nicer I am quite happy with to_raise for my purposes.

Added in master.
You can do the following now:

stub_request(:any, 'www.example.net').to_timeout

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

It will always raise http client specific timeout exception.

Groovy, thanks very much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Integralist picture Integralist  路  5Comments

bootstraponline picture bootstraponline  路  18Comments

Morred picture Morred  路  14Comments

RKushnir picture RKushnir  路  14Comments

lokenmakwana picture lokenmakwana  路  6Comments