Skip to content

Commit

Permalink
contrib/dimfeld/httptreemux.v5: do not trim slash from path
Browse files Browse the repository at this point in the history
When handling the supported redirect modes of the router
we were incorrectly trimming trailing slashes from the
route path matched by the router. The previous behaviour
is restored where we return the exact path matched by
the router.
  • Loading branch information
devillecodes committed Apr 25, 2024
1 parent aeb6214 commit 4d3f72e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
8 changes: 0 additions & 8 deletions contrib/dimfeld/httptreemux.v5/httptreemux.go
Expand Up @@ -147,14 +147,6 @@ func getRoute(router *httptreemux.TreeMux, w http.ResponseWriter, req *http.Requ
newP = "/:" + k
route = strings.Replace(route, oldP, newP, 1)
}

// remove trailing slash from route to standardize returned value
// the router does not allow you to register two matching routes with the only difference being a trailing slash
// this only affects the resulting returned value and not the actual request URL set on tag http.url
if trailingSlash {
route = strings.TrimSuffix(route, "/")
}

return route, true
}

Expand Down
8 changes: 4 additions & 4 deletions contrib/dimfeld/httptreemux.v5/httptreemux_test.go
Expand Up @@ -301,7 +301,7 @@ func TestTrailingSlashRoutesWithBehaviorRedirect301(t *testing.T) {
s := spans[0]
assert.Equal("http.request", s.OperationName())
assert.Equal("my-service", s.Tag(ext.ServiceName))
assert.Equal("GET /api/:parameter", s.Tag(ext.ResourceName))
assert.Equal("GET /api/:parameter/", s.Tag(ext.ResourceName))
assert.Equal("301", s.Tag(ext.HTTPCode))
assert.Equal("GET", s.Tag(ext.HTTPMethod))
assert.Equal("http://example.com/api/paramvalue/", s.Tag(ext.HTTPURL))
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestTrailingSlashRoutesWithBehaviorRedirect307(t *testing.T) {
s := spans[0]
assert.Equal("http.request", s.OperationName())
assert.Equal("my-service", s.Tag(ext.ServiceName))
assert.Equal("GET /api/:parameter", s.Tag(ext.ResourceName))
assert.Equal("GET /api/:parameter/", s.Tag(ext.ResourceName))
assert.Equal("307", s.Tag(ext.HTTPCode))
assert.Equal("GET", s.Tag(ext.HTTPMethod))
assert.Equal("http://example.com/api/paramvalue/", s.Tag(ext.HTTPURL))
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestTrailingSlashRoutesWithBehaviorRedirect308(t *testing.T) {
s := spans[0]
assert.Equal("http.request", s.OperationName())
assert.Equal("my-service", s.Tag(ext.ServiceName))
assert.Equal("GET /api/:parameter", s.Tag(ext.ResourceName))
assert.Equal("GET /api/:parameter/", s.Tag(ext.ResourceName))
assert.Equal("308", s.Tag(ext.HTTPCode))
assert.Equal("GET", s.Tag(ext.HTTPMethod))
assert.Equal("http://example.com/api/paramvalue/", s.Tag(ext.HTTPURL))
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestTrailingSlashRoutesWithBehaviorUseHandler(t *testing.T) {
s := spans[0]
assert.Equal("http.request", s.OperationName())
assert.Equal("my-service", s.Tag(ext.ServiceName))
assert.Equal("GET /api/:parameter", s.Tag(ext.ResourceName))
assert.Equal("GET /api/:parameter/", s.Tag(ext.ResourceName))
assert.Equal("200", s.Tag(ext.HTTPCode))
assert.Equal("GET", s.Tag(ext.HTTPMethod))
assert.Equal("http://example.com/api/paramvalue/", s.Tag(ext.HTTPURL))
Expand Down

0 comments on commit 4d3f72e

Please sign in to comment.