Skip to content
Open
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
5 changes: 4 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ func fsFile(c *Context, file string, filesystem fs.FS) error {
}
defer f.Close()

fi, _ := f.Stat()
fi, err := f.Stat()
if err != nil {
return err
}
if fi.IsDir() {
file = filepath.ToSlash(filepath.Join(file, indexPage)) // ToSlash is necessary for Windows. fs.Open and os.Open are different in that aspect.
f, err = filesystem.Open(file)
Expand Down
24 changes: 24 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,30 @@ func TestContext_FileFS(t *testing.T) {
}
}

var errFileStat = fmt.Errorf("stat failed")

type statErrorFS struct{}

func (statErrorFS) Open(string) (fs.File, error) {
return statErrorFile{}, nil
}

type statErrorFile struct{}

func (statErrorFile) Stat() (fs.FileInfo, error) { return nil, errFileStat }
func (statErrorFile) Read([]byte) (int, error) { return 0, io.EOF }
func (statErrorFile) Close() error { return nil }

func TestContext_FileFSReturnsStatError(t *testing.T) {
e := New()
c := e.NewContext(
httptest.NewRequest(http.MethodGet, "/file", nil),
httptest.NewRecorder(),
)

assert.ErrorIs(t, c.FileFS("file", statErrorFS{}), errFileStat)
}

func TestLogger(t *testing.T) {
e := New()
c := e.NewContext(nil, nil)
Expand Down