Skip to content

Commit

Permalink
Merge pull request #116 from stmcginnis/client-tests
Browse files Browse the repository at this point in the history
Expand client test coverage
  • Loading branch information
stmcginnis authored Jan 25, 2021
2 parents bc31b7b + a88694d commit 32343b1
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const (
}
]
}`
expectErrorStatus = `{"error": ` + errMsg + "}"
expectErrorStatus = `{"error": ` + errMsg + "}"
nonErrorStructErrorStatus = "Internal Server Error"
)

// TestError400 tests the parsing of error reply.
Expand Down Expand Up @@ -101,6 +102,30 @@ func TestError404(t *testing.T) {
}
}

// TestErrorOther tests failures that do not return an Error struct
func TestErrorOther(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
w.Write([]byte(nonErrorStructErrorStatus))
}))
defer ts.Close()

_, err := Connect(ClientConfig{Endpoint: ts.URL, HTTPClient: ts.Client()})
if err == nil {
t.Error("connect should fail")
}
errStruct, ok := err.(*common.Error)
if !ok {
t.Errorf("call should return known error type: %v", err)
}
if errStruct.HTTPReturnedStatusCode != 500 {
t.Errorf("The error code is different from 500")
}
if err.Error() != "500: Internal Server Error" {
t.Errorf("Expected '500: %s', got '%s'", nonErrorStructErrorStatus, err.Error())
}
}

// TestConnectContextTimeout
func TestConnectContextTimeout(t *testing.T) {

Expand Down Expand Up @@ -174,3 +199,16 @@ func TestConnectDefaultContextCancel(t *testing.T) {
t.Error("Context should be cancelled")
}
}

func TestClientRunRawRequestNoURL(t *testing.T) {
client := APIClient{}

_, err := client.runRawRequest("", "", nil, "")
if err == nil {
t.Error("Request without relative path should have failed")
}

if err.Error() != "unable to execute request, no target provided" {
t.Errorf("Unexpected error response: %s", err.Error())
}
}

0 comments on commit 32343b1

Please sign in to comment.