Merge pull request #4390 from jianzhiyao/fix-3928

fix-3928
This commit is contained in:
Ming Deng 2021-01-03 20:12:25 +08:00 committed by GitHub
commit 384364e2f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -583,7 +583,7 @@ func (p *ControllerRegister) getURL(t *Tree, url, controllerName, methodName str
for _, l := range t.leaves { for _, l := range t.leaves {
if c, ok := l.runObject.(*ControllerInfo); ok { if c, ok := l.runObject.(*ControllerInfo); ok {
if c.routerType == routerTypeBeego && if c.routerType == routerTypeBeego &&
strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), controllerName) { strings.HasSuffix(path.Join(c.controllerType.PkgPath(), c.controllerType.Name()), `/`+controllerName) {
find := false find := false
if HTTPMETHOD[strings.ToUpper(methodName)] { if HTTPMETHOD[strings.ToUpper(methodName)] {
if len(c.methods) == 0 { if len(c.methods) == 0 {

View File

@ -26,6 +26,14 @@ import (
"github.com/beego/beego/v2/server/web/context" "github.com/beego/beego/v2/server/web/context"
) )
type PrefixTestController struct {
Controller
}
func (ptc *PrefixTestController) PrefixList() {
ptc.Ctx.Output.Body([]byte("i am list in prefix test"))
}
type TestController struct { type TestController struct {
Controller Controller
} }
@ -87,6 +95,20 @@ func (jc *JSONController) Get() {
jc.Ctx.Output.Body([]byte("ok")) jc.Ctx.Output.Body([]byte("ok"))
} }
func TestPrefixUrlFor(t *testing.T){
handler := NewControllerRegister()
handler.Add("/my/prefix/list", &PrefixTestController{}, "get:PrefixList")
if a := handler.URLFor(`PrefixTestController.PrefixList`); a != `/my/prefix/list` {
logs.Info(a)
t.Errorf("PrefixTestController.PrefixList must equal to /my/prefix/list")
}
if a := handler.URLFor(`TestController.PrefixList`); a != `` {
logs.Info(a)
t.Errorf("TestController.PrefixList must equal to empty string")
}
}
func TestUrlFor(t *testing.T) { func TestUrlFor(t *testing.T) {
handler := NewControllerRegister() handler := NewControllerRegister()
handler.Add("/api/list", &TestController{}, SetRouterMethods(&TestController{}, "*:List")) handler.Add("/api/list", &TestController{}, SetRouterMethods(&TestController{}, "*:List"))