Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokomi committed Oct 15, 2015
1 parent 844bf30 commit 9b9b633
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -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}`))
}
43 changes: 43 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
@@ -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"])
}
}
2 changes: 1 addition & 1 deletion hhth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

0 comments on commit 9b9b633

Please sign in to comment.