@@ -84,6 +84,22 @@ 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+ },
95+ { // `e.Static` is not meant to work by pointing `root` to file. This would be insecure.
96+ name : "nok, should not work when relative path for root points to file" ,
97+ givenPrefix : "/images" ,
98+ givenRoot : "./_fixture/images/walle.png" ,
99+ whenURL : "/images" ,
100+ expectStatus : http .StatusNotFound ,
101+ expectBodyStartsWith : "{\" message\" :\" Not Found\" }\n " ,
102+ },
87103 {
88104 name : "No file" ,
89105 givenPrefix : "/images" ,
@@ -246,11 +262,54 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
246262}
247263
248264func 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 )
265+ var testCases = []struct {
266+ name string
267+ givenPath string
268+ givenFile string
269+ whenPath string
270+ expectCode int
271+ expectStartsWith string
272+ }{
273+ {
274+ name : "ok" ,
275+ givenPath : "/walle" ,
276+ givenFile : "_fixture/images/walle.png" ,
277+ whenPath : "/walle" ,
278+ expectCode : http .StatusOK ,
279+ expectStartsWith : string ([]byte {0x89 , 0x50 , 0x4e }),
280+ },
281+ {
282+ name : "ok with relative path" ,
283+ givenPath : "/" ,
284+ givenFile : "./go.mod" ,
285+ whenPath : "/" ,
286+ expectCode : http .StatusOK ,
287+ expectStartsWith : "module github.com/labstack/echo/v" ,
288+ },
289+ {
290+ name : "nok file does not exist" ,
291+ givenPath : "/" ,
292+ givenFile : "./this-file-does-not-exist" ,
293+ whenPath : "/" ,
294+ expectCode : http .StatusNotFound ,
295+ expectStartsWith : "{\" message\" :\" Not Found\" }\n " ,
296+ },
297+ }
298+
299+ for _ , tc := range testCases {
300+ t .Run (tc .name , func (t * testing.T ) {
301+ e := New () // we are using echo.defaultFS instance
302+ e .File (tc .givenPath , tc .givenFile )
303+
304+ c , b := request (http .MethodGet , tc .whenPath , e )
305+ assert .Equal (t , tc .expectCode , c )
306+
307+ if len (b ) > len (tc .expectStartsWith ) {
308+ b = b [:len (tc .expectStartsWith )]
309+ }
310+ assert .Equal (t , tc .expectStartsWith , b )
311+ })
312+ }
254313}
255314
256315func TestEchoMiddleware (t * testing.T ) {
0 commit comments