forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_builder_test.go
61 lines (53 loc) · 1.63 KB
/
request_builder_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
package openai //nolint:testpackage // testing private field
import (
"bytes"
"context"
"errors"
"net/http"
"reflect"
"testing"
)
var errTestMarshallerFailed = errors.New("test marshaller failed")
type failingMarshaller struct{}
func (*failingMarshaller) Marshal(_ any) ([]byte, error) {
return []byte{}, errTestMarshallerFailed
}
func TestRequestBuilderReturnsMarshallerErrors(t *testing.T) {
builder := HTTPRequestBuilder{
marshaller: &failingMarshaller{},
}
_, err := builder.Build(context.Background(), "", "", struct{}{}, nil)
if !errors.Is(err, errTestMarshallerFailed) {
t.Fatalf("Did not return error when marshaller failed: %v", err)
}
}
func TestRequestBuilderReturnsRequest(t *testing.T) {
b := NewRequestBuilder()
var (
ctx = context.Background()
method = http.MethodPost
url = "/foo"
request = map[string]string{"foo": "bar"}
reqBytes, _ = b.marshaller.Marshal(request)
want, _ = http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(reqBytes))
)
got, _ := b.Build(ctx, method, url, request, nil)
if !reflect.DeepEqual(got.Body, want.Body) ||
!reflect.DeepEqual(got.URL, want.URL) ||
!reflect.DeepEqual(got.Method, want.Method) {
t.Errorf("Build() got = %v, want %v", got, want)
}
}
func TestRequestBuilderReturnsRequestWhenRequestOfArgsIsNil(t *testing.T) {
var (
ctx = context.Background()
method = http.MethodGet
url = "/foo"
want, _ = http.NewRequestWithContext(ctx, method, url, nil)
)
b := NewRequestBuilder()
got, _ := b.Build(ctx, method, url, nil, nil)
if !reflect.DeepEqual(got, want) {
t.Errorf("Build() got = %v, want %v", got, want)
}
}