From 9b41a1b340db39222b246a4226c93ab31d0353d0 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 31 May 2016 12:52:06 +0200 Subject: [PATCH] Add error unit tests for Info Signed-off-by: Vincent Demeester --- client/info_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/info_test.go b/client/info_test.go index d3d0175..aa5d9cf 100644 --- a/client/info_test.go +++ b/client/info_test.go @@ -13,6 +13,31 @@ import ( "golang.org/x/net/context" ) +func TestInfoServerError(t *testing.T) { + client := &Client{ + transport: newMockClient(nil, errorMock(http.StatusInternalServerError, "Server error")), + } + _, err := client.Info(context.Background()) + if err == nil || err.Error() != "Error response from daemon: Server error" { + t.Fatalf("expected a Server Error, got %v", err) + } +} + +func TestInfoInvalidResponseJSONError(t *testing.T) { + client := &Client{ + transport: newMockClient(nil, func(req *http.Request) (*http.Response, error) { + return &http.Response{ + StatusCode: http.StatusOK, + Body: ioutil.NopCloser(bytes.NewReader([]byte("invalid json"))), + }, nil + }), + } + _, err := client.Info(context.Background()) + if err == nil || !strings.Contains(err.Error(), "invalid character") { + t.Fatalf("expected a 'invalid character' error, got %v", err) + } +} + func TestInfo(t *testing.T) { expectedURL := "/info" client := &Client{