mirror of
https://github.com/astaxie/beego.git
synced 2025-08-01 00:25:30 +00:00
reuse map in tree.Match
This commit is contained in:
72
tree_test.go
72
tree_test.go
@@ -14,7 +14,11 @@
|
||||
|
||||
package beego
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/astaxie/beego/context"
|
||||
)
|
||||
|
||||
type testinfo struct {
|
||||
url string
|
||||
@@ -27,11 +31,11 @@ var routers []testinfo
|
||||
func init() {
|
||||
routers = make([]testinfo, 0)
|
||||
routers = append(routers, testinfo{"/topic/?:auth:int", "/topic", nil})
|
||||
routers = append(routers, testinfo{"/topic/?:auth:int", "/topic/123", map[string]string{":auth":"123"}})
|
||||
routers = append(routers, testinfo{"/topic/?:auth:int", "/topic/123", map[string]string{":auth": "123"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth", "/topic/1", map[string]string{":id": "1"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth", "/topic/1/2", map[string]string{":id": "1",":auth":"2"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth", "/topic/1/2", map[string]string{":id": "1", ":auth": "2"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth:int", "/topic/1", map[string]string{":id": "1"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth:int", "/topic/1/123", map[string]string{":id": "1",":auth":"123"}})
|
||||
routers = append(routers, testinfo{"/topic/:id/?:auth:int", "/topic/1/123", map[string]string{":id": "1", ":auth": "123"}})
|
||||
routers = append(routers, testinfo{"/:id", "/123", map[string]string{":id": "123"}})
|
||||
routers = append(routers, testinfo{"/hello/?:id", "/hello", map[string]string{":id": ""}})
|
||||
routers = append(routers, testinfo{"/", "/", nil})
|
||||
@@ -69,13 +73,14 @@ func TestTreeRouters(t *testing.T) {
|
||||
for _, r := range routers {
|
||||
tr := NewTree()
|
||||
tr.AddRouter(r.url, "astaxie")
|
||||
obj, param := tr.Match(r.requesturl)
|
||||
ctx := context.NewContext()
|
||||
obj := tr.Match(r.requesturl, ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal(r.url + " can't get obj ")
|
||||
}
|
||||
if r.params != nil {
|
||||
for k, v := range r.params {
|
||||
if vv, ok := param[k]; !ok {
|
||||
if vv, ok := ctx.Input.Params[k]; !ok {
|
||||
t.Fatal(r.url + " " + r.requesturl + " get param empty:" + k)
|
||||
} else if vv != v {
|
||||
t.Fatal(r.url + " " + r.requesturl + " should be:" + v + " get param:" + vv)
|
||||
@@ -91,47 +96,51 @@ func TestAddTree(t *testing.T) {
|
||||
tr.AddRouter("/shop/:sd/ttt_:id(.+)_:page(.+).html", "astaxie")
|
||||
t1 := NewTree()
|
||||
t1.AddTree("/v1/zl", tr)
|
||||
obj, param := t1.Match("/v1/zl/shop/123/account")
|
||||
ctx := context.NewContext()
|
||||
obj := t1.Match("/v1/zl/shop/123/account", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/v1/zl/shop/:id/account can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":id"] != "123" {
|
||||
if ctx.Input.Params[":id"] != "123" {
|
||||
t.Fatal("get :id param error")
|
||||
}
|
||||
obj, param = t1.Match("/v1/zl/shop/123/ttt_1_12.html")
|
||||
ctx.Input.Reset(ctx)
|
||||
obj = t1.Match("/v1/zl/shop/123/ttt_1_12.html", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/v1/zl//shop/:sd/ttt_:id(.+)_:page(.+).html can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":sd"] != "123" || param[":id"] != "1" || param[":page"] != "12" {
|
||||
if ctx.Input.Params[":sd"] != "123" || ctx.Input.Params[":id"] != "1" || ctx.Input.Params[":page"] != "12" {
|
||||
t.Fatal("get :sd :id :page param error")
|
||||
}
|
||||
|
||||
t2 := NewTree()
|
||||
t2.AddTree("/v1/:shopid", tr)
|
||||
obj, param = t2.Match("/v1/zl/shop/123/account")
|
||||
ctx.Input.Reset(ctx)
|
||||
obj = t2.Match("/v1/zl/shop/123/account", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/v1/:shopid/shop/:id/account can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":id"] != "123" || param[":shopid"] != "zl" {
|
||||
if ctx.Input.Params[":id"] != "123" || ctx.Input.Params[":shopid"] != "zl" {
|
||||
t.Fatal("get :id :shopid param error")
|
||||
}
|
||||
obj, param = t2.Match("/v1/zl/shop/123/ttt_1_12.html")
|
||||
ctx.Input.Reset(ctx)
|
||||
obj = t2.Match("/v1/zl/shop/123/ttt_1_12.html", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/v1/:shopid/shop/:sd/ttt_:id(.+)_:page(.+).html can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get :shopid param error")
|
||||
}
|
||||
if param[":sd"] != "123" || param[":id"] != "1" || param[":page"] != "12" || param[":shopid"] != "zl" {
|
||||
if ctx.Input.Params[":sd"] != "123" || ctx.Input.Params[":id"] != "1" || ctx.Input.Params[":page"] != "12" || ctx.Input.Params[":shopid"] != "zl" {
|
||||
t.Fatal("get :sd :id :page :shopid param error")
|
||||
}
|
||||
}
|
||||
@@ -142,14 +151,15 @@ func TestAddTree2(t *testing.T) {
|
||||
tr.AddRouter("/shop/:sd/ttt_:id(.+)_:page(.+).html", "astaxie")
|
||||
t3 := NewTree()
|
||||
t3.AddTree("/:version(v1|v2)/:prefix", tr)
|
||||
obj, param := t3.Match("/v1/zl/shop/123/account")
|
||||
ctx := context.NewContext()
|
||||
obj := t3.Match("/v1/zl/shop/123/account", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/:version(v1|v2)/:prefix/shop/:id/account can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":id"] != "123" || param[":prefix"] != "zl" || param[":version"] != "v1" {
|
||||
if ctx.Input.Params[":id"] != "123" || ctx.Input.Params[":prefix"] != "zl" || ctx.Input.Params[":version"] != "v1" {
|
||||
t.Fatal("get :id :prefix :version param error")
|
||||
}
|
||||
}
|
||||
@@ -160,17 +170,19 @@ func TestAddTree3(t *testing.T) {
|
||||
tr.AddRouter("/shop/:sd/account", "astaxie")
|
||||
t3 := NewTree()
|
||||
t3.AddTree("/table/:num", tr)
|
||||
obj, param := t3.Match("/table/123/shop/123/account")
|
||||
ctx := context.NewContext()
|
||||
obj := t3.Match("/table/123/shop/123/account", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/table/:num/shop/:sd/account can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":num"] != "123" || param[":sd"] != "123" {
|
||||
if ctx.Input.Params[":num"] != "123" || ctx.Input.Params[":sd"] != "123" {
|
||||
t.Fatal("get :num :sd param error")
|
||||
}
|
||||
obj, param = t3.Match("/table/123/create")
|
||||
ctx.Input.Reset(ctx)
|
||||
obj = t3.Match("/table/123/create", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/table/:num/create can't get obj ")
|
||||
}
|
||||
@@ -182,17 +194,19 @@ func TestAddTree4(t *testing.T) {
|
||||
tr.AddRouter("/shop/:sd/:account", "astaxie")
|
||||
t4 := NewTree()
|
||||
t4.AddTree("/:info:int/:num/:id", tr)
|
||||
obj, param := t4.Match("/12/123/456/shop/123/account")
|
||||
ctx := context.NewContext()
|
||||
obj := t4.Match("/12/123/456/shop/123/account", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/:info:int/:num/:id/shop/:sd/:account can't get obj ")
|
||||
}
|
||||
if param == nil {
|
||||
if len(ctx.Input.Params) == 0 {
|
||||
t.Fatal("get param error")
|
||||
}
|
||||
if param[":info"] != "12" || param[":num"] != "123" || param[":id"] != "456" || param[":sd"] != "123" || param[":account"] != "account" {
|
||||
if ctx.Input.Params[":info"] != "12" || ctx.Input.Params[":num"] != "123" || ctx.Input.Params[":id"] != "456" || ctx.Input.Params[":sd"] != "123" || ctx.Input.Params[":account"] != "account" {
|
||||
t.Fatal("get :info :num :id :sd :account param error")
|
||||
}
|
||||
obj, param = t4.Match("/12/123/456/create")
|
||||
ctx.Input.Reset(ctx)
|
||||
obj = t4.Match("/12/123/456/create", ctx)
|
||||
if obj == nil || obj.(string) != "astaxie" {
|
||||
t.Fatal("/:info:int/:num/:id/create can't get obj ")
|
||||
}
|
||||
|
Reference in New Issue
Block a user