@@ -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"
@@ -204,6 +205,50 @@ func Test_ResponseMiddleware(t *testing.T) {
204
205
assert .NoError (t , err )
205
206
assert .Equal (t , http .StatusOK , res .StatusCode )
206
207
})
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
+ })
207
252
}
208
253
209
254
func buildRequest (url string ) * http.Request {
0 commit comments