Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests for API Package #91

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 102 additions & 11 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"

Expand All @@ -34,6 +35,18 @@ import (

type stubTypeWithMetaData struct{}

type MyReader struct {
reader io.Reader
}

func (r *MyReader) Read(p []byte) (int, error) {
return r.reader.Read(p)
}

func (r *MyReader) Close() error {
return nil
}

func (s stubTypeWithMetaData) MetaData() http.Header {
h := make(http.Header)
h.Set("foo", "bar")
Expand Down Expand Up @@ -124,6 +137,15 @@ func TestNew(t *testing.T) {
debug: false,
expectError: true,
},
{
name: "Host with showHTTP option",
host: "http://example.com",
opts: ClientOptions{
ShowHTTP: true,
},
debug: false,
expectError: false,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -351,12 +373,6 @@ func TestDoAndGetResponseBody(t *testing.T) {
httpClient := &http.Client{
Transport: &MockTransport{mockHTTPClient: mockHTTPClient},
}
c := &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
}

tests := []struct {
name string
Expand All @@ -367,6 +383,7 @@ func TestDoAndGetResponseBody(t *testing.T) {
mockResponse *http.Response
mockError error
expectedError string
c *client
}{
{
name: "Successful GET request",
Expand All @@ -380,6 +397,12 @@ func TestDoAndGetResponseBody(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: nil,
expectedError: "",
},
Expand All @@ -390,8 +413,14 @@ func TestDoAndGetResponseBody(t *testing.T) {
headers: map[string]string{
"Content-Type": "application/json",
},
body: make(chan int), // invalid JSON body
mockResponse: nil,
body: make(chan int), // invalid JSON body
mockResponse: nil,
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: errors.New("unsupported type error"),
expectedError: "json: unsupported type: chan int",
},
Expand All @@ -407,6 +436,12 @@ func TestDoAndGetResponseBody(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: nil,
expectedError: "",
},
Expand All @@ -422,19 +457,75 @@ func TestDoAndGetResponseBody(t *testing.T) {
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: nil,
expectedError: "",
},
{
name: "POST request with JSON body without Content-Type header set",
method: http.MethodPost,
uri: "/test",
headers: map[string]string{},
body: map[string]string{"key": "value"},
headers: map[string]string{"Custom-Header": "application/json"},
body: &MyReader{
reader: strings.NewReader("Success"),
},
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: nil,
expectedError: "",
},
{
name: "Get request with path not starting with /",
method: http.MethodGet,
uri: "test",
headers: map[string]string{
"content-type": "application/json",
},
body: nil,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: false,
},
mockError: nil,
expectedError: "",
},
{
name: "Successful GET request having client with showHTTP set to true",
method: http.MethodGet,
uri: "test",
headers: map[string]string{
"content-type": "application/json",
},
body: nil,
mockResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`{"success": true}`)),
},
c: &client{
http: httpClient,
host: "https://example.com",
token: "mockToken",
showHTTP: true,
},
mockError: nil,
expectedError: "",
},
Expand All @@ -444,7 +535,7 @@ func TestDoAndGetResponseBody(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
mockHTTPClient.On("Do", mock.Anything).Return(tt.mockResponse, tt.mockError)

res, err := c.DoAndGetResponseBody(
res, err := tt.c.DoAndGetResponseBody(
context.Background(),
tt.method,
tt.uri,
Expand Down