Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Ignore directories in disk resolver #241

Merged
merged 1 commit into from
Sep 30, 2019
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
1 change: 1 addition & 0 deletions v2/_fixtures/http_test/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Css
1 change: 1 addition & 0 deletions v2/_fixtures/http_test/footer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Footer
1 change: 1 addition & 0 deletions v2/_fixtures/http_test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index
1 change: 1 addition & 0 deletions v2/_fixtures/http_test/sub/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sub
2 changes: 1 addition & 1 deletion v2/file/resolver/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *Disk) Resolve(box string, name string) (file.File, error) {
return nil, err
}
if fi.IsDir() {
return file.NewDir(OsPath(name))
return nil, os.ErrNotExist
}
if bb, err := ioutil.ReadFile(path); err == nil {
return file.NewFile(OsPath(name), bb)
Expand Down
42 changes: 40 additions & 2 deletions v2/http_box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var httpBox = func() packd.Box {
}

hg, err := resolver.NewHexGzip(map[string]string{
"index.html": ind,
"hello.txt": hello,
"index.html": ind,
"hello.txt": hello,
})
if err != nil {
panic(err)
Expand Down Expand Up @@ -129,3 +129,41 @@ func Test_HTTPBox_CaseInsensitive(t *testing.T) {
})
}
}

func Test_HTTPBox_Disk(t *testing.T) {
r := require.New(t)

box := New("http disk box", "./_fixtures/http_test")
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(box))

type testcase struct {
URL, Content, Location string
Code int
}

testcases := []testcase{
{"/", "Index", "", 200},
{"/sub", "Sub", "", 200},
{"/index.html", "", "./", 301},
{"/sub/index.html", "", "./", 301},
{"/sub/", "", "../sub", 301},
{"/footer.html", "Footer", "", 200},
{"/css/main.css", "Css", "", 200},
{"/css", "404 page not found", "", 404},
{"/css/", "404 page not found", "", 404},
}

for _, tc := range testcases {
t.Run("path"+tc.URL, func(t *testing.T) {
req, err := http.NewRequest("GET", tc.URL, nil)
r.NoError(err)
res := httptest.NewRecorder()
mux.ServeHTTP(res, req)

r.Equal(tc.Code, res.Code)
r.Equal(tc.Location, res.Header().Get("location"))
r.Equal(tc.Content, strings.TrimSpace(res.Body.String()))
})
}
}