From b8046ce30306fe68e383ea6448982d6121639369 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sun, 22 Sep 2019 12:16:17 -0400 Subject: [PATCH] once --- pkging/stdos/file.go | 12 +++------- pkging/stdos/file_test.go | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/pkging/stdos/file.go b/pkging/stdos/file.go index 7ab1619..0488385 100644 --- a/pkging/stdos/file.go +++ b/pkging/stdos/file.go @@ -61,13 +61,8 @@ func (f *File) Name() string { return f.info.Name() } -type HTTP struct { - pkging.File - osf *os.File -} - -func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) { - osinfos, err := f.osf.Readdir(count) +func (f *File) Readdir(count int) ([]os.FileInfo, error) { + osinfos, err := f.File.Readdir(count) if err != nil { return nil, err } @@ -78,14 +73,13 @@ func (f *HTTP) Readdir(count int) ([]os.FileInfo, error) { } return infos, err } - func (f *File) Open(name string) (http.File, error) { fp := path.Join(f.Path().Name, name) f2, err := f.pkging.Open(fp) if err != nil { return nil, err } - return &HTTP{File: f2, osf: f.File}, nil + return f2, nil } func (f *File) Path() pkging.Path { diff --git a/pkging/stdos/file_test.go b/pkging/stdos/file_test.go index 90e0e5c..625afcc 100644 --- a/pkging/stdos/file_test.go +++ b/pkging/stdos/file_test.go @@ -1,6 +1,10 @@ package stdos import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -27,3 +31,47 @@ func Test_File_Stat_No_Info(t *testing.T) { r.Equal(oi.Name(), info.Name()) // r.Equal("", f.Name()) } + +func Test_File_HTTP_Dir(t *testing.T) { + r := require.New(t) + + pkg, err := New() + r.NoError(err) + + fp := filepath.Join("..", "..", "examples", "app", "public") + + gots := httptest.NewServer(http.FileServer(http.Dir(fp))) + defer gots.Close() + + dir, err := pkg.Open("/examples/app/public") + r.NoError(err) + + pkgts := httptest.NewServer(http.FileServer(dir)) + defer pkgts.Close() + + paths := []string{ + "/", + "/index.html", + "/images", + "/images/images/mark.png", + } + + for _, path := range paths { + t.Run(path, func(st *testing.T) { + r := require.New(st) + + gores, err := http.Get(gots.URL + path) + r.NoError(err) + + pkgres, err := http.Get(pkgts.URL + path) + r.NoError(err) + + gobody, err := ioutil.ReadAll(gores.Body) + r.NoError(err) + + pkgbody, err := ioutil.ReadAll(pkgres.Body) + r.NoError(err) + r.Equal(string(gobody), string(pkgbody)) + }) + } +}