21

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 doesn't work, it has

NoMethodError:
   undefined method `map' for /.*?/:Regexp
Bruce Lin
  • 2,700
  • 6
  • 28
  • 38

2 Answers2

17

You should be able to just use

WebMock.stub_request(:post, 'api.quickblox.com/')
  .to_return(status: 201, body: 'whatever', headers: { some_kind_of: 'header' })

If you don't specify the body or headers on the stub itself, it will allow anything for the body or headers. This does not apply to query parameters.

For example, this sample project's test passes:

bundle exec rspec output:

Test
  does a thing

Finished in 0.00379 seconds (files took 0.25797 seconds to load)
1 example, 0 failures

lib/test.rb

require 'faraday'
require 'json'

class Test
  def self.do_a_thing
    JSON.parse(Faraday.get('http://www.example.com') do |request|
      request.headers['Something'] = 'anything'
      request.body = 'bla bla bla'
    end.body)
  end
end

spec/test_spec.rb

require_relative '../lib/test.rb'
require 'webmock/rspec'

describe Test do
  WebMock.stub_request(:get, 'http://www.example.com')
    .to_return(body: '{ "this": "is a test" }')

  it 'does a thing' do
    expect(described_class.do_a_thing).to include({ 'this' => 'is a test' })
  end
end

.ruby-version

ruby-2.0.0

Gemfile

gem 'rspec'
gem 'webmock'
gem 'faraday'

Gemfile.lock

GEM
  specs:
    addressable (2.3.8)
    crack (0.4.2)
      safe_yaml (~> 1.0.0)
    diff-lcs (1.2.5)
    faraday (0.9.1)
      multipart-post (>= 1.2, < 3)
    multipart-post (2.0.0)
    rspec (3.2.0)
      rspec-core (~> 3.2.0)
      rspec-expectations (~> 3.2.0)
      rspec-mocks (~> 3.2.0)
    rspec-core (3.2.3)
      rspec-support (~> 3.2.0)
    rspec-expectations (3.2.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.2.0)
    rspec-mocks (3.2.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.2.0)
    rspec-support (3.2.2)
    safe_yaml (1.0.4)
    webmock (1.20.4)
      addressable (>= 2.3.6)
      crack (>= 0.3.2)

PLATFORMS
  ruby

DEPENDENCIES
  faraday
  rspec
  webmock

BUNDLED WITH
   1.10.5
jnfjnjtj
  • 409
  • 4
  • 10
  • 2
    This is not true, it will not match. – lzap Jul 15 '15 at 12:30
  • 2
    Yeah. It should work. I thought it didn't, either, but then I realized I actually had never called `stub_request` – Christopher Davies Jan 21 '16 at 20:56
  • 2
    BEWARE OF THE TRAILING SLASH on the url! stub_request(:get, ' h ttp://www.example.com') will match anything, but stub_request(:get, ' h ttp://www.example.com/') won't!! Lost some time on this. – Mauricio Moraes Jun 19 '17 at 21:43
  • Make sure that the URLs match exactly. If you aren't specifying a protocol in your expectation, it will default to `http`. – techpeace Sep 18 '18 at 02:02
9

Check out the Webmock docs on headers. You need to provide it a hash with further details on what you are matching.

Aaron K
  • 6,901
  • 3
  • 34
  • 29