* move request_builder into internal pkg (#304) * add some test for internal.RequestBuilder * add a test for openai.GetEngine
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
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{}{})
|
|
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)
|
|
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)
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("Build() got = %v, want %v", got, want)
|
|
}
|
|
}
|