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

Commit

Permalink
Ignore directories in disk resolver (#241)
Browse files Browse the repository at this point in the history
The disk resolver was the only one that returned directories as box
files. This modification allows http redirections of index.html to
work when working with the default disk resolver.

A test case has been added to verify http's behavior when working with
the disk resolver.

Fix #162
Fix #211
Fix #235
  • Loading branch information
Lesterpig authored and markbates committed Sep 30, 2019
1 parent b6d537d commit 0f1e543
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
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()))
})
}
}

0 comments on commit 0f1e543

Please sign in to comment.