package aw import ( "context" "encoding/json" "io" "net/http" "net/http/httptest" "testing" ) func TestEnsureBucketPostsCreate(t *testing.T) { var gotPath string var gotBody map[string]string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path if r.Method != http.MethodPost { t.Errorf("method = %q, want POST", r.Method) } b, _ := io.ReadAll(r.Body) _ = json.Unmarshal(b, &gotBody) w.WriteHeader(http.StatusOK) })) defer srv.Close() c := New(srv.URL) if err := c.EnsureBucket(context.Background(), "keel.events", "keel.event", "keel"); err != nil { t.Fatalf("EnsureBucket: %v", err) } if gotPath != "/api/0/buckets/keel.events" { t.Fatalf("path = %q", gotPath) } if gotBody["type"] != "keel.event" || gotBody["client"] != "keel" { t.Fatalf("body = %+v", gotBody) } } func TestEnsureBucketTreatsExistsAsOK(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotModified) // AW returns 304 when the bucket exists })) defer srv.Close() if err := New(srv.URL).EnsureBucket(context.Background(), "keel.events", "keel.event", "keel"); err != nil { t.Fatalf("existing bucket should be OK, got %v", err) } } func TestEnsureBucketErrorsOnServerError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) defer srv.Close() if err := New(srv.URL).EnsureBucket(context.Background(), "keel.events", "keel.event", "keel"); err == nil { t.Fatal("expected error on 500") } }