Files
orbit/sdk/go/subscriptions_test.go

70 lines
2.5 KiB
Go

package orbit
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestSubscriptionHTTPWireAndPathValidation(t *testing.T) {
id := strings.Repeat("1", 32)
zero := uint64(0)
calls := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
if r.Header.Get("Authorization") != "Bearer test-token" || r.Header.Get("Idempotency-Key") != "stable" {
t.Error("missing protected request headers")
}
var raw map[string]json.RawMessage
if err := json.NewDecoder(r.Body).Decode(&raw); err != nil {
t.Error(err)
}
switch r.URL.Path {
case "/v1/subscriptions":
if string(raw["from_sequence"]) != `"0"` {
t.Error("explicit zero cursor omitted or encoded as number")
}
case "/v1/subscriptions/" + id + ":configure":
if string(raw["enabled"]) != "false" || string(raw["expected_revision"]) != `"9007199254740993"` {
t.Error("control intent or integer precision lost")
}
default:
t.Error("wrong subscription route")
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"request_id":"` + id + `","data":{"subscription":{"id":"` + id + `","revision":"9007199254740993"},"deduplicated":false}}`))
}))
defer server.Close()
c, err := NewClient(server.URL, Options{Token: "test-token"})
if err != nil {
t.Fatal(err)
}
out, err := c.CreateSubscription(context.Background(), "stable", CreateSubscriptionRequest{Name: "results", QueueID: id, FromSequence: &zero})
if err != nil || out.Subscription.Revision != 9007199254740993 {
t.Fatal("subscription counter precision", err)
}
if _, err = c.ConfigureSubscription(context.Background(), id, "stable", ConfigureSubscriptionRequest{ExpectedRevision: 9007199254740993, Enabled: false, Reason: "pause"}); err != nil {
t.Fatal(err)
}
for _, bad := range []string{"", id + "/deliveries", "../events"} {
if _, err := c.GetSubscription(context.Background(), bad); err == nil {
t.Fatal("accepted invalid subscription path")
}
if _, err := c.GetEventDelivery(context.Background(), id, bad); err == nil {
t.Fatal("accepted invalid delivery path")
}
}
if _, err = c.ListSubscriptions(context.Background(), id, SubscriptionPageRequest{Limit: 101}); err == nil {
t.Fatal("accepted unbounded page")
}
if _, err = c.ListEventDeliveries(context.Background(), id, DeliveryPageRequest{State: "INVALID"}); err == nil {
t.Fatal("accepted invalid delivery state")
}
if calls != 2 {
t.Fatal("invalid request reached network")
}
}