@@ -16,6 +16,11 @@ import (
16
16
"github.com/sashabaranov/go-openai/jsonschema"
17
17
)
18
18
19
+ const (
20
+ xCustomHeader = "X-CUSTOM-HEADER"
21
+ xCustomHeaderValue = "test"
22
+ )
23
+
19
24
func TestChatCompletionsWrongModel (t * testing.T ) {
20
25
config := DefaultConfig ("whatever" )
21
26
config .BaseURL = "http://localhost/v1"
@@ -68,6 +73,30 @@ func TestChatCompletions(t *testing.T) {
68
73
checks .NoError (t , err , "CreateChatCompletion error" )
69
74
}
70
75
76
+ // TestCompletions Tests the completions endpoint of the API using the mocked server.
77
+ func TestChatCompletionsWithHeaders (t * testing.T ) {
78
+ client , server , teardown := setupOpenAITestServer ()
79
+ defer teardown ()
80
+ server .RegisterHandler ("/v1/chat/completions" , handleChatCompletionEndpoint )
81
+ resp , err := client .CreateChatCompletion (context .Background (), ChatCompletionRequest {
82
+ MaxTokens : 5 ,
83
+ Model : GPT3Dot5Turbo ,
84
+ Messages : []ChatCompletionMessage {
85
+ {
86
+ Role : ChatMessageRoleUser ,
87
+ Content : "Hello!" ,
88
+ },
89
+ },
90
+ })
91
+ checks .NoError (t , err , "CreateChatCompletion error" )
92
+
93
+ a := resp .Header ().Get (xCustomHeader )
94
+ _ = a
95
+ if resp .Header ().Get (xCustomHeader ) != xCustomHeaderValue {
96
+ t .Errorf ("expected header %s to be %s" , xCustomHeader , xCustomHeaderValue )
97
+ }
98
+ }
99
+
71
100
// TestChatCompletionsFunctions tests including a function call.
72
101
func TestChatCompletionsFunctions (t * testing.T ) {
73
102
client , server , teardown := setupOpenAITestServer ()
@@ -281,6 +310,7 @@ func handleChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
281
310
TotalTokens : inputTokens + completionTokens ,
282
311
}
283
312
resBytes , _ = json .Marshal (res )
313
+ w .Header ().Set (xCustomHeader , xCustomHeaderValue )
284
314
fmt .Fprintln (w , string (resBytes ))
285
315
}
286
316
0 commit comments