terraform-provider-mozoutmz/internal/client/client_test.go
2026-11-28 15:28:10 +02:00

100 lines
3.4 KiB
Go

package client
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
func testClient(url string) *Client {
c := New("sandbox_test", "test")
c.BaseURL = url
return c
}
func TestRetriesOn429AndKeepsIdempotencyKey(t *testing.T) {
var calls int32
var keys []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
keys = append(keys, r.Header.Get("Idempotency-Key"))
if atomic.AddInt32(&calls, 1) == 1 {
w.Header().Set("Retry-After", "0")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"error":{"code":"rate_limited","message":"slow down","correlation_id":"c1","details":{}}}`))
return
}
w.Header().Set("X-API-Simulated", "true")
w.WriteHeader(http.StatusAccepted)
_, _ = w.Write([]byte(`{"data":{"action_id":"a1","status":"simulated","simulated":true},"meta":{}}`))
}))
defer srv.Close()
p, err := testClient(srv.URL).Purchase(context.Background(), map[string]interface{}{"product_id": 1, "billing_cycle": "monthly"}, "k1")
if err != nil {
t.Fatal(err)
}
if !p.Simulated || p.ActionID != "a1" {
t.Fatalf("unexpected purchase %+v", p)
}
if len(keys) != 2 || keys[0] != "k1" || keys[1] != "k1" {
t.Fatalf("the retry must reuse the same Idempotency-Key, got %v", keys)
}
}
func TestErrorsAreClear(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusPaymentRequired)
_, _ = w.Write([]byte(`{"error":{"code":"insufficient_credit","message":"x","correlation_id":"c2","details":{"add_funds_url":"https://example.com/add"}}}`))
}))
defer srv.Close()
_, err := testClient(srv.URL).Register(context.Background(), map[string]interface{}{"domain": "example.com"}, "k2")
if err == nil || !HasCode(err, "insufficient_credit") {
t.Fatalf("expected insufficient_credit, got %v", err)
}
if want := "https://example.com/add"; !contains(err.Error(), want) {
t.Fatalf("message should point to %s: %s", want, err)
}
}
func TestPostWithoutKeyIsNotRetried(t *testing.T) {
var calls int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte(`{"error":{"code":"upstream_error","message":"x","correlation_id":"c3","details":{}}}`))
}))
defer srv.Close()
_, err := testClient(srv.URL).CreateDNSRecord(context.Background(), "example.com", DNSRecordInput{Name: "www", Type: "A", Value: "203.0.113.10", TTL: 3600})
if err == nil || calls != 1 {
t.Fatalf("a POST without Idempotency-Key must not be retried (calls %d, err %v)", calls, err)
}
}
func TestListAllWalksPages(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("page") == "1" {
_, _ = w.Write([]byte(`{"data":[{"id":1,"name":"a"}],"meta":{"page":1,"per_page":100,"total":2,"total_pages":2}}`))
return
}
_, _ = w.Write([]byte(`{"data":[{"id":2,"name":"b"}],"meta":{"page":2,"per_page":100,"total":2,"total_pages":2}}`))
}))
defer srv.Close()
list, err := testClient(srv.URL).Servers(context.Background(), "")
if err != nil || len(list) != 2 || list[1].ID != 2 {
t.Fatalf("expected 2 servers, got %v %v", list, err)
}
}
func contains(s, sub string) bool {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}