@@ -3,6 +3,7 @@ package openai_compat
33import (
44 "bytes"
55 "encoding/json"
6+ "fmt"
67 "io"
78 "net/http"
89 "net/http/httptest"
@@ -235,69 +236,57 @@ func TestProviderChat_JSONHTTPErrorDoesNotReportHTML(t *testing.T) {
235236 }
236237}
237238
238- func TestProviderChat_HTMLSuccessResponseReturnsHelpfulError (t * testing.T ) {
239- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
240- w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
241- w .WriteHeader (http .StatusOK )
242- _ , _ = w .Write ([]byte ("<!DOCTYPE html><html><body>gateway login</body></html>" ))
243- }))
244- defer server .Close ()
245-
246- p := NewProvider ("key" , server .URL , "" )
247- _ , err := p .Chat (t .Context (), []Message {{Role : "user" , Content : "hi" }}, nil , "gpt-4o" , nil )
248- if err == nil {
249- t .Fatal ("expected error, got nil" )
250- }
251- if ! strings .Contains (err .Error (), "returned HTML instead of JSON" ) {
252- t .Fatalf ("expected helpful HTML error, got %v" , err )
253- }
254- if ! strings .Contains (err .Error (), "check api_base or proxy configuration" ) {
255- t .Fatalf ("expected configuration hint, got %v" , err )
256- }
257- }
258-
259- func TestProviderChat_HTMLErrorResponseReturnsHelpfulError (t * testing.T ) {
260- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
261- w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
262- w .WriteHeader (http .StatusBadGateway )
263- _ , _ = w .Write ([]byte ("<!DOCTYPE html><html><body>bad gateway</body></html>" ))
264- }))
265- defer server .Close ()
266-
267- p := NewProvider ("key" , server .URL , "" )
268- _ , err := p .Chat (t .Context (), []Message {{Role : "user" , Content : "hi" }}, nil , "gpt-4o" , nil )
269- if err == nil {
270- t .Fatal ("expected error, got nil" )
271- }
272- if ! strings .Contains (err .Error (), "Status: 502" ) {
273- t .Fatalf ("expected status code in error, got %v" , err )
274- }
275- if ! strings .Contains (err .Error (), "returned HTML instead of JSON" ) {
276- t .Fatalf ("expected helpful HTML error, got %v" , err )
277- }
278- if ! strings .Contains (err .Error (), "check api_base or proxy configuration" ) {
279- t .Fatalf ("expected configuration hint, got %v" , err )
239+ func TestProviderChat_HTMLResponsesReturnHelpfulError (t * testing.T ) {
240+ tests := []struct {
241+ name string
242+ contentType string
243+ statusCode int
244+ body string
245+ }{
246+ {
247+ name : "html success response" ,
248+ contentType : "text/html; charset=utf-8" ,
249+ statusCode : http .StatusOK ,
250+ body : "<!DOCTYPE html><html><body>gateway login</body></html>" ,
251+ },
252+ {
253+ name : "html error response" ,
254+ contentType : "text/html; charset=utf-8" ,
255+ statusCode : http .StatusBadGateway ,
256+ body : "<!DOCTYPE html><html><body>bad gateway</body></html>" ,
257+ },
258+ {
259+ name : "mislabeled html success response" ,
260+ contentType : "application/json" ,
261+ statusCode : http .StatusOK ,
262+ body : " \r \n \t <!DOCTYPE html><html><body>gateway login</body></html>" ,
263+ },
280264 }
281- }
282265
283- func TestProviderChat_MislabeledHTMLSuccessResponseReturnsHelpfulError (t * testing.T ) {
284- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
285- w .Header ().Set ("Content-Type" , "application/json" )
286- w .WriteHeader (http .StatusOK )
287- _ , _ = w .Write ([]byte (" \r \n \t <!DOCTYPE html><html><body>gateway login</body></html>" ))
288- }))
289- defer server .Close ()
266+ for _ , tt := range tests {
267+ t .Run (tt .name , func (t * testing.T ) {
268+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
269+ w .Header ().Set ("Content-Type" , tt .contentType )
270+ w .WriteHeader (tt .statusCode )
271+ _ , _ = w .Write ([]byte (tt .body ))
272+ }))
273+ defer server .Close ()
290274
291- p := NewProvider ("key" , server .URL , "" )
292- _ , err := p .Chat (t .Context (), []Message {{Role : "user" , Content : "hi" }}, nil , "gpt-4o" , nil )
293- if err == nil {
294- t .Fatal ("expected error, got nil" )
295- }
296- if ! strings .Contains (err .Error (), "returned HTML instead of JSON" ) {
297- t .Fatalf ("expected helpful HTML error, got %v" , err )
298- }
299- if ! strings .Contains (err .Error (), "check api_base or proxy configuration" ) {
300- t .Fatalf ("expected configuration hint, got %v" , err )
275+ p := NewProvider ("key" , server .URL , "" )
276+ _ , err := p .Chat (t .Context (), []Message {{Role : "user" , Content : "hi" }}, nil , "gpt-4o" , nil )
277+ if err == nil {
278+ t .Fatal ("expected error, got nil" )
279+ }
280+ if ! strings .Contains (err .Error (), fmt .Sprintf ("Status: %d" , tt .statusCode )) {
281+ t .Fatalf ("expected status code in error, got %v" , err )
282+ }
283+ if ! strings .Contains (err .Error (), "returned HTML instead of JSON" ) {
284+ t .Fatalf ("expected helpful HTML error, got %v" , err )
285+ }
286+ if ! strings .Contains (err .Error (), "check api_base or proxy configuration" ) {
287+ t .Fatalf ("expected configuration hint, got %v" , err )
288+ }
289+ })
301290 }
302291}
303292
0 commit comments