Skip to content

Commit be1e312

Browse files
committed
fix: adding more robust testing of consumable request body
1 parent 9dcabc8 commit be1e312

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

pkg/networking/middleware/response.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func NewReponseMiddleware(roundTriper http.RoundTripper, config configuration.Co
3131

3232
func (rm ResponseMiddleware) RoundTrip(req *http.Request) (*http.Response, error) {
3333
res, err := rm.next.RoundTrip(req)
34-
3534
if err != nil {
3635
return res, err
3736
}
@@ -43,7 +42,7 @@ func (rm ResponseMiddleware) RoundTrip(req *http.Request) (*http.Response, error
4342
return res, err
4443
}
4544

46-
// HandleResponse maps the response param to the eror catalog error.
45+
// HandleResponse maps the response param to the error catalog error.
4746
func HandleResponse(res *http.Response, config configuration.Configuration) error {
4847
if res == nil {
4948
return nil
@@ -75,6 +74,7 @@ func getErrorList(res *http.Response) []snyk_errors.Error {
7574
if err != nil {
7675
return []snyk_errors.Error{}
7776
}
77+
7878
res.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
7979

8080
errorList, err := snyk_errors.FromJSONAPIErrorBytes(bodyBytes)

pkg/networking/middleware/response_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middleware_test
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -204,6 +205,50 @@ func Test_ResponseMiddleware(t *testing.T) {
204205
assert.NoError(t, err)
205206
assert.Equal(t, http.StatusOK, res.StatusCode)
206207
})
208+
209+
t.Run("response body should be consumable after roundtrip", func(t *testing.T) {
210+
testCases := []struct {
211+
name string
212+
urlPath string
213+
expectError bool
214+
}{
215+
{
216+
name: "no error",
217+
urlPath: "",
218+
expectError: false,
219+
},
220+
{
221+
name: "with error",
222+
urlPath: "/error-catalog",
223+
expectError: true,
224+
},
225+
}
226+
227+
for _, tc := range testCases {
228+
t.Run(tc.name, func(t *testing.T) {
229+
config := getBaseConfig()
230+
config.Set(configuration.AUTHENTICATION_ADDITIONAL_URLS, []string{server.URL})
231+
232+
rt := middleware.NewReponseMiddleware(http.DefaultTransport, config, errHandler)
233+
req := buildRequest(server.URL + tc.urlPath)
234+
res, err := rt.RoundTrip(req)
235+
236+
assert.NotNil(t, res)
237+
if tc.expectError {
238+
assert.Error(t, err)
239+
} else {
240+
assert.NoError(t, err)
241+
}
242+
243+
bodyBytes, err := io.ReadAll(res.Body)
244+
assert.NoError(t, err, "Body should be readable")
245+
assert.NotNil(t, bodyBytes, "Should be able to read body")
246+
247+
err = res.Body.Close()
248+
assert.NoError(t, err, "Body should close without errors")
249+
})
250+
}
251+
})
207252
}
208253

209254
func buildRequest(url string) *http.Request {

0 commit comments

Comments
 (0)