@@ -84,6 +84,14 @@ func TestEchoStatic(t *testing.T) {
8484 expectStatus : http .StatusOK ,
8585 expectBodyStartsWith : string ([]byte {0x89 , 0x50 , 0x4e , 0x47 }),
8686 },
87+ {
88+ name : "ok with relative path for root points to directory" ,
89+ givenPrefix : "/images" ,
90+ givenRoot : "./_fixture/images" ,
91+ whenURL : "/images/walle.png" ,
92+ expectStatus : http .StatusOK ,
93+ expectBodyStartsWith : string ([]byte {0x89 , 0x50 , 0x4e , 0x47 }),
94+ },
8795 {
8896 name : "No file" ,
8997 givenPrefix : "/images" ,
@@ -246,11 +254,54 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
246254}
247255
248256func TestEchoFile (t * testing.T ) {
249- e := New ()
250- e .File ("/walle" , "_fixture/images/walle.png" )
251- c , b := request (http .MethodGet , "/walle" , e )
252- assert .Equal (t , http .StatusOK , c )
253- assert .NotEmpty (t , b )
257+ var testCases = []struct {
258+ name string
259+ givenPath string
260+ givenFile string
261+ whenPath string
262+ expectCode int
263+ expectStartsWith string
264+ }{
265+ {
266+ name : "ok" ,
267+ givenPath : "/walle" ,
268+ givenFile : "_fixture/images/walle.png" ,
269+ whenPath : "/walle" ,
270+ expectCode : http .StatusOK ,
271+ expectStartsWith : string ([]byte {0x89 , 0x50 , 0x4e }),
272+ },
273+ {
274+ name : "ok with relative path" ,
275+ givenPath : "/" ,
276+ givenFile : "./go.mod" ,
277+ whenPath : "/" ,
278+ expectCode : http .StatusOK ,
279+ expectStartsWith : "module github.com/labstack/echo/v" ,
280+ },
281+ {
282+ name : "nok file does not exist" ,
283+ givenPath : "/" ,
284+ givenFile : "./this-file-does-not-exist" ,
285+ whenPath : "/" ,
286+ expectCode : http .StatusNotFound ,
287+ expectStartsWith : "{\" message\" :\" Not Found\" }\n " ,
288+ },
289+ }
290+
291+ for _ , tc := range testCases {
292+ t .Run (tc .name , func (t * testing.T ) {
293+ e := New () // we are using echo.defaultFS instance
294+ e .File (tc .givenPath , tc .givenFile )
295+
296+ c , b := request (http .MethodGet , tc .whenPath , e )
297+ assert .Equal (t , tc .expectCode , c )
298+
299+ if len (b ) > len (tc .expectStartsWith ) {
300+ b = b [:len (tc .expectStartsWith )]
301+ }
302+ assert .Equal (t , tc .expectStartsWith , b )
303+ })
304+ }
254305}
255306
256307func TestEchoMiddleware (t * testing.T ) {
0 commit comments