X Tutup
Skip to content

Commit aa8f8e8

Browse files
committed
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>
1 parent 15a7ab6 commit aa8f8e8

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

pkg/httpmock/legacy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ import (
1212
// TODO: clean up methods in this file when there are no more callers
1313

1414
func (r *Registry) StubResponse(status int, body io.Reader) {
15-
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
16-
return httpResponse(status, body), nil
15+
r.Register(MatchAny, func(req *http.Request) (*http.Response, error) {
16+
return httpResponse(status, req, body), nil
1717
})
1818
}
1919

2020
func (r *Registry) StubWithFixture(status int, fixtureFileName string) func() {
2121
fixturePath := path.Join("../test/fixtures/", fixtureFileName)
2222
fixtureFile, err := os.Open(fixturePath)
23-
r.Register(MatchAny, func(*http.Request) (*http.Response, error) {
23+
r.Register(MatchAny, func(req *http.Request) (*http.Response, error) {
2424
if err != nil {
2525
return nil, err
2626
}
27-
return httpResponse(200, fixtureFile), nil
27+
return httpResponse(200, req, fixtureFile), nil
2828
})
2929
return func() {
3030
if err == nil {

pkg/httpmock/stub.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func decodeJSONBody(req *http.Request, dest interface{}) error {
5959
}
6060

6161
func StringResponse(body string) Responder {
62-
return func(*http.Request) (*http.Response, error) {
63-
return httpResponse(200, bytes.NewBufferString(body)), nil
62+
return func(req *http.Request) (*http.Response, error) {
63+
return httpResponse(200, req, bytes.NewBufferString(body)), nil
6464
}
6565
}
6666

6767
func JSONResponse(body interface{}) Responder {
68-
return func(*http.Request) (*http.Response, error) {
68+
return func(req *http.Request) (*http.Response, error) {
6969
b, _ := json.Marshal(body)
70-
return httpResponse(200, bytes.NewBuffer(b)), nil
70+
return httpResponse(200, req, bytes.NewBuffer(b)), nil
7171
}
7272
}
7373

@@ -84,7 +84,7 @@ func GraphQLMutation(body string, cb func(map[string]interface{})) Responder {
8484
}
8585
cb(bodyData.Variables.Input)
8686

87-
return httpResponse(200, bytes.NewBufferString(body)), nil
87+
return httpResponse(200, req, bytes.NewBufferString(body)), nil
8888
}
8989
}
9090

@@ -100,13 +100,14 @@ func GraphQLQuery(body string, cb func(string, map[string]interface{})) Responde
100100
}
101101
cb(bodyData.Query, bodyData.Variables)
102102

103-
return httpResponse(200, bytes.NewBufferString(body)), nil
103+
return httpResponse(200, req, bytes.NewBufferString(body)), nil
104104
}
105105
}
106106

107-
func httpResponse(status int, body io.Reader) *http.Response {
107+
func httpResponse(status int, req *http.Request, body io.Reader) *http.Response {
108108
return &http.Response{
109109
StatusCode: status,
110+
Request: req,
110111
Body: ioutil.NopCloser(body),
111112
}
112113
}

0 commit comments

Comments
 (0)
X Tutup