Follows the same pattern as Put/Post, accepting context, URL, and body. Closes an obvious gap in the REST client API. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package httpx_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.codelab.vc/pkg/httpx"
|
|
)
|
|
|
|
func TestClient_Patch(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPatch {
|
|
t.Errorf("expected PATCH, got %s", r.Method)
|
|
}
|
|
b, _ := io.ReadAll(r.Body)
|
|
if string(b) != `{"name":"updated"}` {
|
|
t.Errorf("expected body %q, got %q", `{"name":"updated"}`, string(b))
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprint(w, "patched")
|
|
}))
|
|
defer srv.Close()
|
|
|
|
client := httpx.New()
|
|
resp, err := client.Patch(context.Background(), srv.URL+"/item/1", strings.NewReader(`{"name":"updated"}`))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
body, err := resp.String()
|
|
if err != nil {
|
|
t.Fatalf("reading body: %v", err)
|
|
}
|
|
if body != "patched" {
|
|
t.Errorf("expected body %q, got %q", "patched", body)
|
|
}
|
|
}
|