WebMock and url escaping/encoding

236 views
Skip to first unread message

Rhett Sutphin

unread,
Feb 27, 2012, 12:23:00 AM2/27/12
to webmoc...@googlegroups.com
Hi,

I've just started using WebMock. Among other things, I like the way it lets me verify the body of a request using a block, rather than just a string or regex.

I do have one problem with it, though: the fact that it considers escaped and unescaped URIs to be equivalent. The API I'm interacting with only works if I issue requests that are escaped. E.g.,

conn.get('studies/A Test/template')

will not work, while

conn.get('studies/A%20Test/template')

will. (I'm using Faraday with the Net::HTTP adapter for the client.)

Since WebMock considers "http://example.net/studies/A Test/template" and "http://example.net/studies/A%20Test/template" to be the same request, I can't test that my client code is properly escaping its request URIs. Is there a way to change WebMock's behavior so it doesn't consider these URIs to be the same?

I've looked through the code and the docs and not seen anything, but I thought I'd ask in case anyone more experienced with the library has a suggestion.

Thanks,
Rhett

Bartosz Blimke

unread,
Feb 27, 2012, 6:54:38 AM2/27/12
to webmoc...@googlegroups.com
There is no way to do that without changing WebMock. Maybe there should be
some configurable strict mode in WebMock.

WebMock uses addressable for comparing uris and Addressable treats uris that way.

u2 = Addressable::URI.parse("http://www.example.com/studies/A Test/template")
u1 === u2   #=> true

It very much depends on the http client library you use. Some libs will consider these two urls as the same.

Bartosz


2012/2/27 Rhett Sutphin <rhett....@gmail.com>

Rhett Sutphin

unread,
Feb 27, 2012, 4:27:49 PM2/27/12
to webmoc...@googlegroups.com
Hi Bartosz,

On Feb 27, 2012, at 5:54 AM, Bartosz Blimke wrote:

> There is no way to do that without changing WebMock. Maybe there should be
> some configurable strict mode in WebMock.
>
> WebMock uses addressable for comparing uris and Addressable treats uris that way.
>
> u1 = Addressable::URI.parse("http://www.example.com/studies/A%20Test/template")
> u2 = Addressable::URI.parse("http://www.example.com/studies/A Test/template")
> u1 === u2 #=> true
>
> It very much depends on the http client library you use. Some libs will consider these two urls as the same.

Thanks for your reply. In my case, the problem not the client so much as the service -- whatever HTTP client (or even curl) I use, if it receives a URI with a path component with an unescaped value in it, it won't handle it properly. I'd like to be able to use a testing library to verify that the URIs my code is using fit this constraint. After I sent my last e-mail, it occurred to me that I might be able to do this:

WebMock.should have_requested(:get, "http://www.example.com/studies/A%20Test/template") do |request|
request.uri.to_s == request.uri.normalize.to_s
end

However, this doesn't fail when the tested code does not escape the URI. Reviewing the code, it seems like WebMock normalizes URIs as soon as it encounters them, both expected and actual, so it would indeed be a big change to be able to test proper escaping.

Thanks again for your time,
Rhett

Bartosz Blimke

unread,
Feb 28, 2012, 3:50:30 AM2/28/12
to webmoc...@googlegroups.com
It would have to be configuration in webmock 1.x. Changing this behaviour in 1.x series would break compatibility.

I'll think what's the best option for webmock 2.0
Reply all
Reply to author
Forward
0 new messages