mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 02:51:28 +00:00
beego: fix the router rule for *
This commit is contained in:
parent
1f6e689e5d
commit
a5a6a30744
@ -79,19 +79,23 @@ func TestUrlFor(t *testing.T) {
|
|||||||
handler := NewControllerRegister()
|
handler := NewControllerRegister()
|
||||||
handler.Add("/api/list", &TestController{}, "*:List")
|
handler.Add("/api/list", &TestController{}, "*:List")
|
||||||
handler.Add("/person/:last/:first", &TestController{}, "*:Param")
|
handler.Add("/person/:last/:first", &TestController{}, "*:Param")
|
||||||
handler.AddAuto(&TestController{})
|
if a := handler.UrlFor("TestController.List"); a != "/api/list" {
|
||||||
if handler.UrlFor("TestController.List") != "/api/list" {
|
Info(a)
|
||||||
Info(handler.UrlFor("TestController.List"))
|
|
||||||
t.Errorf("TestController.List must equal to /api/list")
|
t.Errorf("TestController.List must equal to /api/list")
|
||||||
}
|
}
|
||||||
if handler.UrlFor("TestController.Param", ":last", "xie", ":first", "asta") != "/person/xie/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 " + handler.UrlFor("TestController.Param", ":last", "xie", ":first", "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" {
|
if a := handler.UrlFor("TestController.GetUrl"); a != "/test/geturl" {
|
||||||
t.Errorf("TestController.GetUrl must equal to /test/geturl")
|
t.Errorf("TestController.GetUrl must equal to /test/geturl, but get " + a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
tree.go
16
tree.go
@ -137,26 +137,36 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string,
|
|||||||
}
|
}
|
||||||
filterCards = append(filterCards, v)
|
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 {
|
} else {
|
||||||
t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards})
|
t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards})
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
seg := segments[0]
|
seg := segments[0]
|
||||||
iswild, params, regexpStr := splitSegment(seg)
|
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 iswild {
|
||||||
if t.wildcard == nil {
|
if t.wildcard == nil {
|
||||||
t.wildcard = NewTree()
|
t.wildcard = NewTree()
|
||||||
}
|
}
|
||||||
if regexpStr != "" {
|
if regexpStr != "" {
|
||||||
if reg == "" {
|
if reg == "" {
|
||||||
|
rr := ""
|
||||||
for _, w := range wildcards {
|
for _, w := range wildcards {
|
||||||
if w == "." || w == ":" {
|
if w == "." || w == ":" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
regexpStr = "([^/]+)/" + regexpStr
|
if w == ":splat" {
|
||||||
|
rr = rr + "(.+)/"
|
||||||
|
} else {
|
||||||
|
rr = rr + "([^/]+)/"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
regexpStr = rr + regexpStr
|
||||||
} else {
|
} else {
|
||||||
regexpStr = "/" + regexpStr
|
regexpStr = "/" + regexpStr
|
||||||
}
|
}
|
||||||
|
@ -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/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/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{"/*", "/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{"/*.*", "/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/*.*", "/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"}})
|
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) {
|
|||||||
if r.params != nil {
|
if r.params != nil {
|
||||||
for k, v := range r.params {
|
for k, v := range r.params {
|
||||||
if vv, ok := param[k]; !ok {
|
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 {
|
} 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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user