Skip to content

Commit

Permalink
Merge pull request #274 from treetopllc/master
Browse files Browse the repository at this point in the history
Fix capture group replacement in URLFor / URLWith
  • Loading branch information
José Miguel Molina committed Jun 17, 2014
2 parents adaa8f7 + 69b1f20 commit 9af2cc2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func (r *route) Handle(c Context, res http.ResponseWriter) {
// URLWith returns the url pattern replacing the parameters for its values
func (r *route) URLWith(args []string) string {
if len(args) > 0 {
reg := regexp.MustCompile(`:[^/#?()\.\\]+`)
reg := regexp.MustCompile(`:[^/#?()\.\\]+|\(\?P<[a-zA-Z0-9]+>.*\)`)
argCount := len(args)
i := 0
url := reg.ReplaceAllStringFunc(r.pattern, func(m string) string {
Expand Down
5 changes: 5 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,14 @@ func Test_URLFor(t *testing.T) {
// Nothing
}).Name("bar")

router.Get("/baz/:id/(?P<name>[a-z]*)", func(params Params, routes Routes) {
// Nothing
}).Name("baz_id")

router.Get("/bar/:id/:name", func(params Params, routes Routes) {
expect(t, routes.URLFor("foo", nil), "/foo")
expect(t, routes.URLFor("bar", 5), "/bar/5")
expect(t, routes.URLFor("baz_id", 5, "john"), "/baz/5/john")
expect(t, routes.URLFor("bar_id", 5, "john"), "/bar/5/john")
}).Name("bar_id")

Expand Down

0 comments on commit 9af2cc2

Please sign in to comment.