File size: 7,709 Bytes
04f1444 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | // Hand-written wire tests for the generated OpenMeter Go SDK. The generator's
// output cleaner preserves *_test.go files, so these survive regeneration.
package openmeter_test
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"testing"
openmeter "github.com/openmeterio/openmeter/api/v3/client"
)
func TestAPIErrorParsesRFC7807(t *testing.T) {
t.Parallel()
body := `{"status":404,"type":"https://openmeter.io/problems/not-found","title":"Not Found","detail":"meter not found","instance":"kong:trace:abc123"}`
rec := &requestRecorder{}
om := newTestClient(t, rec.handler(http.StatusNotFound, body))
_, err := om.Meters.Get(t.Context(), "m-1")
apiErr, ok := openmeter.AsAPIError(err)
if !ok {
t.Fatalf("error %v is not an *APIError", err)
}
if apiErr.StatusCode != http.StatusNotFound {
t.Errorf("StatusCode = %d, want 404", apiErr.StatusCode)
}
if apiErr.Status != 404 {
t.Errorf("Status = %d, want 404", apiErr.Status)
}
if apiErr.Type != "https://openmeter.io/problems/not-found" {
t.Errorf("Type = %q, want the problem type", apiErr.Type)
}
if apiErr.Title != "Not Found" {
t.Errorf("Title = %q, want %q", apiErr.Title, "Not Found")
}
if apiErr.Detail != "meter not found" {
t.Errorf("Detail = %q, want %q", apiErr.Detail, "meter not found")
}
if apiErr.Instance != "kong:trace:abc123" {
t.Errorf("Instance = %q, want %q", apiErr.Instance, "kong:trace:abc123")
}
if string(apiErr.RawBody) != body {
t.Errorf("RawBody = %q, want the undecoded body", apiErr.RawBody)
}
if want := "openmeter: 404 Not Found: meter not found"; apiErr.Error() != want {
t.Errorf("Error() = %q, want %q", apiErr.Error(), want)
}
}
func TestAPIErrorTitleOnly(t *testing.T) {
t.Parallel()
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusTeapot, `{"status":418,"title":"Teapot"}`))
_, err := om.Meters.Get(t.Context(), "m-1")
apiErr, ok := openmeter.AsAPIError(err)
if !ok {
t.Fatalf("error %v is not an *APIError", err)
}
if want := "openmeter: 418 Teapot"; apiErr.Error() != want {
t.Errorf("Error() = %q, want %q", apiErr.Error(), want)
}
}
func TestAPIErrorNonJSONBodyFallsBackToRawEcho(t *testing.T) {
t.Parallel()
t.Run("short body echoed in full", func(t *testing.T) {
body := "<html>bad gateway</html>"
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusBadGateway, body))
_, err := om.Meters.Get(t.Context(), "m-1")
apiErr, ok := openmeter.AsAPIError(err)
if !ok {
t.Fatalf("error %v is not an *APIError", err)
}
if apiErr.Title != "" {
t.Errorf("Title = %q, want empty for a non-problem body", apiErr.Title)
}
if want := "openmeter: unexpected status 502: " + body; apiErr.Error() != want {
t.Errorf("Error() = %q, want %q", apiErr.Error(), want)
}
})
t.Run("long body truncated at 512 bytes", func(t *testing.T) {
body := strings.Repeat("x", 600)
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusBadGateway, body))
_, err := om.Meters.Get(t.Context(), "m-1")
apiErr, ok := openmeter.AsAPIError(err)
if !ok {
t.Fatalf("error %v is not an *APIError", err)
}
want := "openmeter: unexpected status 502: " + strings.Repeat("x", 512) + "… (truncated)"
if apiErr.Error() != want {
t.Errorf("Error() = %q, want %q", apiErr.Error(), want)
}
// The message is truncated; RawBody still carries the full payload.
if len(apiErr.RawBody) != 600 {
t.Errorf("len(RawBody) = %d, want the full 600 bytes", len(apiErr.RawBody))
}
})
}
func TestAsAPIError(t *testing.T) {
t.Parallel()
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusInternalServerError, `{"status":500,"title":"Boom"}`))
_, err := om.Meters.Get(t.Context(), "m-1")
if err == nil {
t.Fatal("Meters.Get returned nil error for a 500 response")
}
wrapped := fmt.Errorf("listing usage: %w", err)
apiErr, ok := openmeter.AsAPIError(wrapped)
if !ok {
t.Fatalf("AsAPIError did not find the APIError inside %v", wrapped)
}
if apiErr.StatusCode != http.StatusInternalServerError {
t.Errorf("StatusCode = %d, want 500", apiErr.StatusCode)
}
if got, ok := openmeter.AsAPIError(errors.New("plain")); ok || got != nil {
t.Errorf("AsAPIError(plain error) = (%v, %t), want (nil, false)", got, ok)
}
}
func TestDecodeAPIError(t *testing.T) {
t.Parallel()
type validationProblem struct {
Status int `json:"status"`
Title string `json:"title"`
Errors []struct {
Field string `json:"field"`
} `json:"errors"`
}
t.Run("decodes a typed error body", func(t *testing.T) {
body := `{"status":400,"title":"Bad Request","errors":[{"field":"key"},{"field":"name"}]}`
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusBadRequest, body))
_, err := om.Meters.Get(t.Context(), "m-1")
problem, ok, decodeErr := openmeter.DecodeAPIError[validationProblem](err)
if decodeErr != nil {
t.Fatalf("DecodeAPIError: %v", decodeErr)
}
if !ok {
t.Fatal("DecodeAPIError reported the error is not an APIError")
}
if problem.Status != 400 || problem.Title != "Bad Request" || len(problem.Errors) != 2 || problem.Errors[1].Field != "name" {
t.Errorf("decoded problem = %+v, want the typed body", problem)
}
})
t.Run("non-API errors are reported as not decodable", func(t *testing.T) {
problem, ok, decodeErr := openmeter.DecodeAPIError[validationProblem](errors.New("dial tcp: refused"))
if ok || decodeErr != nil {
t.Errorf("DecodeAPIError(plain error) = (%+v, %t, %v), want ok=false with nil error", problem, ok, decodeErr)
}
})
t.Run("undecodable body surfaces the decode error", func(t *testing.T) {
om := newTestClient(t, (&requestRecorder{}).handler(http.StatusInternalServerError, "not json"))
_, err := om.Meters.Get(t.Context(), "m-1")
_, ok, decodeErr := openmeter.DecodeAPIError[validationProblem](err)
if !ok {
t.Fatal("DecodeAPIError reported the error is not an APIError")
}
if decodeErr == nil {
t.Error("DecodeAPIError returned nil error for a non-JSON body")
}
})
}
func TestEmptyIDGuard(t *testing.T) {
t.Parallel()
// Empty IDs must be rejected client-side: no request may reach the server.
om := newTestClient(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Errorf("unexpected HTTP request %s %s for an empty-ID call", r.Method, r.URL)
}))
cases := []struct {
name string
wantParam string
call func(ctx context.Context) error
}{
{
name: "meters get",
wantParam: "meterID",
call: func(ctx context.Context) error {
_, err := om.Meters.Get(ctx, "")
return err
},
},
{
name: "meters update",
wantParam: "meterID",
call: func(ctx context.Context) error {
_, err := om.Meters.Update(ctx, "", openmeter.UpdateMeterRequest{})
return err
},
},
{
name: "meters delete",
wantParam: "meterID",
call: func(ctx context.Context) error { return om.Meters.Delete(ctx, "") },
},
{
name: "customers get",
wantParam: "customerID",
call: func(ctx context.Context) error {
_, err := om.Customers.Get(ctx, "")
return err
},
},
{
name: "invoices get",
wantParam: "invoiceID",
call: func(ctx context.Context) error {
_, err := om.Invoices.Get(ctx, "")
return err
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := tc.call(t.Context())
if err == nil {
t.Fatal("call with empty ID returned nil error")
}
if !errors.Is(err, openmeter.ErrEmptyID) {
t.Errorf("errors.Is(err, ErrEmptyID) = false for %v", err)
}
if !strings.Contains(err.Error(), tc.wantParam) {
t.Errorf("error %q does not name the parameter %q", err, tc.wantParam)
}
})
}
}
|