From de160d806cf99924acb7d7a92a8993af4fba4350 Mon Sep 17 00:00:00 2001 From: Rocktavious Date: Wed, 20 Jul 2022 11:21:47 -0500 Subject: [PATCH] Fix problem with REST client having incorrect base url --- .changes/unreleased/Bugfix-20220720-112131.yaml | 3 +++ client.go | 9 +-------- clientGQL.go | 9 ++++++++- clientGQL_test.go | 12 ++++++------ clientRest_test.go | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 .changes/unreleased/Bugfix-20220720-112131.yaml diff --git a/.changes/unreleased/Bugfix-20220720-112131.yaml b/.changes/unreleased/Bugfix-20220720-112131.yaml new file mode 100644 index 00000000..c2d71cc5 --- /dev/null +++ b/.changes/unreleased/Bugfix-20220720-112131.yaml @@ -0,0 +1,3 @@ +kind: Bugfix +body: Fix problem with REST client base url including `graphql` +time: 2022-07-20T11:21:31.823974-05:00 diff --git a/client.go b/client.go index 8afeb697..f337d989 100644 --- a/client.go +++ b/client.go @@ -43,14 +43,7 @@ func SetAPIToken(apiToken string) Option { func SetURL(url string) Option { return func(c *ClientSettings) { - c.url = fmt.Sprintf("%s/graphql", strings.TrimRight(url, "/")) - } -} - -// SetTestUrl - Only use this when making test Clients -func SetTestURL(url string) Option { - return func(c *ClientSettings) { - c.url = url + c.url = fmt.Sprintf("%s", strings.TrimRight(url, "/")) } } diff --git a/clientGQL.go b/clientGQL.go index 3a7b226b..fec034d1 100644 --- a/clientGQL.go +++ b/clientGQL.go @@ -56,10 +56,17 @@ func NewGQLClient(options ...Option) *Client { underlyingTransport: standardClient.Transport, }, } + var url string + if strings.Contains(settings.url, "/LOCAL_TESTING/") { + url = settings.url + } else { + url = fmt.Sprintf("%s/graphql", settings.url) + } + fmt.Println(url) return &Client{ pageSize: graphql.Int(settings.pageSize), - client: graphql.NewClient(settings.url, standardClient), + client: graphql.NewClient(url, standardClient), } } diff --git a/clientGQL_test.go b/clientGQL_test.go index 3acdd235..c7a178a6 100644 --- a/clientGQL_test.go +++ b/clientGQL_test.go @@ -71,33 +71,33 @@ func FixtureQueryValidation(t *testing.T, fixture string) autopilot.RequestValid } func ATestClient(t *testing.T, endpoint string) *ol.Client { - return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetTestURL(autopilot.RegisterEndpoint(fmt.Sprintf("/%s", endpoint), + return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetURL(autopilot.RegisterEndpoint(fmt.Sprintf("/LOCAL_TESTING/%s", endpoint), autopilot.FixtureResponse(fmt.Sprintf("%s_response.json", endpoint)), FixtureQueryValidation(t, fmt.Sprintf("%s_request.json", endpoint))))) } func ATestClientAlt(t *testing.T, response string, request string) *ol.Client { - return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetTestURL(autopilot.RegisterEndpoint(fmt.Sprintf("/%s__%s", response, request), + return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetURL(autopilot.RegisterEndpoint(fmt.Sprintf("/LOCAL_TESTING/%s__%s", response, request), autopilot.FixtureResponse(fmt.Sprintf("%s_response.json", response)), FixtureQueryValidation(t, fmt.Sprintf("%s_request.json", request))))) } func ATestClientSkipRequest(t *testing.T, endpoint string) *ol.Client { - return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetTestURL(autopilot.RegisterEndpoint(fmt.Sprintf("/%s", endpoint), + return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetURL(autopilot.RegisterEndpoint(fmt.Sprintf("/LOCAL_TESTING/%s", endpoint), autopilot.FixtureResponse(fmt.Sprintf("%s_response.json", endpoint)), autopilot.SkipRequestValidation()))) } func ATestClientLogRequest(t *testing.T, endpoint string) *ol.Client { - return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetTestURL(autopilot.RegisterEndpoint(fmt.Sprintf("/%s", endpoint), + return ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetURL(autopilot.RegisterEndpoint(fmt.Sprintf("/LOCAL_TESTING/%s", endpoint), autopilot.FixtureResponse(fmt.Sprintf("%s_response.json", endpoint)), LogRaw()))) } func TestClientQuery(t *testing.T) { // Arrange - url := autopilot.RegisterEndpoint("/account", autopilot.FixtureResponse("account_response.json"), QueryValidation(t, "{account{id}}")) - client := ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetTestURL(url)) + url := autopilot.RegisterEndpoint("/LOCAL_TESTING/account", autopilot.FixtureResponse("account_response.json"), QueryValidation(t, "{account{id}}")) + client := ol.NewGQLClient(ol.SetAPIToken("x"), ol.SetURL(url)) var q struct { Account struct { Id graphql.ID diff --git a/clientRest_test.go b/clientRest_test.go index 66198a5a..37f79657 100644 --- a/clientRest_test.go +++ b/clientRest_test.go @@ -9,7 +9,7 @@ import ( ) func ATestRestClient(t *testing.T, endpoint string) *resty.Client { - return ol.NewRestClient(ol.SetTestURL( + return ol.NewRestClient(ol.SetURL( autopilot.RegisterEndpoint( fmt.Sprintf("/%s", endpoint), autopilot.FixtureResponse(fmt.Sprintf("%s_response.json", endpoint)),