From e597b05c938f0b6348b116426589c636e0966c97 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sat, 26 Dec 2020 18:38:52 +0800 Subject: [PATCH 1/2] fix-3928 --- server/web/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/web/router.go b/server/web/router.go index 5a663386..866ea745 100644 --- a/server/web/router.go +++ b/server/web/router.go @@ -548,7 +548,7 @@ func (p *ControllerRegister) getURL(t *Tree, url, controllerName, methodName str for _, l := range t.leaves { if c, ok := l.runObject.(*ControllerInfo); ok { 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 if HTTPMETHOD[strings.ToUpper(methodName)] { if len(c.methods) == 0 { From 6e4398f7ec0736cdc4aea489ae35e85c5898b6b0 Mon Sep 17 00:00:00 2001 From: Anker Jam Date: Sat, 26 Dec 2020 22:45:56 +0800 Subject: [PATCH 2/2] add UT for issue 3928 --- server/web/router_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/web/router_test.go b/server/web/router_test.go index 87997322..521605e5 100644 --- a/server/web/router_test.go +++ b/server/web/router_test.go @@ -26,6 +26,14 @@ import ( "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 { Controller } @@ -87,6 +95,20 @@ func (jc *JSONController) Get() { 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) { handler := NewControllerRegister() handler.Add("/api/list", &TestController{}, "*:List")