mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:10:55 +00:00
fix #153
已经支持了任意定义变量的路由形式,具体的使用请参考: func TestManyRoute(t *testing.T) { r, _ := http.NewRequest("GET", "/beego32-12.html", nil) w := httptest.NewRecorder() handler := NewControllerRegistor() handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{}) handler.ServeHTTP(w, r) id := r.URL.Query().Get(":id") page := r.URL.Query().Get(":page") if id != "32" { t.Errorf("url param set to [%s]; want [%s]", id, "32") } if page != "12" { t.Errorf("url param set to [%s]; want [%s]", page, "12") } }
This commit is contained in:
parent
3745bb7279
commit
9d84969bf6
41
router.go
41
router.go
@ -94,6 +94,47 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
|
|||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//url like someprefix:id(xxx).html
|
||||||
|
if strings.Contains(part, ":") && strings.Contains(part, "(") && strings.Contains(part, ")") {
|
||||||
|
var out []rune
|
||||||
|
var start bool
|
||||||
|
var startexp bool
|
||||||
|
var param []rune
|
||||||
|
var expt []rune
|
||||||
|
for _, v := range part {
|
||||||
|
if start {
|
||||||
|
if v != '(' {
|
||||||
|
param = append(param, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if startexp {
|
||||||
|
if v != ')' {
|
||||||
|
expt = append(expt, v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if v == ':' {
|
||||||
|
param = make([]rune, 0)
|
||||||
|
param = append(param, ':')
|
||||||
|
start = true
|
||||||
|
} else if v == '(' {
|
||||||
|
startexp = true
|
||||||
|
start = false
|
||||||
|
params[j] = string(param)
|
||||||
|
j++
|
||||||
|
expt = make([]rune, 0)
|
||||||
|
expt = append(expt, '(')
|
||||||
|
} else if v == ')' {
|
||||||
|
startexp = false
|
||||||
|
expt = append(expt, ')')
|
||||||
|
out = append(out, expt...)
|
||||||
|
} else {
|
||||||
|
out = append(out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parts[i] = string(out)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
reflectVal := reflect.ValueOf(c)
|
reflectVal := reflect.ValueOf(c)
|
||||||
t := reflect.Indirect(reflectVal).Type()
|
t := reflect.Indirect(reflectVal).Type()
|
||||||
|
@ -69,6 +69,26 @@ func TestRouteOk(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManyRoute(t *testing.T) {
|
||||||
|
|
||||||
|
r, _ := http.NewRequest("GET", "/beego32-12.html", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
handler := NewControllerRegistor()
|
||||||
|
handler.Add("/beego:id([0-9]+)-:page([0-9]+).html", &TestController{})
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
id := r.URL.Query().Get(":id")
|
||||||
|
page := r.URL.Query().Get(":page")
|
||||||
|
|
||||||
|
if id != "32" {
|
||||||
|
t.Errorf("url param set to [%s]; want [%s]", id, "32")
|
||||||
|
}
|
||||||
|
if page != "12" {
|
||||||
|
t.Errorf("url param set to [%s]; want [%s]", page, "12")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNotFound(t *testing.T) {
|
func TestNotFound(t *testing.T) {
|
||||||
r, _ := http.NewRequest("GET", "/", nil)
|
r, _ := http.NewRequest("GET", "/", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
Loading…
Reference in New Issue
Block a user