pkger/pkging/pkgtest/http.go

180 lines
3.1 KiB
Go
Raw Permalink Normal View History

2019-09-03 19:41:21 +03:00
package pkgtest
import (
2019-10-18 19:24:51 +03:00
"io"
2019-09-03 19:41:21 +03:00
"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/stretchr/testify/require"
)
2019-10-18 19:24:51 +03:00
func (s Suite) WriteFolder(root string) error {
app, err := App()
if err != nil {
return err
}
2019-10-15 23:40:45 +03:00
2019-10-18 19:24:51 +03:00
return filepath.Walk(app.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
2019-10-15 23:40:45 +03:00
return err
}
2019-10-18 19:24:51 +03:00
if info.IsDir() {
return nil
2019-10-15 23:40:45 +03:00
}
2019-09-03 19:41:21 +03:00
2019-10-18 19:24:51 +03:00
of, err := os.Open(path)
if err != nil {
2019-09-03 19:41:21 +03:00
return err
}
2019-10-18 19:24:51 +03:00
defer of.Close()
2019-09-03 19:41:21 +03:00
2019-10-18 19:24:51 +03:00
path = strings.TrimPrefix(path, app.Dir)
path = filepath.Join(root, path)
2019-09-12 04:29:39 +03:00
2019-10-18 19:24:51 +03:00
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
2019-09-03 19:41:21 +03:00
2019-10-18 19:24:51 +03:00
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
2019-10-16 16:50:40 +03:00
2019-10-18 19:24:51 +03:00
if _, err := io.Copy(f, of); err != nil {
return err
}
return nil
})
2019-09-03 19:41:21 +03:00
}
2019-10-18 19:24:51 +03:00
func (s Suite) LoadFolder(pkg pkging.Pkger) error {
app, err := App()
if err != nil {
return err
2019-09-03 22:18:31 +03:00
}
2019-10-18 19:24:51 +03:00
return filepath.Walk(app.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
of, err := os.Open(path)
if err != nil {
return err
}
defer of.Close()
2019-09-03 22:18:31 +03:00
2019-10-24 23:22:15 +03:00
if a, ok := pkg.(pkging.Adder); ok {
return a.Add(of)
}
2019-10-18 19:24:51 +03:00
path = strings.TrimPrefix(path, app.Dir)
2019-09-03 22:18:31 +03:00
2019-10-18 19:24:51 +03:00
pt, err := pkg.Parse(path)
if err != nil {
return err
}
2019-10-16 00:37:51 +03:00
2019-10-18 19:24:51 +03:00
if err := pkg.MkdirAll(filepath.Dir(pt.Name), 0755); err != nil {
return err
}
2019-09-03 22:18:31 +03:00
2019-10-24 23:22:15 +03:00
if info.IsDir() {
return nil
}
2019-10-18 19:24:51 +03:00
f, err := pkg.Create(pt.String())
if err != nil {
return err
}
defer f.Close()
2019-09-03 22:18:31 +03:00
2019-10-18 19:24:51 +03:00
if _, err := io.Copy(f, of); err != nil {
return err
}
return nil
})
2019-09-03 22:18:31 +03:00
}
2019-10-18 19:24:51 +03:00
func (s Suite) Test_HTTP(t *testing.T) {
2019-09-05 19:22:35 +03:00
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-18 19:24:51 +03:00
"/images/img1.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)
2019-10-18 19:24:51 +03:00
r.Equal(gores.StatusCode, pkgres.StatusCode)
2019-10-15 23:40:45 +03:00
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, "")
2019-10-16 16:50:40 +03:00
exp = clean(exp)
r.Equal(exp, clean(string(pkgbody)))
2019-10-15 23:40:45 +03:00
})
}
2019-09-05 19:22:35 +03:00
})
}
}