diff --git a/authorize.go b/authorize.go index b4f03dc..edd6d0b 100644 --- a/authorize.go +++ b/authorize.go @@ -31,13 +31,16 @@ func GetAuthorize(key, user string) (AuthorizeResponse, error) { } defer resp.Body.Close() - result, err := ioutil.ReadAll(resp.Body) - if err != nil { - return authorizeResp, errors.Wrap(err, "error in ReadAll") + if resp.StatusCode != http.StatusOK { + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return authorizeResp, errors.Wrap(err, "error in ReadAll") + } + return authorizeResp, errors.Errorf("error body: %s", body) } - if err := json.Unmarshal(result, &authorizeResp); err != nil { - return authorizeResp, errors.Wrap(err, "error in Unmarshal") + if err := json.NewDecoder(resp.Body).Decode(&authorizeResp); err != nil { + return authorizeResp, errors.Wrap(err, "error in Decode") } return authorizeResp, nil diff --git a/authorize_test.go b/authorize_test.go index f83e98d..7f42f90 100644 --- a/authorize_test.go +++ b/authorize_test.go @@ -1,15 +1,20 @@ package dozens import ( - "errors" + "fmt" "net/http" "strings" "testing" + + "github.com/delphinus/go-dozens/endpoint" + "github.com/jarcoal/httpmock" + "github.com/pkg/errors" ) func TestGetAuthorizeWithNewRequestError(t *testing.T) { originalMethodGet := methodGet methodGet = "(" // invalid method + defer func() { methodGet = originalMethodGet }() _, err := GetAuthorize("", "") result := err.Error() @@ -18,8 +23,6 @@ func TestGetAuthorizeWithNewRequestError(t *testing.T) { if strings.Index(result, expected) != 0 { t.Errorf("expected '%s', but got '%s'", expected, result) } - - methodGet = originalMethodGet } type mockedErrorClient struct{} @@ -31,6 +34,7 @@ func (c *mockedErrorClient) Do(req *http.Request) (*http.Response, error) { func TestGetAuthorizeWithDoError(t *testing.T) { originalClient := httpClient httpClient = &mockedErrorClient{} + defer func() { httpClient = originalClient }() _, err := GetAuthorize("", "") result := err.Error() @@ -39,13 +43,12 @@ func TestGetAuthorizeWithDoError(t *testing.T) { if strings.Index(result, expected) != 0 { t.Errorf("expected '%s', but got '%s'", expected, result) } - - httpClient = originalClient } func TestGetAuthorizeWithReadAllError(t *testing.T) { originalClient := httpClient httpClient = &mockedClient{} + defer func() { httpClient = originalClient }() _, err := GetAuthorize("", "") result := err.Error() @@ -54,6 +57,64 @@ func TestGetAuthorizeWithReadAllError(t *testing.T) { if strings.Index(result, expected) != 0 { t.Errorf("expected '%s', but got '%s'", expected, result) } +} + +func TestGetAuthorizeWithErrorResponse(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + url := endpoint.Authorize().String() + validStatus := http.StatusOK + invalidJSON := "(" + httpmock.RegisterResponder(methodGet, url, httpmock.NewStringResponder(validStatus, invalidJSON)) + + _, err := GetAuthorize("", "") + result := err.Error() + + expected := "error in Decode" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', but got '%s'", expected, result) + } +} + +func TestGetAuthorizeStatusNotOK(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() - httpClient = originalClient + url := endpoint.Authorize().String() + invalidStatus := http.StatusBadRequest + mockStr := "as a mock" + + httpmock.RegisterResponder(methodGet, url, httpmock.NewStringResponder(invalidStatus, mockStr)) + + _, err := GetAuthorize("", "") + result := errors.Cause(err).Error() + + expected := fmt.Sprintf("error body: %s", mockStr) + if result != expected { + t.Errorf("expected '%s',but got '%s'", expected, result) + } +} + +func TestGetAuthorizeValidResponse(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + url := endpoint.Authorize().String() + validStatus := http.StatusOK + validResp := AuthorizeResponse{"hoge"} + + responder, _ := httpmock.NewJsonResponder(validStatus, &validResp) + httpmock.RegisterResponder(methodGet, url, responder) + + resp, err := GetAuthorize("", "") + if err != nil { + t.Errorf("error got: %v", err) + } + + result := resp.AuthToken + expected := validResp.AuthToken + if result != expected { + t.Errorf("expected '%s', but got '%s'", expected, result) + } } diff --git a/record_test.go b/record_test.go index 2207ae6..a85b3c8 100644 --- a/record_test.go +++ b/record_test.go @@ -24,6 +24,7 @@ func TestDoRecordRequestInvalidRequest(t *testing.T) { func TestDoRecordRequestIOError(t *testing.T) { originalClient := httpClient httpClient = &mockedClient{} + defer func() { httpClient = originalClient }() _, err := doRecordRequest(&http.Request{}) result := err.Error() @@ -32,8 +33,6 @@ func TestDoRecordRequestIOError(t *testing.T) { if strings.Index(result, expected) != 0 { t.Errorf("expected '%s', but got '%s'", expected, result) } - - httpClient = originalClient } func TestDoRecordRequestStatusNotOK(t *testing.T) { diff --git a/request_test.go b/request_test.go index 916ca00..d4f697c 100644 --- a/request_test.go +++ b/request_test.go @@ -43,6 +43,7 @@ func TestMakeDelete(t *testing.T) { func TestMakeGetWithError(t *testing.T) { originalMethodGet := methodGet methodGet = "(" // invalid method rune + defer func() { methodGet = originalMethodGet }() p := endpoint.Endpoint{ Base: &url.URL{}, @@ -51,6 +52,4 @@ func TestMakeGetWithError(t *testing.T) { if _, err := MakeGet("", p); err == nil { t.Errorf("MakeGet did not return error") } - - methodGet = originalMethodGet } diff --git a/zone_test.go b/zone_test.go index d275d95..9da8a93 100644 --- a/zone_test.go +++ b/zone_test.go @@ -40,6 +40,7 @@ func (c *mockedClient) Do(req *http.Request) (*http.Response, error) { func TestDoZoneRequestIOError(t *testing.T) { originalClient := httpClient httpClient = &mockedClient{} + defer func() { httpClient = originalClient }() _, err := doZoneRequest(&http.Request{}) result := err.Error() @@ -48,28 +49,25 @@ func TestDoZoneRequestIOError(t *testing.T) { if strings.Index(result, expected) != 0 { t.Errorf("expected '%s', but got '%s'", expected, result) } - - httpClient = originalClient } func TestDoZoneRequestStatusNotOK(t *testing.T) { httpmock.Activate() defer httpmock.DeactivateAndReset() - method := "GET" hogeURL := "http://hoge.com" mockStr := "as a mock" badStatus := http.StatusBadRequest - httpmock.RegisterResponder(method, hogeURL, httpmock.NewStringResponder(badStatus, mockStr)) - req, _ := http.NewRequest(method, hogeURL, nil) + httpmock.RegisterResponder(methodGet, hogeURL, httpmock.NewStringResponder(badStatus, mockStr)) + req, _ := http.NewRequest(methodGet, hogeURL, nil) _, err := doZoneRequest(req) result := errors.Cause(err).Error() expected := fmt.Sprintf("error body: %s", mockStr) if result != expected { - t.Errorf("expected '%s', bug got '%s'", expected, result) + t.Errorf("expected '%s', but got '%s'", expected, result) } } @@ -89,7 +87,7 @@ func TestDoZoneRequestBadJSON(t *testing.T) { expected := "error in Decode" if strings.Index(result, expected) != 0 { - t.Errorf("expected '%s', bug got '%s'", expected, result) + t.Errorf("expected '%s', but got '%s'", expected, result) } }