From 69df1aac1e3bf980f7a7fc334264867a100f1051 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 10:22:51 +0900 Subject: [PATCH 01/11] Use mocked client to test --- record.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/record.go b/record.go index 0a02faf..f919aa6 100644 --- a/record.go +++ b/record.go @@ -25,7 +25,7 @@ type record struct { func doRecordRequest(req *http.Request) (RecordResponse, error) { recordResp := RecordResponse{[]record{}} - resp, err := http.DefaultClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { return recordResp, errors.Wrap(err, "error in Do") } From 0fd79ec093d72a8f8724ac249cf3c595dc47f908 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 10:23:01 +0900 Subject: [PATCH 02/11] Fix typo --- zone_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zone_test.go b/zone_test.go index bf1a304..d275d95 100644 --- a/zone_test.go +++ b/zone_test.go @@ -112,6 +112,6 @@ func TestDoZoneRequestValidResponse(t *testing.T) { } if string(result) != expected { - t.Errorf("expected '%+v', bug got '%+v'", expected, result) + t.Errorf("expected '%+v', bug got '%+v'", expected, string(result)) } } From 1e3b6df732cbc00ce08d56c524e39f186fd28ef5 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 10:23:09 +0900 Subject: [PATCH 03/11] Add record_test --- record_test.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 record_test.go diff --git a/record_test.go b/record_test.go new file mode 100644 index 0000000..ac8fb82 --- /dev/null +++ b/record_test.go @@ -0,0 +1,101 @@ +package dozens + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + "testing" + + "github.com/jarcoal/httpmock" + "github.com/pkg/errors" +) + +func TestDoRecordRequestInvalidRequest(t *testing.T) { + _, err := doRecordRequest(&http.Request{}) + result := err.Error() + + expected := "error in Do" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', but got '%s'", expected, result) + } +} + +func TestDoRecordRequestIOError(t *testing.T) { + originalClient := httpClient + httpClient = &mockedClient{} + + _, err := doRecordRequest(&http.Request{}) + result := err.Error() + + expected := "error in ReadAll" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', but got '%s'", expected, result) + } + + httpClient = originalClient +} + +func TestDoRecordRequestStatusNotOK(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) + + _, err := doRecordRequest(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) + } +} + +func TestDoRecordRequestBadJSON(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + method := "GET" + hogeURL := "http://hoge.com" + badJSON := "{hoge}" + + httpmock.RegisterResponder(method, hogeURL, httpmock.NewStringResponder(http.StatusOK, badJSON)) + req, _ := http.NewRequest(method, hogeURL, nil) + + _, err := doRecordRequest(req) + result := err.Error() + + expected := "error in Decode" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', bug got '%s'", expected, result) + } +} + +func TestDoRecordRequestValidResponse(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + method := "GET" + hogeURL := "http://hoge.com" + + expected := `{"record":[{"id":"hoge","name":"fuga","type":"A","prio":"10","content":"192.168.0.1","ttl":"10"}]}` + httpmock.RegisterResponder(method, hogeURL, httpmock.NewStringResponder(http.StatusOK, expected)) + req, _ := http.NewRequest(method, hogeURL, nil) + + resultResp, _ := doRecordRequest(req) + result, err := json.Marshal(&resultResp) + if err != nil { + t.Errorf("error in Marshal: %v", err) + return + } + + if string(result) != expected { + t.Errorf("expected '%+v', bug got '%+v'", expected, string(result)) + } +} From 8a5bc877d2c740254af8ae65d02da152977d8192 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 15:39:30 +0900 Subject: [PATCH 04/11] Do goveralls in $REPO_PATH --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 4adcf94..134386a 100644 --- a/circle.yml +++ b/circle.yml @@ -20,4 +20,4 @@ test: post: - mkdir -p $CIRCLE_TEST_REPORTS/golang - cat $TEST_FILE | go-junit-report > $CIRCLE_TEST_REPORTS/golang/junit.xml - - goveralls -coverprofile=$COVERAGE -service=circle-ci -repotoken=$COVERALLS_TOKEN + - cd $REPO_PATH && goveralls -coverprofile=$COVERAGE -service=circle-ci -repotoken=$COVERALLS_TOKEN From a6faa4612e9c16d7dbecd9bac8f8fae87327f408 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 18:01:09 +0900 Subject: [PATCH 05/11] Add test for empty response --- record_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/record_test.go b/record_test.go index ac8fb82..2207ae6 100644 --- a/record_test.go +++ b/record_test.go @@ -99,3 +99,26 @@ func TestDoRecordRequestValidResponse(t *testing.T) { t.Errorf("expected '%+v', bug got '%+v'", expected, string(result)) } } +func TestDoRecordRequestEmptyResponse(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + method := "GET" + hogeURL := "http://hoge.com" + + emptyResp := `[]` + httpmock.RegisterResponder(method, hogeURL, httpmock.NewStringResponder(http.StatusOK, emptyResp)) + req, _ := http.NewRequest(method, hogeURL, nil) + + resultResp, _ := doRecordRequest(req) + result, err := json.Marshal(&resultResp) + if err != nil { + t.Errorf("error in Marshal: %v", err) + return + } + + expected := `{"record":[]}` + if string(result) != expected { + t.Errorf("expected '%+v', bug got '%+v'", expected, string(result)) + } +} From 884357c395408fa903bbaa2c69dbe85ca77f4501 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 18:43:33 +0900 Subject: [PATCH 06/11] Add test for request.go --- request_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 request_test.go diff --git a/request_test.go b/request_test.go new file mode 100644 index 0000000..0b0dd19 --- /dev/null +++ b/request_test.go @@ -0,0 +1,51 @@ +package dozens + +import ( + "net/url" + "testing" + + "github.com/delphinus/go-dozens/endpoint" +) + +func TestMakeGet(t *testing.T) { + hogeURL, _ := url.Parse("http://hoge.com") + p := endpoint.Endpoint{ + Base: hogeURL, + Chunk: "", + } + if _, err := MakeGet("", p); err != nil { + t.Errorf("MakeGet returned error: %v", err) + } +} + +func TestMakePost(t *testing.T) { + hogeURL, _ := url.Parse("http://hoge.com") + p := endpoint.Endpoint{ + Base: hogeURL, + Chunk: "", + } + if _, err := MakePost("", p, nil); err != nil { + t.Errorf("MakeGet returned error: %v", err) + } +} + +func TestMakeDelete(t *testing.T) { + hogeURL, _ := url.Parse("http://hoge.com") + p := endpoint.Endpoint{ + Base: hogeURL, + Chunk: "", + } + if _, err := MakeDelete("", p); err != nil { + t.Errorf("MakeGet returned error: %v", err) + } +} + +func TestMakeGetWithError(t *testing.T) { + p := endpoint.Endpoint{ + Base: &url.URL{}, + Chunk: "", + } + if _, err := MakeGet("", p); err == nil { + t.Errorf("MakeGet did not return error") + } +} From b999ee7a9076c3932212029843e31a22ad85cd69 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 18:50:10 +0900 Subject: [PATCH 07/11] Use variable to test --- request.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/request.go b/request.go index 989db85..4002dc6 100644 --- a/request.go +++ b/request.go @@ -8,19 +8,23 @@ import ( "github.com/pkg/errors" ) +var methodGet = "GET" +var methodPost = "POST" +var methodDelete = "DELETE" + // MakeGet returns request for dozens func MakeGet(token string, p endpoint.Endpoint) (*http.Request, error) { - return makeRequest("GET", token, p, nil) + return makeRequest(methodGet, token, p, nil) } // MakePost returns request for dozens func MakePost(token string, p endpoint.Endpoint, body io.Reader) (*http.Request, error) { - return makeRequest("POST", token, p, body) + return makeRequest(methodPost, token, p, body) } // MakeDelete returns request for dozens func MakeDelete(token string, p endpoint.Endpoint) (*http.Request, error) { - return makeRequest("DELETE", token, p, nil) + return makeRequest(methodDelete, token, p, nil) } func makeRequest(method, token string, p endpoint.Endpoint, body io.Reader) (*http.Request, error) { From cb493f06e6092f4b8fa98de1cdf6a27d37e397ce Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 18:50:25 +0900 Subject: [PATCH 08/11] Set invalid method to test --- request_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/request_test.go b/request_test.go index 0b0dd19..b1b153e 100644 --- a/request_test.go +++ b/request_test.go @@ -45,6 +45,7 @@ func TestMakeGetWithError(t *testing.T) { Base: &url.URL{}, Chunk: "", } + methodGet = "(" // invalid method rune if _, err := MakeGet("", p); err == nil { t.Errorf("MakeGet did not return error") } From 4c32d34d6dd428b9ded976dad148d487a5714a21 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 20:04:52 +0900 Subject: [PATCH 09/11] Use variables to test --- authorize.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/authorize.go b/authorize.go index 3c51368..b4f03dc 100644 --- a/authorize.go +++ b/authorize.go @@ -18,14 +18,14 @@ type AuthorizeResponse struct { func GetAuthorize(key, user string) (AuthorizeResponse, error) { authorizeResp := AuthorizeResponse{} - req, err := http.NewRequest("GET", endpoint.Authorize().String(), nil) + req, err := http.NewRequest(methodGet, endpoint.Authorize().String(), nil) if err != nil { return authorizeResp, errors.Wrap(err, "error in NewRequest") } req.Header.Set("X-Auth-Key", key) req.Header.Set("X-Auth-User", user) - resp, err := http.DefaultClient.Do(req) + resp, err := httpClient.Do(req) if err != nil { return authorizeResp, errors.Wrap(err, "error in Do") } From 399ef998605a53fb8914d31d5704b7201bdc50ad Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 20:05:06 +0900 Subject: [PATCH 10/11] Fix test --- request_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/request_test.go b/request_test.go index b1b153e..916ca00 100644 --- a/request_test.go +++ b/request_test.go @@ -41,12 +41,16 @@ func TestMakeDelete(t *testing.T) { } func TestMakeGetWithError(t *testing.T) { + originalMethodGet := methodGet + methodGet = "(" // invalid method rune + p := endpoint.Endpoint{ Base: &url.URL{}, Chunk: "", } - methodGet = "(" // invalid method rune if _, err := MakeGet("", p); err == nil { t.Errorf("MakeGet did not return error") } + + methodGet = originalMethodGet } From 302406ac28605a263d36f5d5934f037138f9e4f6 Mon Sep 17 00:00:00 2001 From: delphinus Date: Fri, 30 Dec 2016 20:05:14 +0900 Subject: [PATCH 11/11] Add test for GetAuthorize --- authorize_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 authorize_test.go diff --git a/authorize_test.go b/authorize_test.go new file mode 100644 index 0000000..f83e98d --- /dev/null +++ b/authorize_test.go @@ -0,0 +1,59 @@ +package dozens + +import ( + "errors" + "net/http" + "strings" + "testing" +) + +func TestGetAuthorizeWithNewRequestError(t *testing.T) { + originalMethodGet := methodGet + methodGet = "(" // invalid method + + _, err := GetAuthorize("", "") + result := err.Error() + + expected := "error in NewRequest" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', but got '%s'", expected, result) + } + + methodGet = originalMethodGet +} + +type mockedErrorClient struct{} + +func (c *mockedErrorClient) Do(req *http.Request) (*http.Response, error) { + return nil, errors.New("hoge error") +} + +func TestGetAuthorizeWithDoError(t *testing.T) { + originalClient := httpClient + httpClient = &mockedErrorClient{} + + _, err := GetAuthorize("", "") + result := err.Error() + + expected := "error in Do" + 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{} + + _, err := GetAuthorize("", "") + result := err.Error() + + expected := "error in ReadAll" + if strings.Index(result, expected) != 0 { + t.Errorf("expected '%s', but got '%s'", expected, result) + } + + httpClient = originalClient +}