Esempio n. 1
0
func TestAddTree4(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/create", "astaxie")
	tr.AddRouter("/shop/:sd/:account", "astaxie")
	t4 := NewTree()
	t4.AddTree("/:info:int/:num/:id", tr)
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":info") != "12" || ctx.Input.Param(":num") != "123" ||
		ctx.Input.Param(":id") != "456" || ctx.Input.Param(":sd") != "123" ||
		ctx.Input.Param(":account") != "account" {
		t.Fatal("get :info :num :id :sd :account param error")
	}
	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 ")
	}
}
Esempio n. 2
0
// NewControllerRegister returns a new ControllerRegister.
func NewControllerRegister() *ControllerRegister {
	cr := &ControllerRegister{
		routers: make(map[string]*Tree),
	}
	cr.pool.New = func() interface{} {
		return beecontext.NewContext()
	}
	return cr
}
Esempio n. 3
0
// Test for issue #1595
func TestAddTree5(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/v1/shop/:id", "shopdetail")
	tr.AddRouter("/v1/shop/", "shophome")
	ctx := context.NewContext()
	obj := tr.Match("/v1/shop/", ctx)
	if obj == nil || obj.(string) != "shophome" {
		t.Fatal("url /v1/shop/ need match router /v1/shop/ ")
	}
}
Esempio n. 4
0
func TestAddTree(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/shop/:id/account", "astaxie")
	tr.AddRouter("/shop/:sd/ttt_:id(.+)_:page(.+).html", "astaxie")
	t1 := NewTree()
	t1.AddTree("/v1/zl", tr)
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":id") != "123" {
		t.Fatal("get :id param error")
	}
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":sd") != "123" || ctx.Input.Param(":id") != "1" || ctx.Input.Param(":page") != "12" {
		t.Fatal("get :sd :id :page param error")
	}

	t2 := NewTree()
	t2.AddTree("/v1/:shopid", tr)
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":id") != "123" || ctx.Input.Param(":shopid") != "zl" {
		t.Fatal("get :id :shopid param error")
	}
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get :shopid param error")
	}
	if ctx.Input.Param(":sd") != "123" || ctx.Input.Param(":id") != "1" || ctx.Input.Param(":page") != "12" || ctx.Input.Param(":shopid") != "zl" {
		t.Fatal("get :sd :id :page :shopid param error")
	}
}
Esempio n. 5
0
func TestStaticPath(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/topic/:id", "wildcard")
	tr.AddRouter("/topic", "static")
	ctx := context.NewContext()
	obj := tr.Match("/topic", ctx)
	if obj == nil || obj.(string) != "static" {
		t.Fatal("/topic is  a static route")
	}
	obj = tr.Match("/topic/1", ctx)
	if obj == nil || obj.(string) != "wildcard" {
		t.Fatal("/topic/1 is a wildcard route")
	}
}
Esempio n. 6
0
func TestAddTree2(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/shop/:id/account", "astaxie")
	tr.AddRouter("/shop/:sd/ttt_:id(.+)_:page(.+).html", "astaxie")
	t3 := NewTree()
	t3.AddTree("/:version(v1|v2)/:prefix", tr)
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":id") != "123" || ctx.Input.Param(":prefix") != "zl" || ctx.Input.Param(":version") != "v1" {
		t.Fatal("get :id :prefix :version param error")
	}
}
Esempio n. 7
0
func TestTreeRouters(t *testing.T) {
	for _, r := range routers {
		tr := NewTree()
		tr.AddRouter(r.url, "astaxie")
		ctx := context.NewContext()
		obj := tr.Match(r.requesturl, ctx)
		if obj == nil || obj.(string) != "astaxie" {
			t.Fatal(r.url+" can't get obj, Expect ", r.requesturl)
		}
		if r.params != nil {
			for k, v := range r.params {
				if vv := ctx.Input.Param(k); vv != v {
					t.Fatal("The Rule: " + r.url + "\nThe RequestURL:" + r.requesturl + "\nThe Key is " + k + ", The Value should be: " + v + ", but get: " + vv)
				} else if vv == "" && v != "" {
					t.Fatal(r.url + "    " + r.requesturl + " get param empty:" + k)
				}
			}
		}
	}
}
Esempio n. 8
0
func TestAddTree3(t *testing.T) {
	tr := NewTree()
	tr.AddRouter("/create", "astaxie")
	tr.AddRouter("/shop/:sd/account", "astaxie")
	t3 := NewTree()
	t3.AddTree("/table/:num", tr)
	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 ctx.Input.ParamsLen() == 0 {
		t.Fatal("get param error")
	}
	if ctx.Input.Param(":num") != "123" || ctx.Input.Param(":sd") != "123" {
		t.Fatal("get :num :sd param error")
	}
	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 ")
	}
}