h2non / gock

HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
https://pkg.go.dev/github.com/h2non/gock
MIT License
2.09k stars 107 forks source link

Lack of clarity about the usage of `gock.New` and how it only matches hosts #111

Open zacharyarnaise opened 1 year ago

zacharyarnaise commented 1 year ago

I believe that the documentation isn't clear enough about the fact that only hosts should be passed to gock.New. It's even more confusing because gock won't raise any errors if you do.

The way things are actually, a gock like this:

gock.New("https://foobar.test/api/v1").
    Get("/some-endpoint").
    Reply(200)

Would match any requests with such URLs: https://foobar.test/<this could be anything>/some-endpoint.

I think that the documentation should make this behaviour more apparent and since it's an expected behaviour, gock.New should raise an error if any part of URL that isn't a host is passed to it.

ImFlog commented 2 days ago

I also stumbled upon an issue of the same kind I think.

If I have an URL that looks like: https://somewhere-over-the-rainbow.com And another one like: https://tsomewhere-over-the-rainbow.com

If i define mocks on each URLs, they may be matched by the first URL (without the t). There is probably something that checks with wildcard before the host or something ? This was quite a confusing debug moment 😅

Edit: after looking at the documentation again it seems that the option DisableRegexpHost might be what I was looking for ?