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

Support for custom mime types #87

Closed
cakejelly opened this issue Jun 21, 2021 · 4 comments
Closed

Support for custom mime types #87

cakejelly opened this issue Jun 21, 2021 · 4 comments

Comments

@cakejelly
Copy link
Contributor

I'm having trouble trying to write a mock that matches POST requests based on the request body and the issue seems to be a result of gock being a bit too strict when it comes to validating mime types.

From reading the source code I noticed that gock checks to see if the request content type header value matches a fixed list of supported mime types:

gock/matchers.go

Lines 21 to 28 in f77fde8

var BodyTypes = []string{
"text/html",
"text/plain",
"application/json",
"application/xml",
"multipart/form-data",
"application/x-www-form-urlencoded",
}
but the problem is that the API i'm writing a mock for uses a custom json mime type like application/vnd.apiname.v1+json, so it never matches.

Do you have any suggestions on how I could workaround this?

@h2non
Copy link
Owner

h2non commented Jun 21, 2021

This is a hard limitation, but ideally, the matcher should also accept potentially text-based MIME types or explicitly defined text-based MIME types via a new API mock method, e.g: mock.BodyTypes("application/vnd.apiname.v1+json")

Would you like to send a PR? That would possibly be the faster way to proceed forward.

@cakejelly
Copy link
Contributor Author

@h2non Thanks for the quick reply. After submitting the issue I discovered the ability to add custom matchers, which allowed me to workaround this limitation pretty easily. I think the new method you're suggesting would be a nice addition though and I'd be happy to submit a PR.

If I understand your suggestion correctly, this new method would be used in conjunction with a body matcher? E.g. something like:

gock.New("http://foo.com").
    Post("/bar").
    BodyTypes("application/vnd.apiname.v1+json").
    BodyString(`{"foo": "bar"}`).
    Reply(200).
    JSON(map[string]string{"foo": "bar"})

@h2non
Copy link
Owner

h2non commented Jun 25, 2021

That's exactly it. Happy to review and merge the PR!

@cakejelly
Copy link
Contributor Author

Closing as this is fixed now

#88
https://github.com/h2non/gock/releases/tag/v1.1.1

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

No branches or pull requests

2 participants