Skip to content

Commit

Permalink
static: only strip if the file path is not a single slash (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhan005 committed Aug 8, 2023
1 parent b567fe0 commit a89cfda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions static.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ func Static(opts ...StaticOptions) Handler {
}
}

// The go embed file system returns an error when the path ends with a slash.
file = strings.TrimRight(file, "/")
// The go embed file system returns an error when the path ends with a slash or the path is empty.
if file == "/" {
file = "."
} else {
file = strings.TrimRight(file, "/")
}

f, err := opt.FileSystem.Open(file)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion static_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/assert"
)

//go:embed internal
//go:embed internal README.md
var embedFS embed.FS

func TestStatic(t *testing.T) {
Expand Down Expand Up @@ -142,6 +142,11 @@ func TestStatic_Options(t *testing.T) {
index: "README.md",
wantStatusCode: http.StatusOK,
},
{
uri: "/embed/",
index: "README.md",
wantStatusCode: http.StatusOK,
},
}
for _, test := range tests {
t.Run(test.index, func(t *testing.T) {
Expand Down

0 comments on commit a89cfda

Please sign in to comment.