feat: add Apps endpoint and query parameter support
- add `Apps` endpoint to the `Client` struct and initialize it in `NewClient` - implement `doWithJsonResponse` helper to handle request execution and JSON decoding in one step - update `newRequest` to support passing and encoding query parameters via `net/url` - rename `Option` to `ClientOption` for better clarity in the `NewClient` signature - refactor `Ping` tests to use a more structured nested layout - remove `option.go` and `option_test.go` as part of the configuration refactor
This commit is contained in:
+131
@@ -0,0 +1,131 @@
|
||||
package onspring_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/onspring-api-sdk-go"
|
||||
)
|
||||
|
||||
func TestApps(t *testing.T) {
|
||||
t.Run("Get", func(t *testing.T) {
|
||||
t.Run("it should return an error if context is nil", func(t *testing.T) {
|
||||
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
var nilContext context.Context = nil
|
||||
|
||||
_, err := client.Apps.Get(nilContext)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected error for nil context, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should return an error if context is canceled", func(t *testing.T) {
|
||||
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
|
||||
cancel()
|
||||
|
||||
_, err := client.Apps.Get(ctx)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected error for canceled context, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should return an error if encounters a network error", func(t *testing.T) {
|
||||
client := onspring.NewClient(
|
||||
"test-api-key",
|
||||
onspring.WithBaseURL("http://invalid-url"),
|
||||
onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}),
|
||||
)
|
||||
|
||||
_, err := client.Apps.Get(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected network error, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should return an error if create a request fails", func(t *testing.T) {
|
||||
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
invalidClient := onspring.NewClient(
|
||||
"test-api-key",
|
||||
onspring.WithBaseURL("http://[::1]:namedport"),
|
||||
onspring.WithHTTPClient(client.HTTPClient()),
|
||||
)
|
||||
|
||||
_, err := invalidClient.Apps.Get(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected request creation error, got nil")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should perform a GET request to the /apps endpoint and return page of apps if receives 200 status code", func(t *testing.T) {
|
||||
expectedPage := onspring.Page[onspring.App]{
|
||||
TotalPages: 1,
|
||||
TotalRecords: 1,
|
||||
PageNumber: 1,
|
||||
PageSize: 1,
|
||||
Items: []onspring.App{
|
||||
{
|
||||
Href: "https://test.com",
|
||||
Id: 1,
|
||||
Name: "App",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("Expected GET method, got %s", r.Method)
|
||||
}
|
||||
|
||||
if r.URL.Path != "/apps" {
|
||||
t.Errorf("Expected /apps endpoint, got %s", r.URL.Path)
|
||||
}
|
||||
|
||||
jsonData, _ := json.Marshal(expectedPage)
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(jsonData)
|
||||
})
|
||||
|
||||
page, err := client.Apps.Get(t.Context())
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expectedPage, page) {
|
||||
t.Errorf("Expected %v but got %v", expectedPage, page)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should return an error if the /apps endpoint returns a non-200 status code", func(t *testing.T) {
|
||||
_, client := setupMockServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
})
|
||||
|
||||
_, err := client.Apps.Get(t.Context())
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected error, got nil")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user