Regex matching urls

81 views
Skip to first unread message

Ben Schwarz

unread,
Jun 23, 2009, 5:25:24 AM6/23/09
to FakeWeb
Hi,

Many of times I've found myself using an api service which requires
particular params for responses.
I found this fork / commit from quite some time ago that implemented
regex matching.

http://github.com/jacqui/fakeweb/commit/d745de3f110dbdc3cbe6455a2d946baae7932787

Can this, or something like it be merged in so that one could mock out
methods.

Cheers,

Ben

Dr Nic Williams

unread,
Jun 23, 2009, 1:23:03 PM6/23/09
to fakewe...@googlegroups.com
I haven't played with this patch, but being able to pass a regexp in seems awfully useful, especially to match parameter arguments.
--
Dr Nic Williams
Mocra - Premier iPhone and Ruby on Rails Consultants
w - http://mocra.com
twitter - @drnic
skype - nicwilliams
e - dr...@mocra.com
p - +61 412 002 126 or +61 7 3102 3237

Chris Kampmeier

unread,
Jun 23, 2009, 8:03:35 PM6/23/09
to FakeWeb
On Jun 23, 2:25 am, Ben Schwarz <ben.schw...@gmail.com> wrote:
> I found this fork / commit from quite some time ago that implemented
> regex matching.
>
>  http://github.com/jacqui/fakeweb/commit/d745de3f110dbdc3cbe6455a2d946...
>
> Can this, or something like it be merged in so that one could mock out
> methods.

I've got a branch of that fork in my repo that I've been working on
since mid-April or so. :) See http://github.com/chrisk/fakeweb/network
for the haps, it's the chrisk/fakeweb/regexes branch.

Actually, I think it's about ready to go -- I was doing some last-
minute test cleanup yesterday. So hopefully I'll merge it into master
soonish and the forthcoming 1.2.4 release will have experimental regex
support. Woo!

Chris

Ruben Medellín

unread,
Jul 15, 2009, 6:30:33 PM7/15/09
to FakeWeb
The regexp support is great!
I think next step towards making this absolutely awesome (and useful)
is to use the matched information from the url in the response.
Something like this comes to my mind:

FakeWeb.register_uri :any, %r{http://somesite.com/search?query=([^&]*)
&max_results=([^&]*)} do |query, max_results|
if query == "cheeseburger"
"You gotta be kidding"
else
"Found: #{query}! " * max_results.to_i
end
end

or maybe :body and :response objects accepting Proc objects to
evaluate the response.

I am only contributing with ideas by now, if I get some free time I'll
take a look at the code and implement the functionality.
Kudos for the great work!

On Jun 23, 7:03 pm, Chris Kampmeier <ch...@kampers.net> wrote:
> On Jun 23, 2:25 am, Ben Schwarz <ben.schw...@gmail.com> wrote:
>
> > I found this fork / commit from quite some time ago that implemented
> > regex matching.
>
> >  http://github.com/jacqui/fakeweb/commit/d745de3f110dbdc3cbe6455a2d946...
>
> > Can this, or something like it be merged in so that one could mock out
> > methods.
>
> I've got a branch of that fork in my repo that I've been working on
> since mid-April or so. :) Seehttp://github.com/chrisk/fakeweb/network

Ruben Medellín

unread,
Jul 16, 2009, 2:00:05 PM7/16/09
to FakeWeb
Woo, seems like implementing sometthing simple but functional was not
difficult at all. I've implemented my idea of body accepting Proc
objects in a fork of the project. Did the tests first, you can look
them at
http://github.com/chubas/fakeweb/blob/78228c700fe90ce7cf5452d7293712b040d4d703/test/test_yield_matches_in_regexes.rb

and they are passing =) You can now do things like

FakeWeb.register_uri :get, %r{http://search.twitter.com/search.json\?q=
(.*)}, :body => Proc.new{|search| {:results => ["No results for #
{search}"]}.to_json }

which is exactly the kind I was looking for. I'm working on a library
'fakewhale', a mock of Twitter behavior for tests. Will probably
release initial work very soon.

Here's my fork of the project with the implemented changes
http://github.com/chubas/fakeweb/tree/master, hope you find them
useful

Cheers.
Ruben (Chubas)
Reply all
Reply to author
Forward
0 new messages