-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters