Skip to content

Commit

Permalink
supported method [PUT,DELETE,OPTIONS,HEAD]
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokomi committed Oct 25, 2015
1 parent baf9625 commit d95f198
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 22 deletions.
43 changes: 35 additions & 8 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,49 @@
package example

import "net/http"
import (
"io/ioutil"
"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" {
switch r.Method {
case "GET", "HEAD":
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("hoge"))
case "PUT":
message := r.FormValue("message")
if message == "hello" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(message))
} else {
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "hello", "url": "/hoge?message=hello"}`))
}
case "POST":
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(http.StatusText(http.StatusBadRequest)))
return
}
w.Header().Set("Location", "http://localhost:8080/hoge/"+string(data))
w.WriteHeader(http.StatusCreated)
w.Write(data)
case "DELETE":
w.WriteHeader(http.StatusNoContent)
case "OPTIONS":
w.Header().Add("Allow", "GET,HEAD,PUT,POST,DELETE")
w.WriteHeader(http.StatusNoContent)
default:
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) {
Expand All @@ -26,7 +53,7 @@ func hogeJSONHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Add("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name": "hoge", "age": 20}`))
}
29 changes: 29 additions & 0 deletions hhth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type HTTPHandlerTestHelper interface {

// method
Get(urlStr string, testCases ...HandlerTestCaseFunc) Response
Head(urlStr string, testCases ...HandlerTestCaseFunc) Response
Delete(urlStr string, testCases ...HandlerTestCaseFunc) Response
Options(urlStr string, testCases ...HandlerTestCaseFunc) Response
Put(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response
Post(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response
}

Expand Down Expand Up @@ -55,6 +59,31 @@ func (h *httpHandlerTestHelper) Get(urlStr string, testCases ...HandlerTestCaseF
return h.do(nil, testCases...)
}

func (h *httpHandlerTestHelper) Head(urlStr string, testCases ...HandlerTestCaseFunc) Response {
h.method = "HEAD"
h.url = urlStr
return h.do(nil, testCases...)
}

func (h *httpHandlerTestHelper) Delete(urlStr string, testCases ...HandlerTestCaseFunc) Response {
h.method = "DELETE"
h.url = urlStr
return h.do(nil, testCases...)
}

func (h *httpHandlerTestHelper) Options(urlStr string, testCases ...HandlerTestCaseFunc) Response {
h.method = "OPTIONS"
h.url = urlStr
return h.do(nil, testCases...)
}

func (h *httpHandlerTestHelper) Put(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response {
h.method = "PUT"
h.url = urlStr
h.SetHeader("Content-Type", bodyType)
return h.do(body, testCases...)
}

func (h *httpHandlerTestHelper) Post(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response {
h.method = "POST"
h.url = urlStr
Expand Down
120 changes: 106 additions & 14 deletions hhth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package hhth_test
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"testing"

"github.com/kyokomi/hhth"
)

func TestHogeHandler(t *testing.T) {
func TestGetHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
hhth.TestCaseStatusCode(http.StatusOK),
Expand All @@ -25,6 +26,54 @@ func TestHogeHandler(t *testing.T) {
fmt.Println(resp.String())
}

func TestHeadHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
hhth.TestCaseStatusCode(http.StatusOK),
hhth.TestCaseContentType("text/plain; charset=utf-8"),
hhth.TestCaseContentLength(len("hogehoge")),
)

resp := hhtHelper.Head("/hoge")
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
fmt.Println(resp.String())
}

func TestDeleteHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
hhth.TestCaseStatusCode(http.StatusNoContent),
)

resp := hhtHelper.Delete("/hoge")
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
fmt.Println(resp.String())
}

func TestOptionsHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
hhth.TestCaseStatusCode(http.StatusNoContent),
hhth.HandlerTestCaseFunc(func(resp hhth.Response) error {
r, _ := resp.Result()
if r.Header().Get("Allow") != "GET,HEAD,PUT,POST,DELETE" {
return fmt.Errorf("allow header error %s", r.Header().Get("Allow"))
}
return nil
}),
)

resp := hhtHelper.Options("/hoge")
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
fmt.Println(resp.String())
}

func TestErrorStatusCode(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
Expand Down Expand Up @@ -106,6 +155,24 @@ func TestHogeHeaderHandler(t *testing.T) {
fmt.Println(resp.String())
}

func TestPutHogeHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
hhth.TestCaseStatusCode(http.StatusCreated),
)

formData := url.Values{}
formData.Set("message", "hello")

resp := hhtHelper.Put("/hoge", "application/x-www-form-urlencoded",
bytes.NewBufferString(formData.Encode()),
)
if resp.Error() != nil {
t.Errorf("error %s", resp.Error())
}
fmt.Println(resp.String())
}

func TestPostHandler(t *testing.T) {
hhtHelper := hhth.New(http.DefaultServeMux)
hhtHelper.SetTestCase(
Expand Down Expand Up @@ -231,15 +298,40 @@ func init() {
}

func hogeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
renderError(http.StatusMethodNotAllowed, w)
return
switch r.Method {
case "GET", "HEAD":
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Hoge-Version", "1.0.0")
w.WriteHeader(http.StatusOK)
w.Write([]byte("hogehoge"))
case "PUT":
message := r.FormValue("message")
if message == "hello" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(message))
} else {
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "hello", "url": "/hoge?message=hello"}`))
}
case "POST":
data, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(http.StatusText(http.StatusBadRequest)))
return
}
w.Header().Set("Location", "http://localhost:8080/hoge/"+string(data))
w.WriteHeader(http.StatusCreated)
w.Write(data)
case "DELETE":
w.WriteHeader(http.StatusNoContent)
case "OPTIONS":
w.Header().Add("Allow", "GET,HEAD,PUT,POST,DELETE")
w.WriteHeader(http.StatusNoContent)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed)))
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Hoge-Version", "1.0.0")
w.Write([]byte("hogehoge"))
}

func getFormHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -253,8 +345,8 @@ func getFormHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("get-form"))
}

Expand All @@ -264,8 +356,8 @@ func hogeJSONHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name": "hogehoge", "age": 20}`))
}

Expand All @@ -275,8 +367,8 @@ func errorJSONHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name": "hogehoge", "age": 20`)) // json parse error
}

Expand All @@ -292,8 +384,8 @@ func headerHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("header ok " + xAppHoge))
}

Expand All @@ -313,8 +405,8 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
return
}

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("post ok " + r.PostForm.Encode()))
}

Expand Down

0 comments on commit d95f198

Please sign in to comment.