Skip to content

Commit 13374d1

Browse files
authored
add tests for Echo.Static()
1 parent d2b8a7f commit 13374d1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

echo_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ func TestEchoStatic(t *testing.T) {
8686
e.ServeHTTP(rec, req)
8787
assert.Equal(http.StatusMovedPermanently, rec.Code)
8888
assert.Equal("/folder/", rec.HeaderMap["Location"][0])
89+
90+
// Directory Redirect with non-root path
91+
e.Static("/static", "_fixture")
92+
req = httptest.NewRequest(http.MethodGet, "/static", nil)
93+
rec = httptest.NewRecorder()
94+
e.ServeHTTP(rec, req)
95+
assert.Equal(http.StatusMovedPermanently, rec.Code)
96+
assert.Equal("/static/", rec.HeaderMap["Location"][0])
8997

9098
// Directory with index.html
9199
e.Static("/", "_fixture")
@@ -100,6 +108,40 @@ func TestEchoStatic(t *testing.T) {
100108

101109
}
102110

111+
func TestEchoStaticRedirectIndex(t *testing.T) {
112+
assert := assert.New(t)
113+
e := New()
114+
115+
// HandlerFunc
116+
e.Static("/static", "_fixture")
117+
118+
errCh := make(chan error)
119+
120+
go func() {
121+
errCh <- e.Start("127.0.0.1:1323")
122+
}()
123+
124+
time.Sleep(200 * time.Millisecond)
125+
126+
if resp, err := http.Get("http://127.0.0.1:1323/static"); err == nil {
127+
defer resp.Body.Close()
128+
assert.Equal(http.StatusOK, resp.StatusCode)
129+
130+
if body, err := ioutil.ReadAll(resp.Body); err == nil {
131+
assert.Equal(true, strings.HasPrefix(string(body), "<!doctype html>"))
132+
} else {
133+
assert.Fail(err.Error())
134+
}
135+
136+
} else {
137+
assert.Fail(err.Error())
138+
}
139+
140+
if err := e.Close(); err != nil {
141+
t.Fatal(err)
142+
}
143+
}
144+
103145
func TestEchoFile(t *testing.T) {
104146
e := New()
105147
e.File("/walle", "_fixture/images/walle.png")

0 commit comments

Comments
 (0)