Nocilla icon indicating copy to clipboard operation
Nocilla copied to clipboard

Stub GET (or POST) params in different order

Open patoroco opened this issue 10 years ago • 3 comments

Hi,

I'm trying to stub a request with Nocilla, and I'm not sure that with regex should be enough to define path structure. I mean, when you send GET or POST params, you don't need to send these params in any order, so regex isn't the same for GET: http://api.myhost.com/users?pagination=5&per_page=10 than for GET: http://api.myhost.com/users?per_page=10&pagination=5, but API response should be the same.

Should be interesting to have a dictionary with GET/POST params instead of a NSString and iterate over it instead of match exactly? If your answer is yes, I could start to work in my first PR to Nocilla :)

What do you think about that?

patoroco avatar Dec 29 '14 10:12 patoroco

:+1: I realize that this issue is super old, but I would appreciate this feature :)

Ben-G avatar Jun 24 '15 04:06 Ben-G

@patoroco @Ben-G

Hi,

It's not documented but Nocilla is pretty extensible. In the DSL, both the expected URL and the expected body, expect an LSMatcheable.

All you have to do is create an object that conforms to that protocol and has the logic that you wish to implement and pass that object to the DSL. For instance you could do:

stubRequest(@"GET", matchingUrl(@"http://foo.com").withQuery(@{"abc": @"123"}))
.withBody(matchingJson(@{"fgh":@"666"))

Your functions matchingUrl().withQuery() and matchingJson() should return an LSMatcheable

And LSMatcheable spits LSMatchers that can compare in arbitrary ways the actual string (url, body, anything) with whatever data you passed into the DSL. That comparison could be order agnostic if you wish.

Nocilla is internally implemented using LSMatcheable, that's how I added support for regular expressions in URL, so you'll have examples of usage in the code already.

Play with it, let me know if you have any questions and contribute back if you feel like it.

luisobo avatar Jun 24 '15 16:06 luisobo

Thanks @luisobo. I think I'll look into this!

Ben-G avatar Jun 26 '15 01:06 Ben-G