@@ -3,6 +3,7 @@ package middleware_test
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io"
6
7
"net/http"
7
8
"net/http/httptest"
8
9
"testing"
@@ -46,6 +47,8 @@ func Test_ResponseMiddleware(t *testing.T) {
46
47
assert .Nil (t , err )
47
48
default :
48
49
w .WriteHeader (http .StatusOK )
50
+ _ , err := w .Write ([]byte ("hello" ))
51
+ assert .Nil (t , err )
49
52
}
50
53
})
51
54
server := httptest .NewServer (handler )
@@ -77,7 +80,7 @@ func Test_ResponseMiddleware(t *testing.T) {
77
80
req := buildRequest (url )
78
81
res , err := rt .RoundTrip (req )
79
82
80
- assert .Nil (t , res )
83
+ assert .NotNil (t , res )
81
84
assert .ErrorAs (t , err , & snykErr )
82
85
assert .Equal (t , code , snykErr .StatusCode )
83
86
@@ -121,7 +124,7 @@ func Test_ResponseMiddleware(t *testing.T) {
121
124
req := buildRequest (server .URL + "/jsonapi-SNYK-0003" )
122
125
res , err := rt .RoundTrip (req )
123
126
124
- assert .Nil (t , res )
127
+ assert .NotNil (t , res )
125
128
assert .Error (t , err )
126
129
127
130
actual := snyk_errors.Error {}
@@ -138,7 +141,7 @@ func Test_ResponseMiddleware(t *testing.T) {
138
141
req := buildRequest (server .URL + "/jsonapi-SNYK-0000" )
139
142
res , err := rt .RoundTrip (req )
140
143
141
- assert .Nil (t , res )
144
+ assert .NotNil (t , res )
142
145
assert .Error (t , err )
143
146
144
147
actual := snyk_errors.Error {}
@@ -155,7 +158,7 @@ func Test_ResponseMiddleware(t *testing.T) {
155
158
req := buildRequest (server .URL + "/error-catalog" )
156
159
res , err := rt .RoundTrip (req )
157
160
158
- assert .Nil (t , res )
161
+ assert .NotNil (t , res )
159
162
assert .Error (t , err )
160
163
161
164
expected := snyk .NewBadGatewayError ("" )
@@ -204,6 +207,50 @@ func Test_ResponseMiddleware(t *testing.T) {
204
207
assert .NoError (t , err )
205
208
assert .Equal (t , http .StatusOK , res .StatusCode )
206
209
})
210
+
211
+ t .Run ("response body should be consumable after roundtrip" , func (t * testing.T ) {
212
+ testCases := []struct {
213
+ name string
214
+ urlPath string
215
+ expectError bool
216
+ }{
217
+ {
218
+ name : "no error" ,
219
+ urlPath : "" ,
220
+ expectError : false ,
221
+ },
222
+ {
223
+ name : "with error" ,
224
+ urlPath : "/error-catalog" ,
225
+ expectError : true ,
226
+ },
227
+ }
228
+
229
+ for _ , tc := range testCases {
230
+ t .Run (tc .name , func (t * testing.T ) {
231
+ config := getBaseConfig ()
232
+ config .Set (configuration .AUTHENTICATION_ADDITIONAL_URLS , []string {server .URL })
233
+
234
+ rt := middleware .NewReponseMiddleware (http .DefaultTransport , config , errHandler )
235
+ req := buildRequest (server .URL + tc .urlPath )
236
+ res , err := rt .RoundTrip (req )
237
+
238
+ assert .NotNil (t , res )
239
+ if tc .expectError {
240
+ assert .Error (t , err )
241
+ } else {
242
+ assert .NoError (t , err )
243
+ }
244
+
245
+ bodyBytes , err := io .ReadAll (res .Body )
246
+ assert .NoError (t , err , "Body should be readable" )
247
+ assert .NotEmpty (t , bodyBytes , "Should be able to read body" )
248
+
249
+ err = res .Body .Close ()
250
+ assert .NoError (t , err , "Body should close without errors" )
251
+ })
252
+ }
253
+ })
207
254
}
208
255
209
256
func buildRequest (url string ) * http.Request {
0 commit comments