Skip to content

Commit

Permalink
added testcse pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokomi committed Oct 14, 2015
1 parent 0199527 commit 816fac3
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 68 deletions.
29 changes: 29 additions & 0 deletions circleci/test/post/gocoverage_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

# 参考URL: http://www.songmu.jp/riji/entry/2015-01-15-goveralls-multi-package.html

set -e

# 中断した時とかにゴミが残らないようにcleanupする
cleanup() {
retval=$?
if [ $tmpprof != "" ] && [ -f $tmpprof ]; then
rm -f $tmpprof
fi
exit $retval
}
trap cleanup INT QUIT TERM EXIT

# メインの処理
prof=${1:-".profile.cov"}
echo "mode: count" > $prof
gopath1=$(echo $GOPATH | cut -d: -f3)
echo "mode: count" > $prof
for pkg in $(go list ./...); do
tmpprof=$gopath1/src/$pkg/profile.tmp
go test -covermode=count -coverprofile=$tmpprof $pkg
if [ -f $tmpprof ]; then
cat $tmpprof | tail -n +2 >> $prof
rm $tmpprof
fi
done
30 changes: 17 additions & 13 deletions hhth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"io"
"net/http"
"net/http/httptest"

"github.com/kyokomi/hhth/testcase"
)

const (
Expand All @@ -15,8 +17,8 @@ type HTTPHandlerTestHelper interface {
SetForm(key, value string)

// method
Get(urlStr string, testCase TestCase) Response
Post(urlStr string, bodyType string, body io.Reader, testCase TestCase) Response
Get(urlStr string, testCases ...testcase.HandlerTestCase) Response
Post(urlStr string, bodyType string, body io.Reader, testCases ...testcase.HandlerTestCase) Response
}

var _ HTTPHandlerTestHelper = (*httpHandlerTestHelper)(nil)
Expand All @@ -38,17 +40,17 @@ type httpHandlerTestHelper struct {
form map[string]string
}

func (h *httpHandlerTestHelper) Get(urlStr string, testCase TestCase) Response {
func (h *httpHandlerTestHelper) Get(urlStr string, testCases ...testcase.HandlerTestCase) Response {
h.method = "GET"
h.url = urlStr
return h.do(testCase, nil)
return h.do(nil, testCases...)
}

func (h *httpHandlerTestHelper) Post(urlStr string, bodyType string, body io.Reader, testCase TestCase) Response {
func (h *httpHandlerTestHelper) Post(urlStr string, bodyType string, body io.Reader, testCases ...testcase.HandlerTestCase) Response {
h.method = "POST"
h.url = urlStr
h.SetHeader("Content-Type", bodyType)
return h.do(testCase, body)
return h.do(body, testCases...)
}

func (h *httpHandlerTestHelper) SetHeader(key, value string) {
Expand All @@ -59,9 +61,13 @@ func (h *httpHandlerTestHelper) SetForm(key, value string) {
h.form[key] = value
}

func (h *httpHandlerTestHelper) do(testCase TestCase, body io.Reader) *response {
func (h *httpHandlerTestHelper) do(body io.Reader, testCases ...testcase.HandlerTestCase) *response {
resp := httptest.NewRecorder()
req, err := http.NewRequest(h.method, h.url, body)
if err != nil {
return &response{err: err, response: nil}
}

if body == nil {
for key, val := range h.form {
req.Form.Set(key, val)
Expand All @@ -72,14 +78,12 @@ func (h *httpHandlerTestHelper) do(testCase TestCase, body io.Reader) *response
req.Header.Set(key, val)
}

if err != nil {
return &response{err: err, response: nil}
}

h.handler.ServeHTTP(resp, req)

if err := testCase.Execute(resp); err != nil {
return &response{err: err, response: nil}
for _, testCase := range testCases {
if err := testCase.Execute(resp); err != nil {
return &response{err: err, response: nil}
}
}

return &response{err: nil, response: resp}
Expand Down
30 changes: 21 additions & 9 deletions hhth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import (
"testing"

"github.com/kyokomi/hhth"
"github.com/kyokomi/hhth/testcase"
)

func TestHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)

testCase1 := hhth.NewTestCase(http.StatusOK, "text/plain; charset=utf-8")
resp := hhtHelper.Get("/hoge", testCase1)
resp := hhtHelper.Get("/hoge",
testcase.StatusCode(http.StatusOK),
testcase.ContentType("text/plain; charset=utf-8"),
)
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
Expand All @@ -23,19 +26,26 @@ func TestHogeHandler(t *testing.T) {

func TestHogeJSONHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
testCase := hhth.NewTestCase(http.StatusOK, "application/json; charset=UTF-8")

var resp map[string]interface{}
if err := hhtHelper.Get("/hoge.json", testCase).JSON(&resp); err != nil {
if err := hhtHelper.Get("/hoge.json",
testcase.StatusCode(http.StatusOK),
testcase.ContentType("application/json; charset=UTF-8"),
).JSON(&resp); err != nil {
t.Errorf("error %s", err)
}
fmt.Println(resp)
}

func TestHogeHeaderHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)

hhtHelper.SetHeader("X-App-Hoge", "hoge-header")
testCase := hhth.NewTestCase(http.StatusOK, "text/plain; charset=utf-8")
resp := hhtHelper.Get("/header", testCase)

resp := hhtHelper.Get("/header",
testcase.StatusCode(http.StatusOK),
testcase.ContentType("text/plain; charset=utf-8"),
)
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
Expand All @@ -44,14 +54,16 @@ func TestHogeHeaderHandler(t *testing.T) {

func TestPostHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
testCase := hhth.NewTestCase(http.StatusOK, "text/plain; charset=utf-8")

formData := url.Values{}
formData.Set("name", "hoge")
formData.Set("age", "19")

body := bytes.NewBufferString(formData.Encode())
resp := hhtHelper.Post("/post", "application/x-www-form-urlencoded", body, testCase)
resp := hhtHelper.Post("/post", "application/x-www-form-urlencoded",
bytes.NewBufferString(formData.Encode()),
testcase.StatusCode(http.StatusOK),
testcase.ContentType("text/plain; charset=utf-8"),
)
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
Expand Down
46 changes: 0 additions & 46 deletions testcase.go

This file was deleted.

25 changes: 25 additions & 0 deletions testcase/contentlength.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testcase

import (
"fmt"
"net/http/httptest"
)

type contentLengthTestCase struct {
contentLength int
}

func ContentLength(contentLength int) HandlerTestCase {
return &contentLengthTestCase{
contentLength: contentLength,
}
}

var _ HandlerTestCase = (*contentLengthTestCase)(nil)

func (t *contentLengthTestCase) Execute(resp *httptest.ResponseRecorder) error {
if t.contentLength != resp.Body.Len() {
return fmt.Errorf("should Content-Length %d ≠ %d", t.contentLength, resp.Body.Len())
}
return nil
}
26 changes: 26 additions & 0 deletions testcase/contenttype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testcase

import (
"fmt"
"net/http/httptest"
)

type contentTypeTestCase struct {
contentType string
}

func ContentType(contentType string) HandlerTestCase {
return &contentTypeTestCase{
contentType: contentType,
}
}

var _ HandlerTestCase = (*contentTypeTestCase)(nil)

func (t *contentTypeTestCase) Execute(resp *httptest.ResponseRecorder) error {
contentType := resp.Header().Get("Content-Type")
if contentType != t.contentType {
return fmt.Errorf("should Content-Type %s ≠ %s", contentType, t.contentType)
}
return nil
}
25 changes: 25 additions & 0 deletions testcase/statuscode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testcase

import (
"fmt"
"net/http/httptest"
)

type statusCodeTestCase struct {
statusCode int
}

func StatusCode(statusCode int) HandlerTestCase {
return &statusCodeTestCase{
statusCode: statusCode,
}
}

var _ HandlerTestCase = (*statusCodeTestCase)(nil)

func (t *statusCodeTestCase) Execute(resp *httptest.ResponseRecorder) error {
if resp.Code != t.statusCode {
return fmt.Errorf("should return HTTP OK %d ≠ %d", resp.Code, t.statusCode)
}
return nil
}
9 changes: 9 additions & 0 deletions testcase/testcase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testcase

import (
"net/http/httptest"
)

type HandlerTestCase interface {
Execute(resp *httptest.ResponseRecorder) error
}

0 comments on commit 816fac3

Please sign in to comment.