Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")

like image 805
B Seven Avatar asked Dec 08 '12 02:12

B Seven


3 Answers

You can use %r{} instead of // for your regular expression in Ruby to avoid having to escape the forward slashes in URLs. For example:

stub_request(:post, %r{\Ahttp://www.example.com/foo/\d+/bar\z})
like image 95
Gerry Shaw Avatar answered Nov 10 '22 22:11

Gerry Shaw


The second argument to stub_request must be a regular expression, not a string.

stub_request(:post, /http:\/\/www.example.com\/foo\/\d+\/bar/)
like image 34
Philippe Rathé Avatar answered Nov 11 '22 00:11

Philippe Rathé


http://www\..*?\.com/foo/\d+/bar should work for you.

like image 12
Jack Avatar answered Nov 10 '22 23:11

Jack