From fd6112e9db5fe9c1a1638685c5bb896aaa9b0b76 Mon Sep 17 00:00:00 2001 From: "Taras S." Date: Thu, 31 Oct 2024 13:25:14 +0200 Subject: [PATCH] feat: add possibility to configure client target --- client/client.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/client/client.go b/client/client.go index 14f540e0120..ea3b4a3d201 100644 --- a/client/client.go +++ b/client/client.go @@ -17,9 +17,10 @@ import ( type ( // Client used for testing GraphQL servers. Not for production use. Client struct { - h http.Handler - dc *mapstructure.DecoderConfig - opts []Option + h http.Handler + dc *mapstructure.DecoderConfig + opts []Option + target string } // Option implements a visitor that mutates an outgoing GraphQL request @@ -48,8 +49,9 @@ type ( // Options can be set that should be applied to all requests made with this client func New(h http.Handler, opts ...Option) *Client { p := &Client{ - h: h, - opts: opts, + h: h, + opts: opts, + target: "/", } return p @@ -111,7 +113,7 @@ var boundaryRegex = regexp.MustCompile(`multipart/form-data; ?boundary=.*`) func (p *Client) newRequest(query string, options ...Option) (*http.Request, error) { bd := &Request{ Query: query, - HTTP: httptest.NewRequest(http.MethodPost, "/", http.NoBody), + HTTP: httptest.NewRequest(http.MethodPost, p.target, http.NoBody), } bd.HTTP.Header.Set("Content-Type", "application/json") @@ -146,6 +148,11 @@ func (p *Client) SetCustomDecodeConfig(dc *mapstructure.DecoderConfig) { p.dc = dc } +// SetCustomTarget sets a custom target path for the client +func (p *Client) SetCustomTarget(target string) { + p.target = target +} + func unpack(data, into any, customDc *mapstructure.DecoderConfig) error { dc := &mapstructure.DecoderConfig{ TagName: "json",