1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

add tests for Echo.Static()

This commit is contained in:
pwli 2020-11-06 22:25:00 +08:00 committed by GitHub
parent d2b8a7fb45
commit 13374d1daa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -86,6 +86,14 @@ func TestEchoStatic(t *testing.T) {
e.ServeHTTP(rec, req)
assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/folder/", rec.HeaderMap["Location"][0])
// Directory Redirect with non-root path
e.Static("/static", "_fixture")
req = httptest.NewRequest(http.MethodGet, "/static", nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(http.StatusMovedPermanently, rec.Code)
assert.Equal("/static/", rec.HeaderMap["Location"][0])
// Directory with index.html
e.Static("/", "_fixture")
@ -100,6 +108,40 @@ func TestEchoStatic(t *testing.T) {
}
func TestEchoStaticRedirectIndex(t *testing.T) {
assert := assert.New(t)
e := New()
// HandlerFunc
e.Static("/static", "_fixture")
errCh := make(chan error)
go func() {
errCh <- e.Start("127.0.0.1:1323")
}()
time.Sleep(200 * time.Millisecond)
if resp, err := http.Get("http://127.0.0.1:1323/static"); err == nil {
defer resp.Body.Close()
assert.Equal(http.StatusOK, resp.StatusCode)
if body, err := ioutil.ReadAll(resp.Body); err == nil {
assert.Equal(true, strings.HasPrefix(string(body), "<!doctype html>"))
} else {
assert.Fail(err.Error())
}
} else {
assert.Fail(err.Error())
}
if err := e.Close(); err != nil {
t.Fatal(err)
}
}
func TestEchoFile(t *testing.T) {
e := New()
e.File("/walle", "_fixture/images/walle.png")