beego: fix the router rule for *
Showing
3 changed files
with
30 additions
and
13 deletions
| ... | @@ -79,19 +79,23 @@ func TestUrlFor(t *testing.T) { | ... | @@ -79,19 +79,23 @@ func TestUrlFor(t *testing.T) { |
| 79 | handler := NewControllerRegister() | 79 | handler := NewControllerRegister() |
| 80 | handler.Add("/api/list", &TestController{}, "*:List") | 80 | handler.Add("/api/list", &TestController{}, "*:List") |
| 81 | handler.Add("/person/:last/:first", &TestController{}, "*:Param") | 81 | handler.Add("/person/:last/:first", &TestController{}, "*:Param") |
| 82 | handler.AddAuto(&TestController{}) | 82 | if a := handler.UrlFor("TestController.List"); a != "/api/list" { |
| 83 | if handler.UrlFor("TestController.List") != "/api/list" { | 83 | Info(a) |
| 84 | Info(handler.UrlFor("TestController.List")) | ||
| 85 | t.Errorf("TestController.List must equal to /api/list") | 84 | t.Errorf("TestController.List must equal to /api/list") |
| 86 | } | 85 | } |
| 87 | if handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta") != "/person/xie/asta" { | 86 | if a := handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta"); a != "/person/xie/asta" { |
| 88 | t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta")) | 87 | t.Errorf("TestController.Param must equal to /person/xie/asta, but get " + a) |
| 89 | } | 88 | } |
| 90 | if handler.UrlFor("TestController.Myext") != "/test/myext" { | 89 | } |
| 91 | t.Errorf("TestController.Myext must equal to /test/myext") | 90 | |
| 91 | func TestUrlFor3(t *testing.T) { | ||
| 92 | handler := NewControllerRegister() | ||
| 93 | handler.AddAuto(&TestController{}) | ||
| 94 | if a := handler.UrlFor("TestController.Myext"); a != "/test/myext" { | ||
| 95 | t.Errorf("TestController.Myext must equal to /test/myext, but get " + a) | ||
| 92 | } | 96 | } |
| 93 | if handler.UrlFor("TestController.GetUrl") != "/test/geturl" { | 97 | if a := handler.UrlFor("TestController.GetUrl"); a != "/test/geturl" { |
| 94 | t.Errorf("TestController.GetUrl must equal to /test/geturl") | 98 | t.Errorf("TestController.GetUrl must equal to /test/geturl, but get " + a) |
| 95 | } | 99 | } |
| 96 | } | 100 | } |
| 97 | 101 | ... | ... |
| ... | @@ -137,26 +137,36 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, | ... | @@ -137,26 +137,36 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, |
| 137 | } | 137 | } |
| 138 | filterCards = append(filterCards, v) | 138 | filterCards = append(filterCards, v) |
| 139 | } | 139 | } |
| 140 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")}) | 140 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: filterCards, regexps: regexp.MustCompile("^" + reg + "$")}) |
| 141 | } else { | 141 | } else { |
| 142 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards}) | 142 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards}) |
| 143 | } | 143 | } |
| 144 | |||
| 145 | } else { | 144 | } else { |
| 146 | seg := segments[0] | 145 | seg := segments[0] |
| 147 | iswild, params, regexpStr := splitSegment(seg) | 146 | iswild, params, regexpStr := splitSegment(seg) |
| 147 | //for the router /login/*/access match /login/2009/11/access | ||
| 148 | if !iswild && utils.InSlice(":splat", wildcards) { | ||
| 149 | iswild = true | ||
| 150 | regexpStr = seg | ||
| 151 | } | ||
| 148 | if iswild { | 152 | if iswild { |
| 149 | if t.wildcard == nil { | 153 | if t.wildcard == nil { |
| 150 | t.wildcard = NewTree() | 154 | t.wildcard = NewTree() |
| 151 | } | 155 | } |
| 152 | if regexpStr != "" { | 156 | if regexpStr != "" { |
| 153 | if reg == "" { | 157 | if reg == "" { |
| 158 | rr := "" | ||
| 154 | for _, w := range wildcards { | 159 | for _, w := range wildcards { |
| 155 | if w == "." || w == ":" { | 160 | if w == "." || w == ":" { |
| 156 | continue | 161 | continue |
| 157 | } | 162 | } |
| 158 | regexpStr = "([^/]+)/" + regexpStr | 163 | if w == ":splat" { |
| 164 | rr = rr + "(.+)/" | ||
| 165 | } else { | ||
| 166 | rr = rr + "([^/]+)/" | ||
| 167 | } | ||
| 159 | } | 168 | } |
| 169 | regexpStr = rr + regexpStr | ||
| 160 | } else { | 170 | } else { |
| 161 | regexpStr = "/" + regexpStr | 171 | regexpStr = "/" + regexpStr |
| 162 | } | 172 | } | ... | ... |
| ... | @@ -19,6 +19,9 @@ func init() { | ... | @@ -19,6 +19,9 @@ func init() { |
| 19 | routers = append(routers, testinfo{"/customer/login", "/customer/login.json", map[string]string{":ext": "json"}}) | 19 | routers = append(routers, testinfo{"/customer/login", "/customer/login.json", map[string]string{":ext": "json"}}) |
| 20 | routers = append(routers, testinfo{"/*", "/customer/123", map[string]string{":splat": "customer/123"}}) | 20 | routers = append(routers, testinfo{"/*", "/customer/123", map[string]string{":splat": "customer/123"}}) |
| 21 | routers = append(routers, testinfo{"/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}}) | 21 | routers = append(routers, testinfo{"/*", "/customer/2009/12/11", map[string]string{":splat": "customer/2009/12/11"}}) |
| 22 | routers = append(routers, testinfo{"/aa/*/bb", "/aa/2009/bb", map[string]string{":splat": "2009"}}) | ||
| 23 | routers = append(routers, testinfo{"/cc/*/dd", "/cc/2009/11/dd", map[string]string{":splat": "2009/11"}}) | ||
| 24 | routers = append(routers, testinfo{"/ee/:year/*/ff", "/ee/2009/11/ff", map[string]string{":year": "2009", ":splat": "11"}}) | ||
| 22 | routers = append(routers, testinfo{"/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}}) | 25 | routers = append(routers, testinfo{"/*.*", "/nice/api.json", map[string]string{":path": "nice/api", ":ext": "json"}}) |
| 23 | routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) | 26 | routers = append(routers, testinfo{"/:name/*.*", "/nice/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) |
| 24 | routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) | 27 | routers = append(routers, testinfo{"/:name/test/*.*", "/nice/test/api.json", map[string]string{":name": "nice", ":path": "api", ":ext": "json"}}) |
| ... | @@ -47,7 +50,7 @@ func TestTreeRouters(t *testing.T) { | ... | @@ -47,7 +50,7 @@ func TestTreeRouters(t *testing.T) { |
| 47 | if r.params != nil { | 50 | if r.params != nil { |
| 48 | for k, v := range r.params { | 51 | for k, v := range r.params { |
| 49 | if vv, ok := param[k]; !ok { | 52 | if vv, ok := param[k]; !ok { |
| 50 | t.Fatal(r.url + r.requesturl + " get param empty:" + k) | 53 | t.Fatal(r.url + " " + r.requesturl + " get param empty:" + k) |
| 51 | } else if vv != v { | 54 | } else if vv != v { |
| 52 | t.Fatal(r.url + " " + r.requesturl + " should be:" + v + " get param:" + vv) | 55 | t.Fatal(r.url + " " + r.requesturl + " should be:" + v + " get param:" + vv) |
| 53 | } | 56 | } | ... | ... |
-
Please register or sign in to post a comment