From a5a6a307444ab3fdc53cc9a1625847fe7fd39661 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 23 Jun 2014 15:28:53 +0800 Subject: [PATCH] beego: fix the router rule for * --- router_test.go | 22 +++++++++++++--------- tree.go | 16 +++++++++++++--- tree_test.go | 7 +++++-- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/router_test.go b/router_test.go index 83dc5dca..b80000f4 100644 --- a/router_test.go +++ b/router_test.go @@ -79,19 +79,23 @@ func TestUrlFor(t *testing.T) { handler := NewControllerRegister() handler.Add("/api/list", &TestController{}, "*:List") handler.Add("/person/:last/:first", &TestController{}, "*:Param") - handler.AddAuto(&TestController{}) - if handler.UrlFor("TestController.List") != "/api/list" { - Info(handler.UrlFor("TestController.List")) + if a := handler.UrlFor("TestController.List"); a != "/api/list" { + Info(a) t.Errorf("TestController.List must equal to /api/list") } - if handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta") != "/person/xie/asta" { - t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta")) + if a := handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" { + t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a) } - if handler.UrlFor("TestController.Myext") != "/test/myext" { - t.Errorf("TestController.Myext must equal to /test/myext") +} + +func TestUrlFor3(t *testing.T) { + handler := NewControllerRegister() + handler.AddAuto(&TestController{}) + if a := handler.UrlFor("TestController.Myext"); a != "/test/myext" { + t.Errorf("TestController.Myext must equal to /test/myext, but get " + a) } - if handler.UrlFor("TestController.GetUrl") != "/test/geturl" { - t.Errorf("TestController.GetUrl must equal to /test/geturl") + if a := handler.UrlFor("TestController.GetUrl"); a != "/test/geturl" { + t.Errorf("TestController.GetUrl must equal to /test/geturl, but get " + a) } } diff --git a/tree.go b/tree.go index 991a120e..93d4ab02 100644 --- a/tree.go +++ b/tree.go @@ -137,26 +137,36 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, } filterCards = append(filterCards, v) } - t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")}) + t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: filterCards, regexps: regexp.MustCompile("^" + reg + "$")}) } else { t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards}) } - } else { seg := segments[0] iswild, params, regexpStr := splitSegment(seg) + //for the router /login/*/access match /login/2009/11/access + if !iswild && utils.InSlice(":splat", wildcards) { + iswild = true + regexpStr = seg + } if iswild { if t.wildcard == nil { t.wildcard = NewTree() } if regexpStr != "" { if reg == "" { + rr := "" for _, w := range wildcards { if w == "." || w == ":" { continue } - regexpStr = "([^/]+)/" + regexpStr + if w == ":splat" { + rr = rr + "(.+)/" + } else { + rr = rr + "([^/]+)/" + } } + regexpStr = rr + regexpStr } else { regexpStr = "/" + regexpStr } diff --git a/tree_test.go b/tree_test.go index f1c37469..57f0a9fd 100644 --- a/tree_test.go +++ b/tree_test.go @@ -19,6 +19,9 @@ func init() { routers = append(routers, testinfo{"/customer/login", "/customer/login.json", map[string]string{":ext": "json"}}) routers = append(routers, testinfo{"/*", "/customer/123", map[string]string{":splat": "customer/123"}}) routers = append(routers, testinfo{"/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}}) + routers = append(routers, testinfo{"/aa/*/bb", "/aa/2009/bb", map[string]string{":splat": "2009"}}) + routers = append(routers, testinfo{"/cc/*/dd", "/cc/2009/11/dd", map[string]string{":splat": "2009/11"}}) + routers = append(routers, testinfo{"/ee/:year/*/ff", "/ee/2009/11/ff", map[string]string{":year": "2009", ":splat": "11"}}) routers = append(routers, testinfo{"/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}}) routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) @@ -47,9 +50,9 @@ func TestTreeRouters(t *testing.T) { if r.params != nil { for k, v := range r.params { if vv, ok := param[k]; !ok { - t.Fatal(r.url + r.requesturl + " get param empty:" + k) + 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) + t.Fatal(r.url + " " + r.requesturl + " should be:" + v + " get param:" + vv) } } }