2015-03-27 23:21:30 +02:00
|
|
|
package echo
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterStatic(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-02 23:41:36 +02:00
|
|
|
r.Add(MethodGET, "/folders/files/echo.gif", func(c *Context) {}, nil)
|
2015-04-02 14:02:52 +02:00
|
|
|
h, _, _ := r.Find(MethodGET, "/folders/files/echo.gif")
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterParam(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-02 23:41:36 +02:00
|
|
|
r.Add(MethodGET, "/users/:id", func(c *Context) {}, nil)
|
2015-04-02 14:02:52 +02:00
|
|
|
h, c, _ := r.Find(MethodGET, "/users/1")
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param id should be 1")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterTwoParam(t *testing.T) {
|
|
|
|
r := New().Router
|
|
|
|
r.Add(MethodGET, "/users/:uid/files/:fid", func(c *Context) {}, nil)
|
|
|
|
h, c, _ := r.Find(MethodGET, "/users/1/files/1")
|
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param uid should be 1")
|
|
|
|
}
|
|
|
|
if c.P(1) != "1" {
|
|
|
|
t.Error("param fid should be 1")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterCatchAll(t *testing.T) {
|
2015-03-07 07:55:51 +02:00
|
|
|
r := New().Router
|
2015-04-02 23:41:36 +02:00
|
|
|
r.Add(MethodGET, "/static/*", func(c *Context) {}, nil)
|
2015-04-02 14:02:52 +02:00
|
|
|
h, _, _ := r.Find(MethodGET, "/static/*")
|
2015-03-07 07:55:51 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 08:51:32 +02:00
|
|
|
func TestRouterMicroParam(t *testing.T) {
|
2015-03-01 19:45:13 +02:00
|
|
|
r := New().Router
|
2015-04-02 23:41:36 +02:00
|
|
|
r.Add(MethodGET, "/:a/:b/:c", func(c *Context) {}, nil)
|
2015-04-04 19:44:48 +02:00
|
|
|
h, c, _ := r.Find(MethodGET, "/1/2/3")
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
if c.P(0) != "1" {
|
|
|
|
t.Error("param a should be 1")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
if c.P(1) != "2" {
|
|
|
|
t.Error("param b should be 2")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-04 19:44:48 +02:00
|
|
|
if c.P(2) != "3" {
|
|
|
|
t.Error("param c should be 3")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-31 17:26:00 +02:00
|
|
|
|
2015-04-01 17:05:54 +02:00
|
|
|
func (n *node) printTree(pfx string, tail bool) {
|
|
|
|
p := prefix(tail, pfx, "└── ", "├── ")
|
2015-04-05 23:21:03 +02:00
|
|
|
fmt.Printf("%s%s has=%d, h=%v, echo=%v\n", p, n.prefix, n.has, n.handler, n.echo)
|
2015-04-01 17:05:54 +02:00
|
|
|
|
|
|
|
nodes := n.edges
|
|
|
|
l := len(nodes)
|
|
|
|
p = prefix(tail, pfx, " ", "│ ")
|
|
|
|
for i := 0; i < l-1; i++ {
|
|
|
|
nodes[i].printTree(p, false)
|
|
|
|
}
|
|
|
|
if l > 0 {
|
|
|
|
nodes[l-1].printTree(p, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prefix(tail bool, p, on, off string) string {
|
|
|
|
if tail {
|
|
|
|
return fmt.Sprintf("%s%s", p, on)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s", p, off)
|
2015-03-31 17:26:00 +02:00
|
|
|
}
|