* move request_builder into internal pkg (#304) * add some test for internal.RequestBuilder * add a test for openai.GetEngine
This commit is contained in:
committed by
GitHub
parent
62eb4beed2
commit
61ba5f3369
61
internal/request_builder_test.go
Normal file
61
internal/request_builder_test.go
Normal file
@@ -0,0 +1,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{}{})
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user