Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove trailing slash from http request if it exists in url #1754

Merged
merged 6 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/cmd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/fatih/color"
Expand Down Expand Up @@ -99,6 +101,7 @@ func NewHTTPServer(
h.ServeHTTP(w, r)
})
})
r.Use(removeTrailingSlash)
r.Use(middleware.Compress(gzip.DefaultCompression))
r.Use(middleware.Recoverer)
r.Mount("/debug", middleware.Profiler())
Expand Down Expand Up @@ -235,3 +238,15 @@ func (h *HTTPServer) Shutdown(ctx context.Context) error {

return h.Server.Shutdown(ctx)
}

func removeTrailingSlash(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(strings.TrimSuffix(r.URL.Path, "/"))
// Panic if URL can not be parsed if a trailing slash is trimmed.
if err != nil {
panic(err)
}
r.URL = u
h.ServeHTTP(w, r)
})
}
61 changes: 61 additions & 0 deletions internal/cmd/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
)

const (
tsoHeader = "trailing-slash-on"
)

func TestTrailingSlashMiddleware(t *testing.T) {
r := chi.NewRouter()

r.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tso := r.Header.Get(tsoHeader)
if tso != "" {
tsh := removeTrailingSlash(h)

tsh.ServeHTTP(w, r)
return
}

h.ServeHTTP(w, r)
})
})
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
})

s := httptest.NewServer(r)

defer s.Close()

// Request with the middleware on.
req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, fmt.Sprintf("%s/hello/", s.URL), nil)
assert.NoError(t, err)
req.Header.Set(tsoHeader, "on")

res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, res.StatusCode)
res.Body.Close()

// Request with the middleware off.
req, err = http.NewRequestWithContext(context.TODO(), http.MethodGet, fmt.Sprintf("%s/hello/", s.URL), nil)
assert.NoError(t, err)

res, err = http.DefaultClient.Do(req)
assert.NoError(t, err)

assert.Equal(t, http.StatusNotFound, res.StatusCode)
res.Body.Close()
}