Skip to content

Match request body using block#53

Closed
brow wants to merge 6 commits intolausobo:masterfrom
brow:body-where
Closed

Match request body using block#53
brow wants to merge 6 commits intolausobo:masterfrom
brow:body-where

Conversation

@brow
Copy link
Copy Markdown

@brow brow commented Nov 7, 2013

Request bodies are most often encoded as application/json, application/x-www-form-urlencoded, or application/xml. Each of those encodings can represent the same data in more than one way.

For example, in JSON encoding, {"a":1, "b":2} is equivalent to {"b":2, "a":1}. In fact, some JSON serializers behave nondeterministically as to which of those two they emit.

Therefore, it usually doesn't make sense to test the body of a request using -[NSData equals]. Instead, you want to use a notion of equality appropriate to the encoding.

This pull enables matching a request body using an arbitrary block, like so:

stubRequest(@"POST", @"https://api.example.com/endpoint.json").
withHeaders(@{@"Accept": @"application/json"}).
withBodyWhere(^(NSData *body){
    id bodyJSONObject = [NSJSONSerialization JSONObjectWithData:body
                                                        options:0
                                                          error:NULL];
    return [bodyJSONObject isEqual:@{@"a": @1, @"b": @2}];
});

@hugobast
Copy link
Copy Markdown

👍 This would be very helpful!

@marklarr
Copy link
Copy Markdown

This is awesome. It would be great to do the same thing for the path also,

http://service.com?a=1&b=2 == http://service.com?b=2&a=1

@lausobo
Copy link
Copy Markdown
Owner

lausobo commented Jun 24, 2015

See: #77 (comment)

@brow brow closed this Feb 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants