@@ -15,6 +15,13 @@ import (
15
15
"github.com/sashabaranov/go-openai/internal/test/checks"
16
16
)
17
17
18
+ // badMarshaler produces invalid JSON when marshaled.
19
+ type badMarshaler struct {}
20
+
21
+ func (badMarshaler ) MarshalJSON () ([]byte , error ) {
22
+ return []byte ("{" ), nil
23
+ }
24
+
18
25
func TestEmbedding (t * testing.T ) {
19
26
embeddedModels := []openai.EmbeddingModel {
20
27
openai .AdaSimilarity ,
@@ -325,3 +332,21 @@ func TestDotProduct(t *testing.T) {
325
332
t .Errorf ("Expected Vector Length Mismatch Error, but got: %v" , err )
326
333
}
327
334
}
335
+
336
+ // TestCreateEmbeddings_UnmarshalError verifies that an error is returned when
337
+ // the JSON bytes produced by marshaling the request cannot be unmarshaled back
338
+ // into a map.
339
+ func TestCreateEmbeddings_UnmarshalError (t * testing.T ) {
340
+ client , server , teardown := setupOpenAITestServer ()
341
+ defer teardown ()
342
+
343
+ server .RegisterHandler ("/v1/embeddings" , func (w http.ResponseWriter , _ * http.Request ) {
344
+ w .WriteHeader (http .StatusOK )
345
+ fmt .Fprint (w , `{"data":[]}` )
346
+ })
347
+
348
+ _ , err := client .CreateEmbeddings (context .Background (), openai.EmbeddingRequest {
349
+ Input : badMarshaler {},
350
+ })
351
+ checks .HasError (t , err , "CreateEmbeddings should fail on unmarshal error" )
352
+ }
0 commit comments