@@ -23,12 +23,15 @@ func TestSendRequest_success(t *testing.T) {
2323 }, nil
2424 }))
2525
26- got , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
26+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
2727 if err != nil {
2828 t .Fatalf ("unexpected error: %v" , err )
2929 }
30- if string (got ) != body {
31- t .Errorf ("body = %q, want %q" , string (got ), body )
30+ if string (resp .Body ) != body {
31+ t .Errorf ("body = %q, want %q" , string (resp .Body ), body )
32+ }
33+ if resp .StatusCode != http .StatusOK {
34+ t .Errorf ("statusCode = %d, want %d" , resp .StatusCode , http .StatusOK )
3235 }
3336}
3437
@@ -68,7 +71,7 @@ func TestSendRequest_rateLimitWithoutRetryAfter(t *testing.T) {
6871 }, nil
6972 }))
7073
71- body , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
74+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
7275 if err == nil {
7376 t .Fatal ("expected error for 429 response" )
7477 }
@@ -79,11 +82,17 @@ func TestSendRequest_rateLimitWithoutRetryAfter(t *testing.T) {
7982 if ce .ErrorCategory != asyncapi .ErrCategoryRateLimit {
8083 t .Errorf ("category = %s, want %s" , ce .ErrorCategory , asyncapi .ErrCategoryRateLimit )
8184 }
85+ if ce .StatusCode != http .StatusTooManyRequests {
86+ t .Errorf ("StatusCode = %d, want %d" , ce .StatusCode , http .StatusTooManyRequests )
87+ }
88+ if resp .StatusCode != http .StatusTooManyRequests {
89+ t .Errorf ("returned statusCode = %d, want %d" , resp .StatusCode , http .StatusTooManyRequests )
90+ }
8291 if ce .RetryAfter != 0 {
8392 t .Errorf ("RetryAfter = %v, want 0 (no header)" , ce .RetryAfter )
8493 }
85- if string (body ) != respBody {
86- t .Errorf ("body = %q, want %q" , string (body ), respBody )
94+ if string (resp . Body ) != respBody {
95+ t .Errorf ("body = %q, want %q" , string (resp . Body ), respBody )
8796 }
8897}
8998
@@ -120,7 +129,7 @@ func TestSendRequest_clientError(t *testing.T) {
120129 }, nil
121130 }))
122131
123- body , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
132+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
124133 if err == nil {
125134 t .Fatal ("expected error for 400 response" )
126135 }
@@ -131,7 +140,13 @@ func TestSendRequest_clientError(t *testing.T) {
131140 if ce .ErrorCategory != asyncapi .ErrCategoryInvalidReq {
132141 t .Errorf ("category = %s, want %s" , ce .ErrorCategory , asyncapi .ErrCategoryInvalidReq )
133142 }
134- if len (body ) == 0 {
143+ if ce .StatusCode != http .StatusBadRequest {
144+ t .Errorf ("StatusCode = %d, want %d" , ce .StatusCode , http .StatusBadRequest )
145+ }
146+ if resp .StatusCode != http .StatusBadRequest {
147+ t .Errorf ("returned statusCode = %d, want %d" , resp .StatusCode , http .StatusBadRequest )
148+ }
149+ if len (resp .Body ) == 0 {
135150 t .Error ("expected response body to be returned with 4xx error" )
136151 }
137152}
@@ -145,7 +160,7 @@ func TestSendRequest_serverError(t *testing.T) {
145160 }, nil
146161 }))
147162
148- body , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
163+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
149164 if err == nil {
150165 t .Fatal ("expected error for 500 response" )
151166 }
@@ -156,7 +171,13 @@ func TestSendRequest_serverError(t *testing.T) {
156171 if ce .ErrorCategory != asyncapi .ErrCategoryServer {
157172 t .Errorf ("category = %s, want %s" , ce .ErrorCategory , asyncapi .ErrCategoryServer )
158173 }
159- if len (body ) == 0 {
174+ if ce .StatusCode != http .StatusInternalServerError {
175+ t .Errorf ("StatusCode = %d, want %d" , ce .StatusCode , http .StatusInternalServerError )
176+ }
177+ if resp .StatusCode != http .StatusInternalServerError {
178+ t .Errorf ("returned statusCode = %d, want %d" , resp .StatusCode , http .StatusInternalServerError )
179+ }
180+ if len (resp .Body ) == 0 {
160181 t .Error ("expected response body to be returned with 5xx error" )
161182 }
162183}
@@ -166,7 +187,7 @@ func TestSendRequest_transportError(t *testing.T) {
166187 return nil , fmt .Errorf ("connection refused" )
167188 }))
168189
169- _ , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
190+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
170191 if err == nil {
171192 t .Fatal ("expected error for transport failure" )
172193 }
@@ -177,6 +198,12 @@ func TestSendRequest_transportError(t *testing.T) {
177198 if ce .ErrorCategory != asyncapi .ErrCategoryUnknown {
178199 t .Errorf ("category = %s, want %s" , ce .ErrorCategory , asyncapi .ErrCategoryUnknown )
179200 }
201+ if ce .StatusCode != 0 {
202+ t .Errorf ("StatusCode = %d, want 0 for transport error" , ce .StatusCode )
203+ }
204+ if resp != nil {
205+ t .Errorf ("resp = %+v, want nil for transport error" , resp )
206+ }
180207}
181208
182209func TestSendRequest_invalidURL (t * testing.T ) {
@@ -220,6 +247,43 @@ func TestSendRequest_contextCancellation(t *testing.T) {
220247 }
221248}
222249
250+ func TestSendRequest_bodyReadFailurePreservesStatusCode (t * testing.T ) {
251+ client := NewHTTPInferenceClient (NewTestClient (func (req * http.Request ) (* http.Response , error ) {
252+ return & http.Response {
253+ StatusCode : http .StatusOK ,
254+ Body : io .NopCloser (& failReader {}),
255+ Header : make (http.Header ),
256+ }, nil
257+ }))
258+
259+ resp , err := client .SendRequest (context .Background (), "http://localhost/v1/completions" , nil , []byte (`{}` ))
260+ if err == nil {
261+ t .Fatal ("expected error for body read failure" )
262+ }
263+ var ce * asyncapi.ClientError
264+ if ! errors .As (err , & ce ) {
265+ t .Fatalf ("expected *ClientError, got %T" , err )
266+ }
267+ if ce .ErrorCategory != asyncapi .ErrCategoryServer {
268+ t .Errorf ("category = %s, want %s" , ce .ErrorCategory , asyncapi .ErrCategoryServer )
269+ }
270+ if resp == nil {
271+ t .Fatal ("expected non-nil response when HTTP response was received" )
272+ }
273+ if resp .StatusCode != http .StatusOK {
274+ t .Errorf ("returned statusCode = %d, want %d (response was received)" , resp .StatusCode , http .StatusOK )
275+ }
276+ if ce .StatusCode != http .StatusOK {
277+ t .Errorf ("ClientError.StatusCode = %d, want %d" , ce .StatusCode , http .StatusOK )
278+ }
279+ }
280+
281+ type failReader struct {}
282+
283+ func (f * failReader ) Read ([]byte ) (int , error ) {
284+ return 0 , fmt .Errorf ("simulated read error" )
285+ }
286+
223287func TestNewHTTPInferenceClient (t * testing.T ) {
224288 httpClient := & http.Client {}
225289 client := NewHTTPInferenceClient (httpClient )
0 commit comments