1

I want to mock NetHTTP requests but some should be allowed.

require "open-uri"    
require "webmock"
WebMock.enable!

When I declare an allowed request like this:

WebMock.disable_net_connect! allow: /\Ahttps:\/\/graph\.facebook\.com\/v2\.8\/debug_token\?access_token=/

and call:

open("https://graph.facebook.com/v2.8/debug_token?access_token=qwerty", &:read)

I get this:

HTTP connections are disabled. Unregistered request: GET https://graph.facebook.com/v2.8/debug_token?access_token=qwerty with headers ...
You can stub this request with the following snippet:
stub_request(:get, "https://graph.facebook.com/v2.8/debug_token?access_token=qwerty").
...

It also fails with regex like this:

/\Ahttps:\/\/graph\.facebook\.com\//

but does not fail with this:

/\Ahttps:\/\/graph\.facebook\.com/

How to allow the full regex that I initially wanted? Why even the \/ after hostname fails matching?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
  • 1
    That's very strange. I tried opening a console and running those commands, in that order, and I got the correct behavior... Are you positive the `disable_net_connect!` line is being run before the `open`? – Glyoko Feb 15 '17 at 20:20
  • 1
    @Glyoko https://gist.github.com/Nakilon/d8a2f473d7675c590e69fc1b45dfaf44 – Nakilon Feb 15 '17 at 21:06
  • 1
    Very strange... I'm getting `OpenURI::HTTPError: 400 Bad Request` which is correct, since the access_token is fake. Sorry, I'm at a loss. – Glyoko Feb 15 '17 at 21:12

1 Answers1

0

Your version of Webmock (1.22.6) has a bug when using open-uri:

https://github.com/bblimke/webmock/issues/600

You can fix this bug by updating your webmock gem to the latest 1.x or 2.x

eiko
  • 5,110
  • 6
  • 17
  • 35