Merge d7d24601340cfbfebb52c5eeb15bd689ff5a0276 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Ruben Pari 2026-10-12 17:38:58 +02:00 committed by GitHub
commit 705ded771b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 1 deletions

View File

@ -258,7 +258,9 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS
c.Writer.WriteHeader(http.StatusNotFound)
}
file := c.Param("filepath")
// Normalize like net/http.FileServer so embed/fs.Sub preflight Open
// accepts directory URLs with trailing slashes (#4451).
file := path.Clean("/" + c.Param("filepath"))
// Check if file exists and/or if we have permission to access it
f, err := fs.Open(file)
if err != nil {

View File

@ -12,6 +12,7 @@ import (
"path/filepath"
"strings"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -703,6 +704,23 @@ func TestRouterStaticFSNotFound(t *testing.T) {
assert.Equal(t, "non existent", w.Body.String())
}
func TestRouterStaticFSTrailingSlashEmbed(t *testing.T) {
fsys := fstest.MapFS{
"dir/index.html": &fstest.MapFile{Data: []byte("INDEX")},
"dir/file.txt": &fstest.MapFile{Data: []byte("FILE")},
}
router := New()
router.StaticFS("/static", http.FS(fsys))
w := PerformRequest(router, http.MethodGet, "/static/dir/")
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "INDEX")
w = PerformRequest(router, http.MethodGet, "/static/dir/file.txt")
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "FILE", w.Body.String())
}
func TestRouterStaticFSFileNotFound(t *testing.T) {
router := New()