package tdclient import ( "context" "net/http" "net/http/httptest" "testing" ) func TestWithTokenSetsAuthorizationHeader(t *testing.T) { var gotAuth string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotAuth = r.Header.Get("Authorization") w.WriteHeader(http.StatusNoContent) })) defer srv.Close() c := New(srv.URL).WithToken("tdsa_abc123") if err := c.DeleteTeam(context.Background(), 1); err != nil { t.Fatalf("DeleteTeam() error = %v", err) } if want := "Bearer tdsa_abc123"; gotAuth != want { t.Errorf("Authorization header = %q, want %q", gotAuth, want) } } // Every delete is idempotent: the thing already being gone is success, any // other failure is not. func TestDeletesTreatNotFoundAsSuccess(t *testing.T) { status := http.StatusNotFound srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status) })) defer srv.Close() c := New(srv.URL) ctx := context.Background() for name, call := range map[string]func() error{ "team": func() error { return c.DeleteTeam(ctx, 1) }, "switch": func() error { return c.DeleteDeadmanSwitch(ctx, 1, 2) }, "integration": func() error { return c.DeleteIntegration(ctx, 1, 2) }, } { status = http.StatusNotFound if err := call(); err != nil { t.Errorf("%s: 404 should be success, got %v", name, err) } status = http.StatusConflict if err := call(); err == nil { t.Errorf("%s: 409 should be an error", name) } } }