Skip to content

Commit

Permalink
[Go][Client] Use configured Scheme and Host in requests (OpenAPITools…
Browse files Browse the repository at this point in the history
…#3115)

* [Go][Client] Ensure test errors don't crash

Previously we did not interrupt execution of a test case
when an error was returned from an API call. This was
causing the tests to crash as soon as we try to
dereference the response.

We now fail the tests as soon as the first API error
is received.

* [Go][Client] Use configured Scheme and Host in requests

Previously we were simply setting the Host header and
completely ignoring the configured scheme.
  • Loading branch information
johanbrandhorst authored and wing328 committed Jun 25, 2019
1 parent 74c569e commit 2b55413
Show file tree
Hide file tree
Showing 78 changed files with 834 additions and 394 deletions.
15 changes: 10 additions & 5 deletions modules/openapi-generator/src/main/resources/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ func (c *APIClient) prepareRequest(
return nil, err
}

// Override request host, if applicable
if c.cfg.Host != "" {
url.Host = c.cfg.Host
}

// Override request scheme, if applicable
if c.cfg.Scheme != "" {
url.Scheme = c.cfg.Scheme
}

// Adding Query Param
query := url.Query()
for k, v := range queryParams {
Expand Down Expand Up @@ -274,11 +284,6 @@ func (c *APIClient) prepareRequest(
localVarRequest.Header = headers
}

// Override request host, if applicable
if c.cfg.Host != "" {
localVarRequest.Host = c.cfg.Host
}

// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)

Expand Down
59 changes: 28 additions & 31 deletions samples/client/petstore/go/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ func TestOAuth2(t *testing.T) {
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -53,8 +52,7 @@ func TestOAuth2(t *testing.T) {
r, err = client.PetApi.DeletePet(auth, 12992, nil)

if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -79,8 +77,7 @@ func TestBasicAuth(t *testing.T) {
r, err := client.PetApi.AddPet(auth, newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -89,8 +86,7 @@ func TestBasicAuth(t *testing.T) {
r, err = client.PetApi.DeletePet(auth, 12992, nil)

if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -110,8 +106,7 @@ func TestAccessToken(t *testing.T) {
r, err := client.PetApi.AddPet(nil, newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -120,8 +115,7 @@ func TestAccessToken(t *testing.T) {
r, err = client.PetApi.DeletePet(auth, 12992, nil)

if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -141,17 +135,15 @@ func TestAPIKeyNoPrefix(t *testing.T) {
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
}

_, r, err = client.PetApi.GetPetById(auth, 12992)
if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}

reqb, _ := httputil.DumpRequest(r.Request, true)
Expand All @@ -161,8 +153,7 @@ func TestAPIKeyNoPrefix(t *testing.T) {

r, err = client.PetApi.DeletePet(auth, 12992, nil)
if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -178,17 +169,15 @@ func TestAPIKeyWithPrefix(t *testing.T) {
r, err := client.PetApi.AddPet(nil, newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
}

_, r, err = client.PetApi.GetPetById(auth, 12992)
if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}

reqb, _ := httputil.DumpRequest(r.Request, true)
Expand All @@ -198,8 +187,7 @@ func TestAPIKeyWithPrefix(t *testing.T) {

r, err = client.PetApi.DeletePet(auth, 12992, nil)
if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -214,8 +202,7 @@ func TestDefaultHeader(t *testing.T) {
r, err := client.PetApi.AddPet(context.Background(), newPet)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -224,8 +211,7 @@ func TestDefaultHeader(t *testing.T) {
r, err = client.PetApi.DeletePet(context.Background(), 12992, nil)

if err != nil {
t.Errorf("Error while deleting pet by id")
t.Log(err)
t.Fatalf("Error while deleting pet by id: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand All @@ -240,15 +226,26 @@ func TestHostOverride(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(context.Background(), nil)

if err != nil {
t.Errorf("Error while finding pets by status")
t.Log(err)
t.Fatalf("Error while finding pets by status: %v", err)
}

if r.Request.Host != testHost {
if r.Request.URL.Host != testHost {
t.Errorf("Request Host is %v, expected %v", r.Request.Host, testHost)
}
}

func TestSchemeOverride(t *testing.T) {
_, r, err := client.PetApi.FindPetsByStatus(context.Background(), nil)

if err != nil {
t.Fatalf("Error while finding pets by status: %v", err)
}

if r.Request.URL.Scheme != testScheme {
t.Errorf("Request Scheme is %v, expected %v", r.Request.URL.Scheme, testScheme)
}
}

// Add custom clients to the context.
func createContext(httpClient *http.Client) context.Context {
parent := oauth2.NoContext
Expand Down
3 changes: 1 addition & 2 deletions samples/client/petstore/go/fake_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestPutBodyWithFileSchema(t *testing.T) {
r, err := client.FakeApi.TestBodyWithFileSchema(context.Background(), schema)

if err != nil {
t.Errorf("Error while adding pet")
t.Log(err)
t.Fatalf("Error while adding pet: %v", err)
}
if r.StatusCode != 200 {
t.Log(r)
Expand Down
15 changes: 10 additions & 5 deletions samples/client/petstore/go/go-petstore-withXml/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions samples/client/petstore/go/go-petstore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ func (c *APIClient) prepareRequest(
return nil, err
}

// Override request host, if applicable
if c.cfg.Host != "" {
url.Host = c.cfg.Host
}

// Override request scheme, if applicable
if c.cfg.Scheme != "" {
url.Scheme = c.cfg.Scheme
}

// Adding Query Param
query := url.Query()
for k, v := range queryParams {
Expand Down Expand Up @@ -285,11 +295,6 @@ func (c *APIClient) prepareRequest(
localVarRequest.Header = headers
}

// Override request host, if applicable
if c.cfg.Host != "" {
localVarRequest.Host = c.cfg.Host
}

// Add the user agent to the request.
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)

Expand Down
Loading

0 comments on commit 2b55413

Please sign in to comment.