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
5
votes
1 answer

Webmock not responding to stub request with query

I'm working on a Ruby on Rails gem and I'm trying webmock because I need to interact (and test) an external API not under my control. So, here is the snippet which is in before(:each) because I was stubbing it there: before do uri =…
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
5
votes
1 answer

Stubing HTTPS call using WebMock

I'd like to stub https call using webmock. Let's assume gateway url as https://some_gateway.com. After doing: stub_request(:post, 'https://some_gateway.com').with(body: {}) in specs. I use Net::HTTP to generate request: …
Leszek Andrukanis
  • 2,114
  • 2
  • 19
  • 30
5
votes
1 answer

How to set the env['SERVER_NAME'] in rack/test?

In Sinatra tests, env['SERVER_NAME'] defaults to www.example.com. How can I set this to some arbitrary domain? Capybara has .default_host method, but not using Capybara. Or, is it possible to change the env[DEFAULT_HOST]? Using RSpec, Sinatra,…
B Seven
  • 44,484
  • 66
  • 240
  • 385
4
votes
1 answer

How to stub a HTTParty request inside a method for testing?

I have created a function that makes a HTTParty get request. It raises a custom error message that i need to test. I tried to stub the request using Webmock in the test but it is raising a . How can i stub the get request if the…
sparkstar
  • 593
  • 1
  • 7
  • 14
4
votes
1 answer

How to stub requests on domain or after method?

config.before(:each) do stub_request(:post,…
Tharan Raj
  • 43
  • 1
  • 4
4
votes
2 answers

WebMock.disable_net_connect! not working with rspec (no VCR)

I tried to put WebMock.disable_net_connect! in a lot of places but as recommended in my last test I put it in spec_helper.rb like this: require 'webmock/rspec' WebMock.disable_net_connect!(allow_localhost: true) My spec is the following: describe…
Nelson Senna
  • 626
  • 7
  • 9
4
votes
1 answer

How can I disable webmocks logging?

I am using webmock for one of my rspec tests, and whenever I run the test suite I get what looks like webmock's logging statements in the middle of the run: ...I, [2016-03-17T09:31:50.735555 #16878] INFO -- : post…
CCC
  • 43
  • 2
4
votes
1 answer

One Test Case Seems to Interfere the Other Test Case

This is on Rails 4.0.11. I am writing a test using minitest and webmock for a worker that interacts with an API. I have two test cases (so far) in my test file. When I run both tests, the second one (the one with "success" in the title) fails. When…
Steven Hirlston
  • 1,869
  • 1
  • 15
  • 19
4
votes
1 answer

What is the purpose of stubbing an HTTP request (e.g. using the WebMock gem)?

As a precursor FYI, I'm a budding developer. I'm trying to write a test for an http POST method for a Ruby gem. From what I can understand, when you stub an http response, for instance with the Ruby WebMock gem, you're basically telling it what to…
Aaron
  • 6,466
  • 7
  • 37
  • 75
4
votes
0 answers

How can I extract parameters from requested URL in tests in WebMock?

I'm trying to write tests for a model that calls external API. Is it possible,using WebMock, to extract the parameters from the URL? For example, I want to match any URL that's like that: http://test.com?id=#{number}&id2=#{number2} with #{number}…
Ahmed Fathy
  • 529
  • 4
  • 20
4
votes
1 answer

How to assert HTTP request is made with correct URI when using VCR and WebMock?

I am testing a Ruby on Rails project using WebMock and VCR. I would like to assert that a request is made to the correct host, but I can't find an example of how to do that. Here's some pseudo-code for what I'm trying to do:…
Andrew
  • 227,796
  • 193
  • 515
  • 708
4
votes
3 answers

How can I test a Ruby command-line program that communicates with a web service?

I am building a Ruby command-line program that communicates with a web service. I am using Cucumber and Aruba to test the program. The problem is that I need to control the data returned from the web service; the program grabs a stream of user…
mipadi
  • 398,885
  • 90
  • 523
  • 479
3
votes
1 answer

Enforce Webmock stub_request to raise at the end of a test if it has not been called

We have a RoR application, Rspec for tests with Webmock for HTTP requests. After having to do some refactoring in our legacy codebase, I realized that many of our tests had unnecessary stubs. Like this example, the do_a function has been refactored…
HubertMine
  • 31
  • 2
3
votes
0 answers

Rails, LDAP & RSpec

I have a custom LDAP configuration for my Rails API application. Are there any good examples out there for mocking the configuration with a combination of RSpec and Webmock? I want to make sure I have proper code coverage and unfortunately things I…
el n00b
  • 1,957
  • 7
  • 37
  • 64
3
votes
1 answer

WebMock stub data from Aws CognitoIdentityProvider

I have the following module in a Rails controller concern: module AwsAuth extend ActiveSupport::Concern require 'aws-sdk' def get_cognito_user(token) cognitoidentityprovider = Aws::CognitoIdentityProvider::Client.new(region:…
Gundam Meister
  • 1,425
  • 2
  • 19
  • 29
1
2
3
11 12