Skip to content

Commit 0482cb3

Browse files
authored
Merge pull request #1671 from pwli0755/fix-conflict
Fix Static files route not working
2 parents 4602335 + 2374af4 commit 0482cb3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

echo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ func (common) static(prefix, root string, get func(string, HandlerFunc, ...Middl
500500
}
501501
return c.File(name)
502502
}
503+
get(prefix, h)
503504
if prefix == "/" {
504505
return get(prefix+"*", h)
505506
}

echo_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ func TestEchoStatic(t *testing.T) {
102102
expectHeaderLocation: "/folder/",
103103
expectBodyStartsWith: "",
104104
},
105+
{
106+
name: "Directory Redirect with non-root path",
107+
givenPrefix: "/static",
108+
givenRoot: "_fixture",
109+
whenURL: "/static",
110+
expectStatus: http.StatusMovedPermanently,
111+
expectHeaderLocation: "/static/",
112+
expectBodyStartsWith: "",
113+
},
105114
{
106115
name: "Directory with index.html",
107116
givenPrefix: "/",
@@ -161,6 +170,40 @@ func TestEchoStatic(t *testing.T) {
161170
}
162171
}
163172

173+
func TestEchoStaticRedirectIndex(t *testing.T) {
174+
assert := assert.New(t)
175+
e := New()
176+
177+
// HandlerFunc
178+
e.Static("/static", "_fixture")
179+
180+
errCh := make(chan error)
181+
182+
go func() {
183+
errCh <- e.Start("127.0.0.1:1323")
184+
}()
185+
186+
time.Sleep(200 * time.Millisecond)
187+
188+
if resp, err := http.Get("http://127.0.0.1:1323/static"); err == nil {
189+
defer resp.Body.Close()
190+
assert.Equal(http.StatusOK, resp.StatusCode)
191+
192+
if body, err := ioutil.ReadAll(resp.Body); err == nil {
193+
assert.Equal(true, strings.HasPrefix(string(body), "<!doctype html>"))
194+
} else {
195+
assert.Fail(err.Error())
196+
}
197+
198+
} else {
199+
assert.Fail(err.Error())
200+
}
201+
202+
if err := e.Close(); err != nil {
203+
t.Fatal(err)
204+
}
205+
}
206+
164207
func TestEchoFile(t *testing.T) {
165208
e := New()
166209
e.File("/walle", "_fixture/images/walle.png")

0 commit comments

Comments
 (0)