2019-09-03 19:41:21 +03:00
|
|
|
package pkgtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-10-15 23:40:45 +03:00
|
|
|
"os"
|
2019-09-03 19:41:21 +03:00
|
|
|
"path/filepath"
|
2019-10-16 00:12:46 +03:00
|
|
|
"strings"
|
2019-09-03 19:41:21 +03:00
|
|
|
"testing"
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
"github.com/markbates/pkger/pkging"
|
2019-09-03 19:41:21 +03:00
|
|
|
"github.com/markbates/pkger/pkging/pkgutil"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ├── main.go
|
|
|
|
// ├── public
|
|
|
|
// │ ├── images
|
|
|
|
// │ │ ├── mark.png
|
|
|
|
// │ └── index.html
|
|
|
|
// └── templates
|
|
|
|
// ├── a.txt
|
|
|
|
// └── b
|
|
|
|
// └── b.txt
|
2019-10-15 23:40:45 +03:00
|
|
|
var folderFiles = []string{
|
|
|
|
"/main.go",
|
|
|
|
"/public/images/mark.png",
|
|
|
|
"/public/index.html",
|
|
|
|
"/templates/a.txt",
|
|
|
|
"/templates/b/b.txt",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Suite) WriteFolder(path string) error {
|
|
|
|
for _, f := range folderFiles {
|
|
|
|
f = filepath.Join(path, f)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(f), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(f, []byte("!"+f), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-03 19:41:21 +03:00
|
|
|
}
|
2019-10-15 23:40:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
2019-09-03 19:41:21 +03:00
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
func (s Suite) LoadFolder(pkg pkging.Pkger) error {
|
|
|
|
for _, f := range folderFiles {
|
2019-09-12 04:29:39 +03:00
|
|
|
if err := pkg.MkdirAll(filepath.Dir(f), 0755); err != nil {
|
2019-09-03 19:41:21 +03:00
|
|
|
return err
|
|
|
|
}
|
2019-09-12 04:29:39 +03:00
|
|
|
if err := pkgutil.WriteFile(pkg, f, []byte("!"+f), 0644); err != nil {
|
2019-09-03 19:41:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Suite) Test_HTTP_Dir(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
cur, err := pkg.Current()
|
2019-09-03 19:41:21 +03:00
|
|
|
r.NoError(err)
|
|
|
|
ip := cur.ImportPath
|
|
|
|
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
req string
|
|
|
|
exp string
|
|
|
|
}{
|
2019-09-03 22:24:16 +03:00
|
|
|
{in: "/", req: "/", exp: `>public/</a`},
|
|
|
|
{in: ":" + "/", req: "/", exp: `>public/</a`},
|
|
|
|
{in: ip + ":" + "/", req: "/", exp: `>public/</a`},
|
2019-09-03 19:41:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
2019-09-03 23:46:51 +03:00
|
|
|
s.Run(t, tt.in+tt.req, func(st *testing.T) {
|
2019-09-12 04:29:39 +03:00
|
|
|
r := require.New(st)
|
2019-09-03 23:46:51 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
|
|
|
r.NoError(s.LoadFolder(pkg))
|
2019-09-03 19:41:21 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
dir, err := pkg.Open(tt.in)
|
2019-09-03 23:46:51 +03:00
|
|
|
r.NoError(err)
|
2019-09-04 01:38:52 +03:00
|
|
|
defer dir.Close()
|
|
|
|
|
2019-09-03 23:46:51 +03:00
|
|
|
ts := httptest.NewServer(http.FileServer(dir))
|
|
|
|
defer ts.Close()
|
2019-09-03 19:41:21 +03:00
|
|
|
|
2019-09-03 23:46:51 +03:00
|
|
|
res, err := http.Get(ts.URL + tt.req)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
2019-09-03 19:41:21 +03:00
|
|
|
|
2019-09-03 23:46:51 +03:00
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Contains(string(b), tt.exp)
|
|
|
|
r.NotContains(string(b), "mark.png")
|
|
|
|
})
|
2019-09-03 19:41:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 22:18:31 +03:00
|
|
|
func (s Suite) Test_HTTP_Dir_IndexHTML(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
cur, err := pkg.Current()
|
2019-09-03 22:18:31 +03:00
|
|
|
r.NoError(err)
|
|
|
|
ip := cur.ImportPath
|
|
|
|
|
|
|
|
table := []struct {
|
2019-10-16 00:37:51 +03:00
|
|
|
in string
|
|
|
|
req string
|
|
|
|
code int
|
2019-09-03 22:18:31 +03:00
|
|
|
}{
|
2019-10-16 00:37:51 +03:00
|
|
|
{in: "/public", req: "/", code: 200},
|
|
|
|
{in: ":" + "/public", req: "/", code: 200},
|
|
|
|
{in: ip + ":" + "/public", req: "/", code: 200},
|
|
|
|
{in: ip + ":" + "/public", req: "/unknown", code: 404},
|
2019-09-03 22:18:31 +03:00
|
|
|
}
|
|
|
|
|
2019-09-03 22:24:16 +03:00
|
|
|
exp := "index.html"
|
2019-09-03 22:18:31 +03:00
|
|
|
for _, tt := range table {
|
2019-09-03 23:45:32 +03:00
|
|
|
s.Run(t, tt.in+exp, func(st *testing.T) {
|
2019-09-03 22:18:31 +03:00
|
|
|
r := require.New(st)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
2019-09-03 23:45:32 +03:00
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
r.NoError(s.LoadFolder(pkg))
|
|
|
|
|
|
|
|
dir, err := pkg.Open(tt.in)
|
2019-09-03 22:18:31 +03:00
|
|
|
r.NoError(err)
|
2019-09-04 01:38:52 +03:00
|
|
|
defer dir.Close()
|
2019-09-03 22:18:31 +03:00
|
|
|
|
|
|
|
ts := httptest.NewServer(http.FileServer(dir))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + tt.req)
|
|
|
|
r.NoError(err)
|
2019-10-16 00:37:51 +03:00
|
|
|
r.Equal(tt.code, res.StatusCode)
|
|
|
|
|
|
|
|
if tt.code != 200 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-03 22:18:31 +03:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
|
|
r.NoError(err)
|
|
|
|
|
2019-09-03 22:24:16 +03:00
|
|
|
body := string(b)
|
|
|
|
r.Contains(body, exp)
|
2019-09-03 22:18:31 +03:00
|
|
|
r.NotContains(body, "mark.png")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 19:22:35 +03:00
|
|
|
func (s Suite) Test_HTTP_File(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
cur, err := pkg.Current()
|
2019-09-05 19:22:35 +03:00
|
|
|
r.NoError(err)
|
|
|
|
ip := cur.ImportPath
|
|
|
|
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
}{
|
|
|
|
{in: "/public"},
|
|
|
|
{in: ":" + "/public"},
|
|
|
|
{in: ip + ":" + "/public"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range table {
|
|
|
|
s.Run(t, tt.in, func(st *testing.T) {
|
|
|
|
r := require.New(st)
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
pkg, err := s.Make()
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
r.NoError(s.LoadFolder(pkg))
|
2019-09-05 19:22:35 +03:00
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
tdir, err := ioutil.TempDir("", "")
|
|
|
|
r.NoError(err)
|
|
|
|
defer os.RemoveAll(tdir)
|
|
|
|
r.NoError(s.WriteFolder(tdir))
|
|
|
|
|
2019-10-16 00:12:46 +03:00
|
|
|
tpub := filepath.Join(tdir, "public")
|
|
|
|
gots := httptest.NewServer(http.FileServer(http.Dir(tpub)))
|
2019-10-15 23:40:45 +03:00
|
|
|
defer gots.Close()
|
|
|
|
|
2019-09-12 04:29:39 +03:00
|
|
|
dir, err := pkg.Open(tt.in)
|
2019-09-05 19:22:35 +03:00
|
|
|
r.NoError(err)
|
|
|
|
defer dir.Close()
|
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
pkgts := httptest.NewServer(http.FileServer(dir))
|
|
|
|
defer pkgts.Close()
|
2019-09-05 19:22:35 +03:00
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
paths := []string{
|
|
|
|
"/",
|
|
|
|
"/index.html",
|
|
|
|
"/images",
|
2019-10-16 00:12:46 +03:00
|
|
|
"/images/mark.png",
|
2019-10-15 23:40:45 +03:00
|
|
|
}
|
2019-09-05 19:22:35 +03:00
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
for _, path := range paths {
|
|
|
|
t.Run(path, func(st *testing.T) {
|
|
|
|
r := require.New(st)
|
2019-09-05 19:22:35 +03:00
|
|
|
|
2019-10-15 23:40:45 +03:00
|
|
|
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)
|
2019-10-16 00:12:46 +03:00
|
|
|
|
|
|
|
exp := strings.ReplaceAll(string(gobody), tdir, "")
|
|
|
|
r.Equal(exp, string(pkgbody))
|
2019-10-15 23:40:45 +03:00
|
|
|
})
|
|
|
|
}
|
2019-09-05 19:22:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-09-04 01:38:52 +03:00
|
|
|
|
2019-09-03 19:41:21 +03:00
|
|
|
//
|
|
|
|
// func (s Suite) Test_HTTP_Dir_Memory_StripPrefix(t *testing.T) {
|
|
|
|
// r := require.New(t)
|
|
|
|
//
|
|
|
|
// fs := NewPkger()
|
|
|
|
// r.NoError(Folder.Create(fs))
|
|
|
|
//
|
|
|
|
// dir, err := fs.Open("/public")
|
|
|
|
// r.NoError(err)
|
|
|
|
// defer dir.Close()
|
|
|
|
//
|
|
|
|
// ts := httptest.NewServer(http.StripPrefix("/assets/", http.FileServer(dir)))
|
|
|
|
// defer ts.Close()
|
|
|
|
//
|
|
|
|
// res, err := http.Get(ts.URL + "/assets/images/mark.png")
|
|
|
|
// r.NoError(err)
|
|
|
|
// r.Equal(200, res.StatusCode)
|
|
|
|
//
|
|
|
|
// b, _ := ioutil.ReadAll(res.Body)
|
|
|
|
// // r.NoError(err)
|
|
|
|
// r.Contains(string(b), "!/public/images/mark.png")
|
|
|
|
//
|
|
|
|
// res, err = http.Get(ts.URL + "/assets/images/")
|
|
|
|
// r.NoError(err)
|
|
|
|
// r.Equal(200, res.StatusCode)
|
|
|
|
//
|
|
|
|
// b, _ = ioutil.ReadAll(res.Body)
|
|
|
|
// // r.NoError(err)
|
|
|
|
// r.Contains(string(b), `<a href="/mark.png">/mark.png</a>`)
|
|
|
|
// r.NotContains(string(b), `/public`)
|
|
|
|
// r.NotContains(string(b), `/images`)
|
|
|
|
// r.NotContains(string(b), `/go.mod`)
|
|
|
|
// }
|