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:
Stevan Freeborn
2026-01-15 16:38:47 -06:00
parent 62426530bd
commit 1f6218416c
7 changed files with 339 additions and 89 deletions
+82 -80
View File
@@ -8,98 +8,100 @@ import (
"github.com/StevanFreeborn/onspring-api-sdk-go"
)
func TestGet(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)
})
func TestPing(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
var nilContext context.Context = nil
err := client.Ping.Get(nilContext)
err := client.Ping.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.Ping.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.Ping.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.Ping.Get(t.Context())
if err == nil {
t.Errorf("Expected request creation error, got nil")
}
})
t.Run("it should perform a GET request to the /ping endpoint and return no error if receives 200 status code", func(t *testing.T) {
_, 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 err == nil {
t.Errorf("Expected error for nil context, got nil")
}
})
if r.URL.Path != "/ping" {
t.Errorf("Expected /ping endpoint, got %s", r.URL.Path)
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.Ping.Get(ctx)
if err == nil {
t.Errorf("Expected error for canceled context, got nil")
}
w.WriteHeader(http.StatusOK)
})
err := client.Ping.Get(t.Context())
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{}}),
)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
err := client.Ping.Get(t.Context())
t.Run("it should return an error if the /ping 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)
if err == nil {
t.Errorf("Expected network error, got nil")
}
})
err := client.Ping.Get(t.Context())
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)
})
if err == nil {
t.Errorf("Expected error, got nil")
}
invalidClient := onspring.NewClient(
"test-api-key",
onspring.WithBaseURL("http://[::1]:namedport"),
onspring.WithHTTPClient(client.HTTPClient()),
)
err := invalidClient.Ping.Get(t.Context())
if err == nil {
t.Errorf("Expected request creation error, got nil")
}
})
t.Run("it should perform a GET request to the /ping endpoint and return no error if receives 200 status code", func(t *testing.T) {
_, 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 != "/ping" {
t.Errorf("Expected /ping endpoint, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
})
err := client.Ping.Get(t.Context())
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
})
t.Run("it should return an error if the /ping 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.Ping.Get(t.Context())
if err == nil {
t.Errorf("Expected error, got nil")
}
})
})
}