jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

Assertions on request bodies? #43

Closed bimargulies-google closed 6 years ago

bimargulies-google commented 6 years ago

It would be convenient to have a way to add code to the mock that checks the request and asserts on it. Would you consider patches in this direction?

dlebech commented 6 years ago

It's possible to do assertions in mocks if you define your own response function, for example:

func MyTest(t *testing.T) {
    httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
        func(r *http.Request (*http.Response, error) {
            if (r.UserAgent() != "agent-007") {
                t.Error("Expected Agent 007 as user agent")
            }
            // More response code
        },
    )
}

Does this work for your use case?

bimargulies-google commented 6 years ago

Yes, thanks.