From ca54b8b27b36d9d1df400693f9181b7155d686a9 Mon Sep 17 00:00:00 2001 From: "Taras S." Date: Thu, 31 Oct 2024 15:28:01 +0200 Subject: [PATCH] chore: add test for client with custom target --- client/client_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client/client_test.go b/client/client_test.go index 115ca3e0020..ed0a78ac3de 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -217,3 +217,33 @@ func TestSetCustomDecodeConfig(t *testing.T) { c.MustPost("user(id: 1) {created_at}", &resp) require.WithinDuration(t, now, resp.CreatedAt, time.Second) } + +func TestClientWithCustomTarget(t *testing.T) { + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b, err := io.ReadAll(r.Body) + if assert.NoError(t, err) { + assert.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b)) + + err = json.NewEncoder(w).Encode(map[string]any{ + "data": map[string]any{ + "name": "bob", + }, + }) + assert.NoError(t, err) + } + }) + + mux := http.NewServeMux() + mux.HandleFunc("/test", h) + + c := client.New(mux) + c.SetCustomTarget("/test") + + var resp struct { + Name string + } + + c.MustPost("user(id:$id){name}", &resp, client.Var("id", 1)) + + require.Equal(t, "bob", resp.Name) +}