From bbf7710086fa8fd299f138b560920a547359f89e Mon Sep 17 00:00:00 2001 From: Max Thomson Date: Thu, 19 Sep 2024 13:33:45 -0700 Subject: [PATCH 1/2] contrib/dimfeld/httptreemux.v5: failing tests for path variable replacement --- .../httptreemux.v5/httptreemux_test.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/contrib/dimfeld/httptreemux.v5/httptreemux_test.go b/contrib/dimfeld/httptreemux.v5/httptreemux_test.go index 6bf900217c..3af094528d 100644 --- a/contrib/dimfeld/httptreemux.v5/httptreemux_test.go +++ b/contrib/dimfeld/httptreemux.v5/httptreemux_test.go @@ -670,6 +670,44 @@ func TestTrailingSlashRoutesWithBehaviorUseHandler(t *testing.T) { }) } +func TestDuplicateWordsParamsHandler(t *testing.T) { + tests := []struct { + name string + route string + url string + }{ + { + name: "Test minimal case", + route: "/1a/:n", + url: "/1a/1", + }, + { + name: "Test string with separators", + route: "/foo/2by4/bar/:n", + url: "/foo/2by4/bar/2", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert := assert.New(t) + mt := mocktracer.Start() + defer mt.Stop() + + router := New() + router.GET(tt.route, handler200) + + r := httptest.NewRequest("GET", tt.url, nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, r) + + spans := mt.FinishedSpans() + assert.Equal(1, len(spans)) + assert.Equal("GET "+tt.route, spans[0].Tag(ext.ResourceName)) + }) + } +} + func TestIsSupportedRedirectStatus(t *testing.T) { tests := []struct { name string From 40c7aca9a957143343f1a78b34ed338118144f88 Mon Sep 17 00:00:00 2001 From: Max Thomson Date: Thu, 19 Sep 2024 13:47:16 -0700 Subject: [PATCH 2/2] contrib/dimfeld/httptreemux.v5: only replace at end of path --- contrib/dimfeld/httptreemux.v5/httptreemux.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/contrib/dimfeld/httptreemux.v5/httptreemux.go b/contrib/dimfeld/httptreemux.v5/httptreemux.go index d9ae06e91d..e50c951cb9 100644 --- a/contrib/dimfeld/httptreemux.v5/httptreemux.go +++ b/contrib/dimfeld/httptreemux.v5/httptreemux.go @@ -145,7 +145,10 @@ func getRoute(router *httptreemux.TreeMux, w http.ResponseWriter, req *http.Requ // replace parameter at end of the path, i.e. "../:param" oldP = "/" + v newP = "/:" + k - route = strings.Replace(route, oldP, newP, 1) + if strings.HasSuffix(route, oldP) { + endPos := strings.LastIndex(route, oldP) + route = route[:endPos] + newP + } } return route, true }