Skip to content

Commit

Permalink
add some test for internal.RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed May 30, 2023
1 parent f750d00 commit 7eb94ba
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions internal/request_builder_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package openai //nolint:testpackage // testing private field

import (
"bytes"
"context"
"errors"
"net/http"
"reflect"
"testing"
)

Expand All @@ -24,3 +27,35 @@ func TestRequestBuilderReturnsMarshallerErrors(t *testing.T) {
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)
}
}

0 comments on commit 7eb94ba

Please sign in to comment.