From 4ce584c5a6a6264646ea774267319131d9bacc09 Mon Sep 17 00:00:00 2001 From: astaxie Date: Sun, 22 Sep 2013 11:12:37 +0800 Subject: [PATCH] fix #201 --- router.go | 10 +++++++++- router_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index f556848b..54c60009 100644 --- a/router.go +++ b/router.go @@ -311,7 +311,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) params := make(map[string]string) - context.Input.Param = params if p.enableFilter { if l, ok := p.filters["BeforRouter"]; ok { for _, filterR := range l { @@ -412,6 +411,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) break } } + context.Input.Param = params if runrouter != nil { if r.Method == "POST" { @@ -584,6 +584,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) if p.enableAuto { if !findrouter { + lastindex := strings.LastIndex(requestPath, "/") + lastsub := requestPath[lastindex+1:] + if subindex := strings.LastIndex(lastsub, "."); subindex != -1 { + context.Input.Param[":ext"] = lastsub[subindex+1:] + r.URL.Query().Add(":ext", lastsub[subindex+1:]) + r.URL.RawQuery = r.URL.Query().Encode() + requestPath = requestPath[:len(requestPath)-len(lastsub[subindex:])] + } for cName, methodmap := range p.autoRouter { if strings.ToLower(requestPath) == "/"+cName { diff --git a/router_test.go b/router_test.go index a79e5a4f..96800fb8 100644 --- a/router_test.go +++ b/router_test.go @@ -21,6 +21,10 @@ func (this *TestController) List() { this.Ctx.Output.Body([]byte("i am list")) } +func (this *TestController) Myext() { + this.Ctx.Output.Body([]byte(this.Ctx.Input.Params(":ext"))) +} + func TestUserFunc(t *testing.T) { r, _ := http.NewRequest("GET", "/api/list", nil) w := httptest.NewRecorder() @@ -45,6 +49,18 @@ func TestAutoFunc(t *testing.T) { } } +func TestAutoExtFunc(t *testing.T) { + r, _ := http.NewRequest("GET", "/test/myext.json", nil) + w := httptest.NewRecorder() + + handler := NewControllerRegistor() + handler.AddAuto(&TestController{}) + handler.ServeHTTP(w, r) + if w.Body.String() != "json" { + t.Errorf("user define func can't run") + } +} + func TestRouteOk(t *testing.T) { r, _ := http.NewRequest("GET", "/person/anderson/thomas?learn=kungfu", nil)