Questions tagged [webmock]

Library for stubbing and setting expectations on HTTP requests in Ruby.

Library for stubbing and setting expectations on HTTP requests in Ruby.

Features

  • Stubbing HTTP requests at low http client lib level (no need to change tests when you change HTTP library)
  • Setting and verifying expectations on HTTP requests
  • Matching requests based on method, URI, headers and body
  • Smart matching of the same URIs in different representations (also encoded and non encoded forms)
  • Smart matching of the same headers in different representations.
  • Support for Test::Unit
  • Support for RSpec 1.x and RSpec 2.x
  • Support for MiniTest

Supported HTTP libraries

  • Net::HTTP and libraries based on Net::HTTP (i.e RightHttpConnection, REST Client, HTTParty)
  • HTTPClient
  • Patron
  • EM-HTTP-Request
  • Curb (currently only Curb::Easy)
  • Typhoeus (currently only Typhoeus::Hydra)
  • Excon

Supported Ruby Interpreters

  • MRI 1.8.6
  • MRI 1.8.7
  • MRI 1.9.1
  • MRI 1.9.2
  • MRI 1.9.3
  • REE 1.8.7
  • JRuby
  • Rubinius

Source: https://github.com/bblimke/webmock

166 questions
30
votes
3 answers

How to use webmock regex matcher?

How to match a URL like: http://www.example.com/foo/:id/bar http://www.example.com/foo/1/bar http://www.example.com/foo/999/bar stub_request(:post, "www.example.com")
B Seven
  • 44,484
  • 66
  • 240
  • 385
29
votes
6 answers

Chromedriver / Capybara Too many open files - socket(2) for "127.0.0.1" port 9518

I'm getting this error (details below) when a capybara selector is malformed or can't find. Why is it happening, and how can I prevent the error? This is new, I think, since our recent upgrade to the latest driver. The command that triggers…
pixelearth
  • 13,674
  • 10
  • 62
  • 110
26
votes
2 answers

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

I am trying to simulate unexpected behaviour from a web api, such as not finding the server and timeouts, using webmock. What would be the best way to do this? All I can think of is to do something like this: stubbed_request = stub_request(:get,…
Automatico
  • 12,420
  • 9
  • 82
  • 110
25
votes
1 answer

Multiple calls to the same endpoint with different results in webmock?

I have some code that looks like this: while response.droplet.status != env["user_droplet_desired_state"] do sleep 2 response = ocean.droplet.show env["droplet_id"] say ".", nil, false end The idea being you can set the app to wait until…
Peter Souter
  • 5,110
  • 1
  • 33
  • 62
21
votes
2 answers

Webmock stub request with any body and header

How to use Webmock to stub request with any body and header? I tried to use regex WebMock.stub_request(:post, 'api.quickblox.com/').with(:body => /.*?/, :headers => /.*?/).to_return(:status => 201, :body => "", :headers => {}) in rspec but it…
Bruce Lin
  • 2,700
  • 6
  • 28
  • 38
7
votes
2 answers

Blacklist URLs with headless Chrome

I'm trying to block URLs in my specs, achieving something like I had when using capybara_webkit: Capybara::Webkit.configure do |config| config.block_url("*google*") config.allow_url('*my_website.com') end After reading this article, I tried to…
fabdurso
  • 2,366
  • 5
  • 29
  • 55
6
votes
1 answer

VCR says "no cassette in use" even after specifying cassette

I have an api-only RoR app with a user model. Users are authenticated via Twilio/Authy (using this gem). Each user has_one authy_user model for storing authy info, with dependent: :destroy. The authy_user model has a before_destroy hook that…
6
votes
1 answer

Mocking certificate using WebMock Rails

I have a webscraper in ruby that analyses certificates by using this: http = Net::HTTP.new(http_endpoint.host, http_endpoint.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start do |h| @cert = h.peer_cert end Is it…
CSLser
  • 193
  • 1
  • 11
6
votes
4 answers

How to correctly replicate response body of an Octokit requests' response for webmock stub

Octokit responses are of type Sawyer::Response They look like this: {:name=>"code.py", :content => "some content"} I am trying to stub my request like so reponse_body = {:content => "some content"} stub_request(:any,…
user1571609
6
votes
1 answer

WebMock stub_request not working

In my rails project, one of the initialisers requests and fetches certain data from S3. S3.buckets[CONFIG['aws']['cdn_bucket']].objects['object_name'].read This breaks the rspec test suite which uses webmock…
Sinstein
  • 887
  • 11
  • 42
6
votes
1 answer

WebMock.disable_net_connect! not working

I am trying to write a WebMock based test case to mimic calling a http API. To do so I included webmock/rspec in my spec_helper.rb file and also added WebMock.disable_net_connect!(allow_localhost: true) to disallow the http requests over the web.…
harshs08
  • 700
  • 10
  • 29
6
votes
3 answers

Using specific VCR cassette based on request

Situation: testing a rails application using Rspec, FactoryGirl and VCR. Every time a User is created, an associated Stripe customer is created through Stripe's API. While testing, it doesn't really makes sense to add a VCR.use_cassette or describe…
Philippe Dionne
  • 103
  • 1
  • 6
6
votes
3 answers

Stub multipart requests with webmock/rspec

I have been trying for a while to stub multipart requests using webmock and have not found a satisfying solution. Ideally, I would like to stub the request as follow: stub_request(:post, 'http://test.api.com').with(:body => { :file1 =>…
Kamchatka
  • 3,597
  • 4
  • 38
  • 69
5
votes
1 answer

How to correctly replicate response body of an HTTParty requests' response for webmock stub

I have an HTTParty request with response of the form: # [{"id"=>"11111", "name"=>"foo", "reference"=>nil}, {"id"=>"22222", "name"=>"bar", "reference"=>nil}] }, …
x6iae
  • 4,074
  • 3
  • 29
  • 50
5
votes
2 answers

Having trouble with WebMock, not stubbing correctly

Ruby 1.9.3, RSpec 2.13.0, WebMock 1.17.4, Rails 3 I am writing tests for a company app. The controller in question displays a table of a customer's placed calls, and allows for sort/filter options. EDIT The test fails because with my current setup,…
onebree
  • 1,853
  • 1
  • 17
  • 44
1
2 3
11 12