Skip to content

Commit

Permalink
feat: add Server option of WithStripTrailingSlash
Browse files Browse the repository at this point in the history
Add the ability for a user to implicitly strip all slashes
from all routes as well as strip all ending slashes from all
requests at the fuego Server level.
  • Loading branch information
dylanhitt committed Mar 5, 2025
1 parent a296bd1 commit a7f9743
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
10 changes: 10 additions & 0 deletions default_middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"net"
"net/http"
"strings"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -158,3 +159,12 @@ func (l defaultLogger) middleware(next http.Handler) http.Handler {
}
})
}

func stripTrailingSlashMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if len(r.URL.Path) > 1 {
r.URL.Path = strings.TrimRight(r.URL.Path, "/")
}
next.ServeHTTP(w, r)
})
}
11 changes: 11 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"slices"
"strconv"
"strings"

"github.com/getkin/kin-openapi/openapi3"
)
Expand Down Expand Up @@ -460,3 +461,13 @@ func OptionSecurity(securityRequirements ...openapi3.SecurityRequirement) func(*
*r.Operation.Security = append(*r.Operation.Security, securityRequirements...)
}
}

// OptionStripTrailingSlash ensure that the route declaration
// will have its ending trailing slash stripped.
func OptionStripTrailingSlash() func(*BaseRoute) {
return func(r *BaseRoute) {
if len(r.Path) > 1 {
r.Path = strings.TrimRight(r.Path, "/")
}
}
}
8 changes: 8 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,11 @@ func TestDefaultStatusCode(t *testing.T) {
require.Equal(t, 500, w.Code)
})
}

func TestOptionStripTrailingSlash(t *testing.T) {
t.Run("Route trailing slash is stripped", func(t *testing.T) {
s := fuego.NewServer()
route := fuego.Get(s, "/test/", helloWorld, fuego.OptionStripTrailingSlash())
require.Equal(t, "/test", route.Path)
})
}
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,13 @@ func WithLoggingMiddleware(loggingConfig LoggingConfig) func(*Server) {
}
}
}

// WithStripTrailingSlash ensure all declared routes trailing slash
// is stripped. This option also applies a middleware
// that strips the trailing slash from every incoming request.
func WithStripTrailingSlash() func(*Server) {
return func(s *Server) {
s.routeOptions = append(s.routeOptions, OptionStripTrailingSlash())
s.globalMiddlewares = append(s.globalMiddlewares, stripTrailingSlashMiddleware)
}
}
54 changes: 54 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,3 +843,57 @@ func TestWithSeveralGlobalMiddelwares(t *testing.T) {
require.Equal(t, "two", res.Body.String())
})
}

func TestWithStripTrailingSlash(t *testing.T) {
s := NewServer(
WithStripTrailingSlash(),
WithAddr(":9998"),
)
Get(s, "/withtrailingslash/", dummyController)
Get(s, "/withouttrailingslash", dummyController)

err := s.setup()
require.NoError(t, err)

t.Run("requests with trailing slash", func(t *testing.T) {
t.Run("route with trailing slash", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/withtrailingslash/", nil)
res := httptest.NewRecorder()

s.Handler.ServeHTTP(res, req)

t.Log(res.Body.String())
require.Equal(t, 200, res.Code)
})
t.Run("route without trailing slash", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/withouttrailingslash/", nil)
res := httptest.NewRecorder()

s.Handler.ServeHTTP(res, req)

t.Log(res.Body.String())
require.Equal(t, 200, res.Code)
})
})

t.Run("requests without trailing slash", func(t *testing.T) {
t.Run("route with trailing slash", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/withtrailingslash", nil)
res := httptest.NewRecorder()

s.Handler.ServeHTTP(res, req)

t.Log(res.Body.String())
require.Equal(t, 200, res.Code)
})
t.Run("route without trailing slash", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/withouttrailingslash", nil)
res := httptest.NewRecorder()

s.Handler.ServeHTTP(res, req)

t.Log(res.Body.String())
require.Equal(t, 200, res.Code)
})
})
}

0 comments on commit a7f9743

Please sign in to comment.