Skip to content

Commit

Permalink
fix: handle end of path wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 6, 2024
1 parent 7441da2 commit dac8251
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ func (def EndpointDefinition) pathParams() ([]string, error) {
var result []string
for _, matches := range pathSegmentPattern.FindAllStringSubmatch(def.Path, strings.Count(def.Path, "/")) {
n := matches[1]
if n == "$" {
continue
}
n = strings.TrimSuffix(n, "...")
if !token.IsIdentifier(n) {
return nil, fmt.Errorf("path parameter name not permitted: %q is not a Go identifier", n)
Expand Down
26 changes: 26 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,18 @@ func TestRoutes(t *testing.T) {
assert.Equal(t, http.StatusText(http.StatusInternalServerError)+"\n", string(res.WriteArgsForCall(0)))
assert.Contains(t, logBuffer.String(), "error=banana")
})

t.Run("when the path has an end of path delimiter", func(t *testing.T) {
//
ts := template.Must(template.New("simple path").Parse(
`{{define "GET /{$} ListArticles(ctx)" }}{{len .}}{{end}}`,
))
mux := http.NewServeMux()
s := new(fake.Receiver)

err := muxt.Handlers(mux, ts, muxt.WithReceiver(s))
require.NoError(t, err)
})
}

type errorWriter struct {
Expand Down Expand Up @@ -672,6 +684,20 @@ func Test_endpoint(t *testing.T) {
}, pat)
},
},
{
Name: "with end of path wildcard",
TemplateName: "PUT /ping/pong/{$}",
ExpMatch: true,
Pattern: func(t *testing.T, pat muxt.EndpointDefinition) {
assert.Equal(t, muxt.EndpointDefinition{
Method: http.MethodPut,
Host: "",
Path: "/ping/pong/{$}",
Pattern: "PUT /ping/pong/{$}",
Handler: "",
}, pat)
},
},
{
Name: "put root",
TemplateName: "OPTIONS /",
Expand Down

0 comments on commit dac8251

Please sign in to comment.