From dac8251dd60db66fdcabaf3141d0b5ca7383445c Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Tue, 6 Aug 2024 00:37:58 -0700 Subject: [PATCH] fix: handle end of path wildcard --- handler.go | 3 +++ handler_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/handler.go b/handler.go index 594fd87..e919477 100644 --- a/handler.go +++ b/handler.go @@ -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) diff --git a/handler_test.go b/handler_test.go index 76c758c..f9eaa08 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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 { @@ -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 /",