diff --git a/v2/_fixtures/http_test/css/main.css b/v2/_fixtures/http_test/css/main.css new file mode 100644 index 0000000..25c06d8 --- /dev/null +++ b/v2/_fixtures/http_test/css/main.css @@ -0,0 +1 @@ +Css diff --git a/v2/_fixtures/http_test/footer.html b/v2/_fixtures/http_test/footer.html new file mode 100644 index 0000000..e36f7f7 --- /dev/null +++ b/v2/_fixtures/http_test/footer.html @@ -0,0 +1 @@ +Footer diff --git a/v2/_fixtures/http_test/index.html b/v2/_fixtures/http_test/index.html new file mode 100644 index 0000000..c846c91 --- /dev/null +++ b/v2/_fixtures/http_test/index.html @@ -0,0 +1 @@ +Index diff --git a/v2/_fixtures/http_test/sub/index.html b/v2/_fixtures/http_test/sub/index.html new file mode 100644 index 0000000..a1a42b5 --- /dev/null +++ b/v2/_fixtures/http_test/sub/index.html @@ -0,0 +1 @@ +Sub diff --git a/v2/file/resolver/disk.go b/v2/file/resolver/disk.go index 4367c8a..8c3c1e7 100644 --- a/v2/file/resolver/disk.go +++ b/v2/file/resolver/disk.go @@ -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) diff --git a/v2/http_box_test.go b/v2/http_box_test.go index 17296ab..3685133 100644 --- a/v2/http_box_test.go +++ b/v2/http_box_test.go @@ -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) @@ -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())) + }) + } +}