mirror of
https://github.com/labstack/echo.git
synced 2024-11-24 08:22:21 +02:00
47fb44deb5
Signed-off-by: Vishal Rana <vr@labstack.com>
122 lines
2.5 KiB
Go
122 lines
2.5 KiB
Go
package echo
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
type route struct {
|
|
method string
|
|
path string
|
|
}
|
|
|
|
var api = []route{
|
|
{"GET", "/authorizations"},
|
|
}
|
|
|
|
func TestRouterStatic(t *testing.T) {
|
|
r := New().Router
|
|
r.Add(MethodGET, "/folders/files/echo.gif", func(c *Context) {}, nil)
|
|
h, _, _ := r.Find(MethodGET, "/folders/files/echo.gif")
|
|
if h == nil {
|
|
t.Fatal("handle not found")
|
|
}
|
|
}
|
|
|
|
func TestRouterParam(t *testing.T) {
|
|
r := New().Router
|
|
r.Add(MethodGET, "/users/:id", func(c *Context) {}, nil)
|
|
h, c, _ := r.Find(MethodGET, "/users/1")
|
|
if h == nil {
|
|
t.Fatal("handle not found")
|
|
}
|
|
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")
|
|
}
|
|
}
|
|
|
|
func TestRouterCatchAll(t *testing.T) {
|
|
r := New().Router
|
|
r.Add(MethodGET, "/static/*", func(c *Context) {}, nil)
|
|
h, _, _ := r.Find(MethodGET, "/static/*")
|
|
if h == nil {
|
|
t.Fatal("handle not found")
|
|
}
|
|
}
|
|
|
|
func TestRouterMicroParam(t *testing.T) {
|
|
r := New().Router
|
|
r.Add(MethodGET, "/:a/:b/:c", func(c *Context) {}, nil)
|
|
h, c, _ := r.Find(MethodGET, "/1/2/3")
|
|
if h == nil {
|
|
t.Fatal("handle not found")
|
|
}
|
|
if c.P(0) != "1" {
|
|
t.Error("param a should be 1")
|
|
}
|
|
if c.P(1) != "2" {
|
|
t.Error("param b should be 2")
|
|
}
|
|
if c.P(2) != "3" {
|
|
t.Error("param c should be 3")
|
|
}
|
|
}
|
|
|
|
func TestRouterAPI(t *testing.T) {
|
|
// r := New().Router
|
|
}
|
|
|
|
func TestRouterServeHTTP(t *testing.T) {
|
|
r := New().Router
|
|
r.Add(MethodGET, "/users", func(c *Context) {}, nil)
|
|
|
|
// OK
|
|
req, _ := http.NewRequest(MethodGET, "/users", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
// NotFound handler
|
|
req, _ = http.NewRequest(MethodGET, "/files", nil)
|
|
w = httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
}
|
|
|
|
func (n *node) printTree(pfx string, tail bool) {
|
|
p := prefix(tail, pfx, "└── ", "├── ")
|
|
fmt.Printf("%s%s has=%d, h=%v, echo=%v\n", p, n.prefix, n.has, n.handler, n.echo)
|
|
|
|
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)
|
|
}
|