Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to configure client target #3349

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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",
Expand Down
Loading