2019-07-31 18:53:36 +03:00
|
|
|
package pkger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-08-09 22:43:58 +03:00
|
|
|
func Test_Open_File(t *testing.T) {
|
2019-07-31 18:53:36 +03:00
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
f, err := Open(".")
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.FileServer(f))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + "/cmd/pkger/main.go")
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
|
|
r.NoError(err)
|
|
|
|
r.Contains(string(b), "does not compute")
|
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
}
|
|
|
|
|
2019-08-09 22:43:58 +03:00
|
|
|
func Test_Open_Dir(t *testing.T) {
|
2019-07-31 18:53:36 +03:00
|
|
|
r := require.New(t)
|
|
|
|
|
2019-08-09 22:30:12 +03:00
|
|
|
f, err := Open("/")
|
2019-07-31 18:53:36 +03:00
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.FileServer(f))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + "/cmd/pkger")
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
|
|
r.NoError(err)
|
2019-08-29 01:15:56 +03:00
|
|
|
r.Contains(string(b), `<a href="/cmd/pkger/main.go">/cmd/pkger/main.go</a>`)
|
2019-07-31 18:53:36 +03:00
|
|
|
|
|
|
|
r.NoError(f.Close())
|
|
|
|
}
|
2019-07-31 23:29:49 +03:00
|
|
|
|
2019-08-09 22:43:58 +03:00
|
|
|
func Test_Open_File_Memory(t *testing.T) {
|
2019-07-31 23:29:49 +03:00
|
|
|
r := require.New(t)
|
|
|
|
|
2019-08-01 21:37:01 +03:00
|
|
|
f, err := Create("/suit/case.txt")
|
|
|
|
r.NoError(err)
|
|
|
|
f.Write([]byte(radio))
|
|
|
|
r.NoError(f.Close())
|
2019-07-31 23:29:49 +03:00
|
|
|
|
2019-08-02 06:21:37 +03:00
|
|
|
r.Equal([]byte(radio), f.data)
|
|
|
|
r.Contains(string(f.data), "I wanna bite the hand that feeds me")
|
|
|
|
|
2019-08-01 21:37:01 +03:00
|
|
|
dir, err := Open("/")
|
2019-07-31 23:29:49 +03:00
|
|
|
r.NoError(err)
|
2019-08-01 21:37:01 +03:00
|
|
|
defer dir.Close()
|
2019-07-31 23:29:49 +03:00
|
|
|
|
2019-08-01 21:37:01 +03:00
|
|
|
ts := httptest.NewServer(http.FileServer(dir))
|
2019-07-31 23:29:49 +03:00
|
|
|
defer ts.Close()
|
|
|
|
|
2019-08-01 21:37:01 +03:00
|
|
|
res, err := http.Get(ts.URL + "/suit/case.txt")
|
2019-07-31 23:29:49 +03:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
|
|
|
|
2019-08-02 06:21:37 +03:00
|
|
|
b, _ := ioutil.ReadAll(res.Body)
|
|
|
|
// r.NoError(err)
|
2019-07-31 23:29:49 +03:00
|
|
|
r.Contains(string(b), "I wanna bite the hand that feeds me")
|
|
|
|
|
|
|
|
}
|
2019-08-09 04:51:58 +03:00
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
func Test_Open_Dir_StripPrefix(t *testing.T) {
|
2019-08-09 04:51:58 +03:00
|
|
|
r := require.New(t)
|
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
ts := httptest.NewServer(http.StripPrefix("/assets/", http.FileServer(http.Dir("./testdata/public"))))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(ts.URL + "/assets/radio.radio")
|
2019-08-09 04:51:58 +03:00
|
|
|
r.NoError(err)
|
2019-08-26 22:42:43 +03:00
|
|
|
r.Equal(200, res.StatusCode)
|
2019-08-09 04:51:58 +03:00
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
b, _ := ioutil.ReadAll(res.Body)
|
|
|
|
// r.NoError(err)
|
|
|
|
r.Contains(string(b), "I wanna bite the hand that feeds me")
|
2019-08-09 04:51:58 +03:00
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
res, err = http.Get(ts.URL + "/assets/")
|
2019-08-09 04:51:58 +03:00
|
|
|
r.NoError(err)
|
2019-08-26 22:42:43 +03:00
|
|
|
r.Equal(200, res.StatusCode)
|
2019-08-09 04:51:58 +03:00
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
b, _ = ioutil.ReadAll(res.Body)
|
|
|
|
// r.NoError(err)
|
|
|
|
r.Contains(string(b), `<a href="radio.radio">radio.radio</a>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Open_Dir_Memory_StripPrefix(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
|
|
|
|
err := MkdirAll("/testdata/public", 0755)
|
|
|
|
r.NoError(err)
|
|
|
|
|
|
|
|
dir, err := Open("/testdata/public")
|
|
|
|
r.NoError(err)
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.StripPrefix("/assets/", http.FileServer(dir)))
|
2019-08-09 04:51:58 +03:00
|
|
|
defer ts.Close()
|
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
res, err := http.Get(ts.URL + "/assets/radio.radio")
|
2019-08-09 04:51:58 +03:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
|
|
|
|
|
|
|
b, _ := ioutil.ReadAll(res.Body)
|
|
|
|
// r.NoError(err)
|
|
|
|
r.Contains(string(b), "I wanna bite the hand that feeds me")
|
|
|
|
|
2019-08-26 22:42:43 +03:00
|
|
|
res, err = http.Get(ts.URL + "/assets/")
|
2019-08-09 04:51:58 +03:00
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(200, res.StatusCode)
|
|
|
|
|
|
|
|
b, _ = ioutil.ReadAll(res.Body)
|
|
|
|
// r.NoError(err)
|
2019-08-09 22:30:12 +03:00
|
|
|
r.Contains(string(b), `<a href="/radio.radio">/radio.radio</a>`)
|
2019-08-26 22:42:43 +03:00
|
|
|
r.NotContains(string(b), `/public`)
|
|
|
|
r.NotContains(string(b), `//`)
|
2019-08-09 04:51:58 +03:00
|
|
|
}
|