Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple matchers #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

stevenmatthewt
Copy link

@stevenmatthewt stevenmatthewt commented Sep 20, 2017

better diff view

I had a slightly confusing moment the other day when I was using gock. I had code very similar to the following:

gock.New("http://foo.com").
    Post("/bar").
    BodyString("bar").
    BodyString("baz").
    Reply(201).
    BodyString("intercepted")

res, err := http.Post("http://foo.com/bar", "application/json", bytes.NewBuffer([]byte(`{"baz":"foo"}`)))
// ...

To my surprise, gock did match the request. The source code makes the issue pretty clear: the second call to BodyString() just overwrote the first. This didn't seem like intuitive behavior to me.

This PR aims to remove this confusion by allowing multiple matchers (of the same or different types). I'm not necessarily finished with the PR; I think there might be a more generic/cleaner way of accomplishing this. But I wanted to go ahead and publish the PR so I could get some feedback on the general idea.

@tomas-fp
Copy link

This looks promising :)

@h2non h2non self-assigned this Sep 20, 2017
request.go Outdated
@@ -48,7 +48,7 @@ type Request struct {
Cookies []*http.Cookie

// BodyBuffer stores the body data to match.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct godoc: // BodyBuffer -> // BodyMatchers

request.go Outdated
return r
}

// BodyString defines the body to match based on a given string.
func (r *Request) BodyString(body string) *Request {
r.BodyBuffer = []byte(body)
var bodyBuffer []byte
bodyBuffer = []byte(body)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the single-line DRYer version: bodyBuffer := []byte(body) ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even: r.BodyMatchers = append(r.BodyMatchers, []byte(body))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do. Got a little too zealous with my copy/paste job :)

@h2non
Copy link
Owner

h2non commented Sep 20, 2017

After a quick review, it looks great. Thanks for adding the test cases too.

My only concern is that this introduces a different behavior that can also affects to other matchers, not just the body, so I want to think about it before introduce this feature.

Thanks for the PR!

@h2non
Copy link
Owner

h2non commented Sep 20, 2017

In essence, I feel like the final goal of this implementation is introducing a different approach of what is currently implemented in gock, where request.go method calls would basically register a matcher function in the registry for later matching.

Overtime I realized that this approach would be more flexible, so I have implemented it in pook, a sort of Python port of gock.

I'm not close to this port this approach into gock if that makes it more flexible (perhaps in v2).

@stevenmatthewt
Copy link
Author

I'm on vacation next week, so I'll probably have some time to do more work on this PR. I'm just going to mess around a bit and see if I can make some improvements (maybe even take a stab at implementing your concept of a deferred 'matcher function'). I'll keep you updated.

No worries if you don't want to merge.
I'm having fun just messing around in some code that's not work related :)

@h2non
Copy link
Owner

h2non commented Sep 21, 2017

Cool, any help would be very welcome. Feel free to update me here with your ideas or approches, so we can converge somehow.

@h2non
Copy link
Owner

h2non commented Sep 27, 2017

Any updates here? Just curious :)

@edwinthinks
Copy link
Contributor

Just tagging in here but I'am down to contribute as well =)! I like the your idea about registering matcher functions for later use.

@stevenmatthewt
Copy link
Author

Finally getting some time to start working on the rehaul of the matcher functions. I'll be making a separate PR when I've got something to share.

@h2non
Copy link
Owner

h2non commented Dec 21, 2017

Any update here?

@h2non h2non mentioned this pull request Dec 21, 2017
@edwinthinks
Copy link
Contributor

Hey @h2non I looked again at this PR and had a few thoughts about how to go forward from here. I don't think we should be defining multiple body matchers in a single matcher and I don't think it should be overwriting the previous definition either. Ideally, gock.New() would only register a very specific HTTP request. I am not 100% sure how you handled it in pook

I'll take a swing at this soon :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants