jarcoal/httpmock

Responder based on the Request header

Nikhila19 opened this issue · 1 comments

I'm trying to customize the responder based on the headers in the request. There, however, does not seem to be a way to do this.

It is not possible at the moment. But you still can register a responder and do the dispatch yourself, as in:

myHeaderResponder := func(r *http.Request) (*http.Response, error) { ... }
// or myHeaderResponder := httpmock.NewStringResponder(200, "hello world")
// or any other pre-built responder...

httpmock.RegisterResponder("GET", "http://example.com/",
  func(r *http.Request) (*http.Response, error) {
    if r.Header.Get("X-My-Header") == "foobar" {
      return myHeaderResponder(r)
    }
    return httpmock.ConnectionFailure(r) // httpmock default responder
  })

If you have an idea to integrate this in a more generic way, please share :)