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

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

Open cameronbraid opened 3 years ago

cameronbraid commented 3 years ago
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
}
teemuniiranen commented 2 years ago

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.