h2non / gock

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

feat(matchers): Add custom body type matcher #88

Closed cakejelly closed 2 years ago

cakejelly commented 3 years ago

PR to add the custom body type matcher that was proposed in https://github.com/h2non/gock/issues/87

The logic is pretty simple - If a user passes a non-empty mime type to the BodyType function, then the Content-Type header on the http request must be the same for there to be a match. If there's no mime type passed then it falls back to the the existing logic of checking against the hardcoded list of supported mime types.

One thing I'm a bit unsure of - I discovered MatchType and now I feel like adding this additional method will be confusing to people, since there will be two separate methods for matching based on the Content-Type header but the behaviour is different.

@h2non Any thoughts on this?

h2non commented 3 years ago

True. It is somehow redundant and can lead to confusion. I believe we should keep MatchType and simply pass the MIME type to match to supportedType function.

cakejelly commented 3 years ago

@h2non makes sense and agreed, I think it's much cleaner to just keep MatchType. Pushed a new commit with your suggestion, please take another look when you get the chance 🙃 thanks.

h2non commented 2 years ago

Thank you!