forked from mirror/pkger
once
This commit is contained in:
parent
b03c3dba90
commit
b8046ce303
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue