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-01 17:05:54 +02:00
|
|
|
r.Add(MethodGET, "/folders/files/bolt.gif", func(c *Context) {})
|
|
|
|
h, _ := r.Find(MethodGET, "/folders/files/bolt.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-01 17:05:54 +02:00
|
|
|
r.Add(MethodGET, "/users/:id", func(c *Context) {})
|
|
|
|
h, c := r.Find(MethodGET, "/users/1")
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
2015-03-31 17:26:00 +02:00
|
|
|
p := c.Param("id")
|
|
|
|
if p != "1" {
|
|
|
|
t.Errorf("id should be equal to 1, found %s", p)
|
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-01 17:05:54 +02:00
|
|
|
r.Add(MethodGET, "/static/*", func(c *Context) {})
|
|
|
|
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-01 17:05:54 +02:00
|
|
|
r.Add(MethodGET, "/:a/:b/:c", func(c *Context) {})
|
|
|
|
h, c := r.Find(MethodGET, "/a/b/c")
|
2015-03-01 19:45:13 +02:00
|
|
|
if h == nil {
|
|
|
|
t.Fatal("handle not found")
|
|
|
|
}
|
|
|
|
p1 := c.P(0)
|
|
|
|
if p1 != "a" {
|
|
|
|
t.Errorf("p1 should be equal to a, found %s", p1)
|
|
|
|
}
|
|
|
|
p2 := c.P(1)
|
|
|
|
if p2 != "b" {
|
|
|
|
t.Errorf("p2 should be equal to b, found %s", p2)
|
|
|
|
}
|
|
|
|
p3 := c.P(2)
|
|
|
|
if p3 != "c" {
|
|
|
|
t.Errorf("p3 should be equal to c, found %s", p3)
|
|
|
|
}
|
|
|
|
}
|
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, "└── ", "├── ")
|
|
|
|
fmt.Printf("%s%s has=%d\n", p, n.prefix, n.has)
|
|
|
|
|
|
|
|
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
|
|
|
}
|