1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 06:50:50 +00:00

beego: fix the router rule for *

This commit is contained in:
astaxie 2014-06-23 15:28:53 +08:00
parent 1f6e689e5d
commit a5a6a30744
3 changed files with 31 additions and 14 deletions

View File

@ -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)
}
}

16
tree.go
View File

@ -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
}

View File

@ -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)
}
}
}