httpmock: propagate original HTTP request to HTTP response

So that it's possible to access it in mocked HTTP tests.

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
This commit is contained in:
Pavel Borzenkov 2020-06-27 18:47:06 +03:00
parent 15a7ab6b92
commit aa8f8e8904
2 changed files with 12 additions and 11 deletions

View file

@ -12,19 +12,19 @@ import (
// TODO: clean up methods in this file when there are no more callers
func (r *Registry) StubResponse(status int, body io.Reader) {
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
return httpResponse(status, body), nil
r.Register(MatchAny, func(req *http.Request) (*http.Response, error) {
return httpResponse(status, req, body), nil
})
}
func (r *Registry) StubWithFixture(status int, fixtureFileName string) func() {
fixturePath := path.Join("../test/fixtures/", fixtureFileName)
fixtureFile, err := os.Open(fixturePath)
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
r.Register(MatchAny, func(req *http.Request) (*http.Response, error) {
if err != nil {
return nil, err
}
return httpResponse(200, fixtureFile), nil
return httpResponse(200, req, fixtureFile), nil
})
return func() {
if err == nil {

View file

@ -59,15 +59,15 @@ func decodeJSONBody(req *http.Request, dest interface{}) error {
}
func StringResponse(body string) Responder {
return func(*http.Request) (*http.Response, error) {
return httpResponse(200, bytes.NewBufferString(body)), nil
return func(req *http.Request) (*http.Response, error) {
return httpResponse(200, req, bytes.NewBufferString(body)), nil
}
}
func JSONResponse(body interface{}) Responder {
return func(*http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {
b, _ := json.Marshal(body)
return httpResponse(200, bytes.NewBuffer(b)), nil
return httpResponse(200, req, bytes.NewBuffer(b)), nil
}
}
@ -84,7 +84,7 @@ func GraphQLMutation(body string, cb func(map[string]interface{})) Responder {
}
cb(bodyData.Variables.Input)
return httpResponse(200, bytes.NewBufferString(body)), nil
return httpResponse(200, req, bytes.NewBufferString(body)), nil
}
}
@ -100,13 +100,14 @@ func GraphQLQuery(body string, cb func(string, map[string]interface{})) Responde
}
cb(bodyData.Query, bodyData.Variables)
return httpResponse(200, bytes.NewBufferString(body)), nil
return httpResponse(200, req, bytes.NewBufferString(body)), nil
}
}
func httpResponse(status int, body io.Reader) *http.Response {
func httpResponse(status int, req *http.Request, body io.Reader) *http.Response {
return &http.Response{
StatusCode: status,
Request: req,
Body: ioutil.NopCloser(body),
}
}