diff --git a/README.md b/README.md index 6fa3553..b4ba199 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,116 @@ hhth hhth is httpHandler test helper library of the golang. +## Install + +``` +go get github.com/kyokomi/hhth +``` + +## Example + +### Handler + +``` +package example + +import "net/http" + +func init() { + http.HandleFunc("/hoge", hogeHandler) + http.HandleFunc("/hoge.json", hogeJSONHandler) +} + +func hogeHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/plain; charset=utf-8") + w.Write([]byte("hoge")) +} + +func hogeJSONHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "application/json; charset=UTF-8") + w.Write([]byte(`{"name": "hoge", "age": 20}`)) +} +``` + +### Get Test + +``` +package example_test + +import ( + "net/http" + "testing" + + "github.com/kyokomi/hhth" +) + +func TestHogeHandler(t *testing.T) { + hhtHelper := hhth.New(http.DefaultServeMux) + + resp := hhtHelper.Get("/hoge", + hhth.TestCaseStatusCode(http.StatusOK), + hhth.TestCaseContentType("text/plain; charset=utf-8"), + ) + if resp.Error() != nil { + t.Errorf("error %s", resp.Error()) + } + if resp.String() != "hoge" { + t.Errorf("error response body hoge != %s", resp.String()) + } +} +``` + +### JSON Parse + +``` +package example_test + +import ( + "net/http" + "testing" + + "github.com/kyokomi/hhth" +) + +func TestJSONParse(t *testing.T) { + hhtHelper := hhth.New(http.DefaultServeMux) + + var resp map[string]interface{} + if err := hhtHelper.Get("/hoge.json", + hhth.TestCaseStatusCode(http.StatusOK), + hhth.TestCaseContentType("application/json; charset=UTF-8"), + ).JSON(&resp); err != nil { + t.Errorf("error %s", err) + } + + if resp["name"].(string) != "hoge" { + t.Errorf("error json response name != %s", resp["name"]) + } + + if resp["age"].(float64) != 20 { + t.Errorf("error json response age != %s", resp["age"]) + } +} +``` + ## TODO - [x] test +- [x] example - [x] POST - [ ] PUT - [ ] DELETE diff --git a/example/example.go b/example/example.go new file mode 100644 index 0000000..a10b74a --- /dev/null +++ b/example/example.go @@ -0,0 +1,32 @@ +package example + +import "net/http" + +func init() { + http.HandleFunc("/hoge", hogeHandler) + http.HandleFunc("/hoge.json", hogeJSONHandler) +} + +func hogeHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "text/plain; charset=utf-8") + w.Write([]byte("hoge")) +} + +func hogeJSONHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) + return + } + + w.WriteHeader(http.StatusOK) + w.Header().Add("Content-Type", "application/json; charset=UTF-8") + w.Write([]byte(`{"name": "hoge", "age": 20}`)) +} diff --git a/example/example_test.go b/example/example_test.go new file mode 100644 index 0000000..bb13396 --- /dev/null +++ b/example/example_test.go @@ -0,0 +1,43 @@ +package example_test + +import ( + "net/http" + "testing" + + "github.com/kyokomi/hhth" +) + +func TestHogeHandler(t *testing.T) { + hhtHelper := hhth.New(http.DefaultServeMux) + + resp := hhtHelper.Get("/hoge", + hhth.TestCaseStatusCode(http.StatusOK), + hhth.TestCaseContentType("text/plain; charset=utf-8"), + ) + if resp.Error() != nil { + t.Errorf("error %s", resp.Error()) + } + if resp.String() != "hoge" { + t.Errorf("error response body hoge != %s", resp.String()) + } +} + +func TestJSONParse(t *testing.T) { + hhtHelper := hhth.New(http.DefaultServeMux) + + var resp map[string]interface{} + if err := hhtHelper.Get("/hoge.json", + hhth.TestCaseStatusCode(http.StatusOK), + hhth.TestCaseContentType("application/json; charset=UTF-8"), + ).JSON(&resp); err != nil { + t.Errorf("error %s", err) + } + + if resp["name"].(string) != "hoge" { + t.Errorf("error json response name != %s", resp["name"]) + } + + if resp["age"].(float64) != 20 { + t.Errorf("error json response age != %s", resp["age"]) + } +} diff --git a/hhth_test.go b/hhth_test.go index 110b340..fd325fb 100644 --- a/hhth_test.go +++ b/hhth_test.go @@ -256,5 +256,5 @@ func postHandler(w http.ResponseWriter, r *http.Request) { func renderError(statusCode int, w http.ResponseWriter) { w.WriteHeader(statusCode) - w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed))) + w.Write([]byte(http.StatusText(statusCode))) }