-
Notifications
You must be signed in to change notification settings - Fork 13
/
client_test.go
66 lines (53 loc) · 1.71 KB
/
client_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
57
58
59
60
61
62
63
64
65
66
package chatgpt
import (
"context"
"fmt"
"net/http"
"testing"
chatgpt_errors "github.com/ayush6624/go-chatgpt/utils"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
_, err := NewClient("test-apikey")
if err != nil {
t.Errorf("NewClient() error = %v", err)
}
_, err = NewClient("")
assert.NotNil(t, err)
assert.Equal(t, err, chatgpt_errors.ErrAPIKeyRequired)
_, err = NewClientWithConfig(&Config{})
assert.NotNil(t, err)
assert.Equal(t, err, chatgpt_errors.ErrAPIKeyRequired)
_, err = NewClientWithConfig(&Config{
APIKey: "test-apikey",
})
assert.Nil(t, err)
}
func TestClient2_sendRequest(t *testing.T) {
// Create a new test HTTP server and client to handle requests
testServer, client := newTestServerAndClient()
// Create a new request
req, err := http.NewRequest("POST", testServer.URL, nil)
assert.NoError(t, err)
// Send the request using the ChatGPT client
res, err := client.sendRequest(context.Background(), req)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
expectedHeader := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", "mock_api_key"),
"OpenAI-Organization": "mock_organization_id",
"Content-Type": "application/json",
"Accept": "application/json",
}
// Check that the request's header is set correctly, after sendRequest was called
for key, value := range expectedHeader {
assert.Equal(t, req.Header.Get(key), value)
}
testServer, client = newTestClientWithInvalidStatusCode()
// Prepare a test request
req, err = http.NewRequest("GET", testServer.URL, nil)
assert.NoError(t, err)
resp, err := client.sendRequest(context.Background(), req)
assert.Error(t, err)
assert.Nil(t, resp)
}