Skip to content

Commit

Permalink
Fix problem with REST client having incorrect base url
Browse files Browse the repository at this point in the history
  • Loading branch information
rocktavious committed Jul 20, 2022
1 parent f565aa7 commit de160d8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Bugfix-20220720-112131.yaml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 1 addition & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/"))
}
}

Expand Down
9 changes: 8 additions & 1 deletion clientGQL.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ func NewGQLClient(options ...Option) *Client {
underlyingTransport: standardClient.Transport,
},
}
var url string
if strings.Contains(settings.url, "/LOCAL_TESTING/") {

This comment has been minimized.

Copy link
@rocktavious

rocktavious Jul 20, 2022

Author Collaborator

I don't love this but i'm not sure of any other way AND this works for now - there is no way we are going to have a URL in our app that contains this in the path for graphql

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),
}
}

Expand Down
12 changes: 6 additions & 6 deletions clientGQL_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clientRest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit de160d8

Please sign in to comment.