Skip to content

Commit

Permalink
chore: add test for client with custom target (#3350)
Browse files Browse the repository at this point in the history
  • Loading branch information
taraktikos authored Oct 31, 2024
1 parent 0b86cf7 commit f02c5b4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f02c5b4

Please sign in to comment.