Skip to content

Commit

Permalink
fixed #479
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vishal.rana@verizon.com>
  • Loading branch information
Vishal Rana committed Nov 14, 2016
1 parent d74c6ad commit c59b79d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
40 changes: 22 additions & 18 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"os"
"path/filepath"
"strings"

"golang.org/x/net/websocket"
)

type (
Expand Down Expand Up @@ -175,16 +173,15 @@ type (
}

context struct {
request *http.Request
response *Response
webSocket *websocket.Conn
path string
pnames []string
pvalues []string
query url.Values
handler HandlerFunc
store Map
echo *Echo
request *http.Request
response *Response
path string
pnames []string
pvalues []string
query url.Values
handler HandlerFunc
store Map
echo *Echo
}
)

Expand Down Expand Up @@ -238,15 +235,22 @@ func (c *context) SetPath(p string) {
c.path = p
}

func (c *context) Param(name string) (value string) {
l := len(c.pnames)
func (c *context) Param(name string) string {
for i, n := range c.pnames {
if n == name && i < l {
value = c.pvalues[i]
break
if i < len(c.pnames) {
if strings.HasPrefix(n, name) {
return c.pvalues[i]
}

// Param name with aliases
for _, p := range strings.Split(n, ",") {
if p == name {
return c.pvalues[i]
}
}
}
}
return
return ""
}

func (c *context) ParamNames() []string {
Expand Down
12 changes: 12 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ func TestContextPathParam(t *testing.T) {
assert.Equal(t, "501", c.Param("fid"))
}

func TestContextPathParamNamesAlais(t *testing.T) {
e := New()
req, _ := http.NewRequest(GET, "/", nil)
c := e.NewContext(req, nil)

c.SetParamNames("id,name")
c.SetParamValues("joe")

assert.Equal(t, "joe", c.Param("id"))
assert.Equal(t, "joe", c.Param("name"))
}

func TestContextFormValue(t *testing.T) {
f := make(url.Values)
f.Set("name", "Jon Snow")
Expand Down
9 changes: 8 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package echo

import "strings"

type (
// Router is the registry of all registered routes for an `Echo` instance for
// request matching and URL path parameter parsing.
Expand Down Expand Up @@ -170,7 +172,12 @@ func (r *Router) insert(method, path string, h HandlerFunc, t kind, ppath string
if h != nil {
cn.addHandler(method, h)
cn.ppath = ppath
cn.pnames = pnames
for i, n := range cn.pnames {
// Param name aliases
if !strings.Contains(n, pnames[i]) {
cn.pnames[i] += "," + pnames[i]
}
}
}
}
return
Expand Down
9 changes: 6 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package echo
import (
"fmt"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -823,9 +824,11 @@ func TestRouterAPI(t *testing.T) {
c := e.NewContext(nil, nil).(*context)
for _, route := range gitHubAPI {
r.Find(route.Method, route.Path, c)
for i, n := range c.pnames {
if assert.NotEmpty(t, n) {
assert.Equal(t, n, c.pnames[i])
for _, n := range c.pnames {
for _, p := range strings.Split(n, ",") {
if assert.NotEmpty(t, p) {
assert.Equal(t, c.Param(p), ":"+p)
}
}
}
}
Expand Down

0 comments on commit c59b79d

Please sign in to comment.