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

Using .JSON forces a header match which isn't always wanted #82

Open
cameronbraid opened this issue Feb 6, 2021 · 1 comment
Open

Comments

@cameronbraid
Copy link

gock.New(url).JSON(...)

Forces a header check for "Content-Type", "application/json"

I am testing a client that sends "content-type" with lower case C and T so this will never match

One solution could be to document the JSON method as adding a header and add a new method JSONBody

// JSON defines the JSON body to match based on a given structure, and adds a matcher for content type
func (r *Request) JSON(data interface{}) *Request {
	if r.Header.Get("Content-Type") == "" {
		r.Header.Set("Content-Type", "application/json")
	}
	return r.JSONBody(data)
}

// JSON defines the JSON body to match based on a given structure
func (r *Request) JSONBody(data interface{}) *Request {
	r.BodyBuffer, r.Error = readAndDecode(data, "json")
	return r
}

@cameronbraid cameronbraid changed the title Using .JSON forces a header match Using .JSON forces a header match which isn't always wanted Feb 6, 2021
@teemuniiranen
Copy link

teemuniiranen commented Mar 14, 2022

I tried to mock AWS SDK for Golang (aws-sdk-go/1.42.39) and noticed this problem. AWS is sending Content-Type: application/x-amz-json-1.1. I would separate totally MatchHeader from the body matching.

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