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

Commit 0f1e543

Browse files
Lesterpigmarkbates
authored andcommitted
Ignore directories in disk resolver (#241)
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
1 parent b6d537d commit 0f1e543

6 files changed

Lines changed: 45 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Css

v2/_fixtures/http_test/footer.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Footer

v2/_fixtures/http_test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Index
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sub

v2/file/resolver/disk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (d *Disk) Resolve(box string, name string) (file.File, error) {
3737
return nil, err
3838
}
3939
if fi.IsDir() {
40-
return file.NewDir(OsPath(name))
40+
return nil, os.ErrNotExist
4141
}
4242
if bb, err := ioutil.ReadFile(path); err == nil {
4343
return file.NewFile(OsPath(name), bb)

v2/http_box_test.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var httpBox = func() packd.Box {
2525
}
2626

2727
hg, err := resolver.NewHexGzip(map[string]string{
28-
"index.html": ind,
29-
"hello.txt": hello,
28+
"index.html": ind,
29+
"hello.txt": hello,
3030
})
3131
if err != nil {
3232
panic(err)
@@ -129,3 +129,41 @@ func Test_HTTPBox_CaseInsensitive(t *testing.T) {
129129
})
130130
}
131131
}
132+
133+
func Test_HTTPBox_Disk(t *testing.T) {
134+
r := require.New(t)
135+
136+
box := New("http disk box", "./_fixtures/http_test")
137+
mux := http.NewServeMux()
138+
mux.Handle("/", http.FileServer(box))
139+
140+
type testcase struct {
141+
URL, Content, Location string
142+
Code int
143+
}
144+
145+
testcases := []testcase{
146+
{"/", "Index", "", 200},
147+
{"/sub", "Sub", "", 200},
148+
{"/index.html", "", "./", 301},
149+
{"/sub/index.html", "", "./", 301},
150+
{"/sub/", "", "../sub", 301},
151+
{"/footer.html", "Footer", "", 200},
152+
{"/css/main.css", "Css", "", 200},
153+
{"/css", "404 page not found", "", 404},
154+
{"/css/", "404 page not found", "", 404},
155+
}
156+
157+
for _, tc := range testcases {
158+
t.Run("path"+tc.URL, func(t *testing.T) {
159+
req, err := http.NewRequest("GET", tc.URL, nil)
160+
r.NoError(err)
161+
res := httptest.NewRecorder()
162+
mux.ServeHTTP(res, req)
163+
164+
r.Equal(tc.Code, res.Code)
165+
r.Equal(tc.Location, res.Header().Get("location"))
166+
r.Equal(tc.Content, strings.TrimSpace(res.Body.String()))
167+
})
168+
}
169+
}

0 commit comments

Comments
 (0)