Skip to content

Commit

Permalink
add HTTP methods support
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Dec 3, 2016
1 parent 322f2f4 commit dc47ac7
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
50 changes: 43 additions & 7 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,35 @@ import (
)

// Version is this package's version number.
const Version = "0.0.1"
const Version = "0.0.2"

// Handler responds to an HTTP request.
// Handler responds to a HTTP request.
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request, map[string]string)
}

// The HandlerFunc type is an adapter to allow the use of an ordinary function
// as a Handler.
type HandlerFunc func(http.ResponseWriter, *http.Request, map[string]string)

func (f HandlerFunc) ServeHTTP(res http.ResponseWriter, req *http.Request, params map[string]string) {
f(res, req, params)
}

// Mux is the HTTP request multiplexer.
type Mux struct {
root map[string]*routing.Node
}

// New returns a new mux.
func New() *Mux {
return &Mux{}
return &Mux{root: map[string]*routing.Node{}}
}

// Handle registers the handler for the given method and url.
func (m *Mux) Handle(method string, url string, handler Handler) *Mux {
method = strings.ToUpper(method)

if _, ok := m.root[method]; !ok {
m.root[method] = routing.New()
}
Expand All @@ -37,6 +47,36 @@ func (m *Mux) Handle(method string, url string, handler Handler) *Mux {
return m
}

// Get registers a handler for the GET http method.
func (m *Mux) Get(url string, handler Handler) *Mux {
return m.Handle(http.MethodGet, url, handler)
}

// Post registers a handler for the POST http method.
func (m *Mux) Post(url string, handler Handler) *Mux {
return m.Handle(http.MethodPost, url, handler)
}

// Put registers a handler for the PUT http method.
func (m *Mux) Put(url string, handler Handler) *Mux {
return m.Handle(http.MethodPut, url, handler)
}

// Delete registers a handler for the DELETE http method.
func (m *Mux) Delete(url string, handler Handler) *Mux {
return m.Handle(http.MethodDelete, url, handler)
}

// Head registers a handler for the HEAD http method.
func (m *Mux) Head(url string, handler Handler) *Mux {
return m.Handle(http.MethodHead, url, handler)
}

// Patch registers a handler for the PATCH http method.
func (m *Mux) Patch(url string, handler Handler) *Mux {
return m.Handle(http.MethodPatch, url, handler)
}

func (m Mux) ServeHTTP(res http.ResponseWriter, req *http.Request) {
uri := req.RequestURI
method := req.Method
Expand All @@ -56,10 +96,6 @@ func (m Mux) ServeHTTP(res http.ResponseWriter, req *http.Request) {
return
}

if method == http.MethodHead {
method = http.MethodGet
}

node, ok := m.root[method]

if !ok {
Expand Down
103 changes: 103 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,104 @@
package mux

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/suite"
)

type MuxSuite struct {
suite.Suite

server *httptest.Server
}

func (s *MuxSuite) SetupSuite() {
mux := New()

mux.Get("/get/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

mux.Post("/post/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

mux.Put("/put/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

mux.Delete("/delete/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

mux.Head("/head/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

mux.Patch("/patch/:id", HandlerFunc(func(res http.ResponseWriter, req *http.Request, params map[string]string) {
res.WriteHeader(http.StatusOK)

res.Write([]byte(params["id"]))
}))

s.server = httptest.NewServer(mux)
}

func (s *MuxSuite) TestMethods() {
methods := []string{
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodDelete,
http.MethodHead,
http.MethodPatch,
}

for _, method := range methods {
req, err := http.NewRequest(method, s.server.URL+"/"+strings.ToLower(method)+"/123", nil)

s.Nil(err)

res, err := sendRequest(req)

s.Nil(err)
s.Equal(http.StatusOK, res.StatusCode)

if method != http.MethodHead {
s.Equal([]byte("123"), getResRawBody(res))
}
}
}

func TestMux(t *testing.T) {
suite.Run(t, new(MuxSuite))
}

func sendRequest(req *http.Request) (*http.Response, error) {
cli := &http.Client{}
return cli.Do(req)
}

func getResRawBody(res *http.Response) []byte {
bytes, err := ioutil.ReadAll(res.Body)

if err != nil {
panic(err)
}

return bytes
}

0 comments on commit dc47ac7

Please sign in to comment.