From 9edf3143e151186d7e8589724d2be82a3abe1c90 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 18 Dec 2013 10:00:52 +0800 Subject: [PATCH] fix autorouter params --- router.go | 8 ++++++++ router_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/router.go b/router.go index 596b158b..8f29a52c 100644 --- a/router.go +++ b/router.go @@ -634,6 +634,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) runrouter = controllerType runMethod = mName findrouter = true + //parse params + otherurl := requestPath[len("/"+cName+"/"+strings.ToLower(mName)):] + if len(otherurl) > 1 { + plist := strings.Split(otherurl, "/") + for k, v := range plist[1:] { + context.Input.Params[strconv.Itoa(k)] = v + } + } break } } diff --git a/router_test.go b/router_test.go index b444c7b9..a79ab5b4 100644 --- a/router_test.go +++ b/router_test.go @@ -19,6 +19,10 @@ func (this *TestController) List() { this.Ctx.Output.Body([]byte("i am list")) } +func (this *TestController) Params() { + this.Ctx.Output.Body([]byte(this.Ctx.Input.Params["0"] + this.Ctx.Input.Params["1"] + this.Ctx.Input.Params["2"])) +} + func (this *TestController) Myext() { this.Ctx.Output.Body([]byte(this.Ctx.Input.Param(":ext"))) } @@ -89,6 +93,18 @@ func TestAutoFunc(t *testing.T) { } } +func TestAutoFuncParams(t *testing.T) { + r, _ := http.NewRequest("GET", "/test/params/2009/11/12", nil) + w := httptest.NewRecorder() + + handler := NewControllerRegistor() + handler.AddAuto(&TestController{}) + handler.ServeHTTP(w, r) + if w.Body.String() != "20091112" { + t.Errorf("user define func can't run") + } +} + func TestAutoExtFunc(t *testing.T) { r, _ := http.NewRequest("GET", "/test/myext.json", nil) w := httptest.NewRecorder()