Skip to content

Commit

Permalink
Merge branch 'hotfix-0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
takama committed Aug 21, 2014
2 parents 60c1c87 + 2f7e493 commit bc719d4
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Content-Length: 102
}
```

- Use timer to calculate the elapsed time of request handling:
- Use timer to calculate duration of request handling:
```go
func main() {
r := router.New()
Expand Down
3 changes: 3 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (p *parser) register(path string, handle Handle) bool {
}

func (p *parser) get(path string) (handle Handle, result []Param, ok bool) {
if handle, ok := p.static[path]; ok {
return handle, nil, true
}
if parts, ok := split(path); ok {
if handle, ok := p.static["/"+strings.Join(parts, "/")]; ok {
return handle, nil, true
Expand Down
111 changes: 97 additions & 14 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,66 @@ import (
"testing"
)

type registered struct {
path string
handle Handle
}

type expected struct {
path string
request string
handle Handle
data string
paramCount int
params []Param
}

var setOfExpected = []expected{
var setOfRegistered = []registered{
{
"/hello/:name",
"/hello/Jane",
func(c *Control) {
c.Body("Hello " + c.Get(":name"))
},
},
{
"/hello/John",
func(c *Control) {
c.Body("Hello from static path")
},
},
{
"/:h/:n",
func(c *Control) {
c.Body(c.Get(":n") + " from " + c.Get(":h"))
},
},
{
"/products/:name/orders/:id",
func(c *Control) {
c.Body("Product: " + c.Get(":name") + " order# " + c.Get(":id"))
},
},
{
"/products/book/orders/:id",
func(c *Control) {
c.Body("Product: book order# " + c.Get(":id"))
},
},
{
"/products/:name/:order/:id",
func(c *Control) {
c.Body("Product: " + c.Get(":name") + " # " + c.Get(":id"))
},
},
{
"/:product/:name/:order/:id",
func(c *Control) {
c.Body(c.Get(":product") + " " + c.Get(":name") + " " + c.Get(":order") + " # " + c.Get(":id"))
},
},
}

var setOfExpected = []expected{
{
"/hello/Jane",
"Hello Jane",
1,
[]Param{
Expand All @@ -33,32 +77,71 @@ var setOfExpected = []expected{
},
{
"/hello/John",
"/hello/John",
func(c *Control) {
c.Body("Hello from static path")
},
"Hello from static path",
0,
[]Param{},
},
{
"/:h/:n",
"/hell/jack",
func(c *Control) {
c.Body(c.Get(":n") + " from " + c.Get(":h"))
},
"jack from hell",
2,
[]Param{
{":h", "hell"},
{":n", "jack"},
},
},
{
"/products/table/orders/23",
"Product: table order# 23",
2,
[]Param{
{":name", "table"},
{":id", "23"},
},
},
{
"/products/book/orders/12",
"Product: book order# 12",
1,
[]Param{
{":id", "12"},
},
},
{
"/products/pen/orders/11",
"Product: pen order# 11",
2,
[]Param{
{":name", "pen"},
{":id", "11"},
},
},
{
"/products/pen/order/10",
"Product: pen # 10",
3,
[]Param{
{":name", "pen"},
{":order", "order"},
{":id", "10"},
},
},
{
"/product/pen/order/10",
"product pen order # 10",
4,
[]Param{
{":product", "product"},
{":name", "pen"},
{":order", "order"},
{":id", "10"},
},
},
}

func TestParserRegisterGet(t *testing.T) {
p := newParser()
for _, request := range setOfExpected {
for _, request := range setOfRegistered {
p.register(request.path, request.handle)
}
for _, exp := range setOfExpected {
Expand All @@ -72,7 +155,7 @@ func TestParserRegisterGet(t *testing.T) {
c := new(Control)
c.Set(params)
trw := new(testResponseWriter)
req, err := http.NewRequest("GET", exp.path, nil)
req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Error("Error creating new request")
}
Expand Down
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

/*
Package router 0.2.2 provides fast HTTP request router.
Package router 0.2.3 provides fast HTTP request router.
The router matches incoming requests by the request method and the path.
If a handle is registered for this path and method, the router delegates the
Expand Down

0 comments on commit bc719d4

Please sign in to comment.