B Seven
B Seven

Reputation: 45943

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

Upvotes: 30

Views: 14853

Answers (3)

Gerry Shaw
Gerry Shaw

Reputation: 9378

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

Upvotes: 39

Philippe Rathé
Philippe Rathé

Reputation: 9088

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

stub_request(:post, /http:\/\/www.example.com\/foo\/\d+\/bar/)

Upvotes: 21

Jack
Jack

Reputation: 5768

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

Upvotes: 12

Related Questions