forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models_test.go
56 lines (47 loc) · 1.47 KB
/
models_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package openai_test
import (
. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test"
"github.com/sashabaranov/go-openai/internal/test/checks"
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
)
// TestListModels Tests the models endpoint of the API using the mocked server.
func TestListModels(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/v1/models", handleModelsEndpoint)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()
config := DefaultConfig(test.GetTestToken())
config.BaseURL = ts.URL + "/v1"
client := NewClientWithConfig(config)
ctx := context.Background()
_, err = client.ListModels(ctx)
checks.NoError(t, err, "ListModels error")
}
func TestAzureListModels(t *testing.T) {
server := test.NewTestServer()
server.RegisterHandler("/openai/models", handleModelsEndpoint)
// create the test server
var err error
ts := server.OpenAITestServer()
ts.Start()
defer ts.Close()
config := DefaultAzureConfig(test.GetTestToken(), "https://dummylab.openai.azure.com/")
config.BaseURL = ts.URL
client := NewClientWithConfig(config)
ctx := context.Background()
_, err = client.ListModels(ctx)
checks.NoError(t, err, "ListModels error")
}
// handleModelsEndpoint Handles the models endpoint by the test server.
func handleModelsEndpoint(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(ModelsList{})
fmt.Fprintln(w, string(resBytes))
}