|
1 | 1 | package middleware
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/labstack/echo/v4" |
| 5 | + "github.com/stretchr/testify/assert" |
4 | 6 | "net/http"
|
5 | 7 | "net/http/httptest"
|
6 |
| - "path/filepath" |
7 | 8 | "testing"
|
8 |
| - |
9 |
| - "github.com/labstack/echo/v4" |
10 |
| - "github.com/stretchr/testify/assert" |
11 | 9 | )
|
12 | 10 |
|
13 | 11 | func TestStatic(t *testing.T) {
|
14 |
| - e := echo.New() |
15 |
| - req := httptest.NewRequest(http.MethodGet, "/", nil) |
16 |
| - rec := httptest.NewRecorder() |
17 |
| - c := e.NewContext(req, rec) |
18 |
| - config := StaticConfig{ |
19 |
| - Root: "../_fixture", |
| 12 | + var testCases = []struct { |
| 13 | + name string |
| 14 | + givenConfig *StaticConfig |
| 15 | + givenAttachedToGroup string |
| 16 | + whenURL string |
| 17 | + expectContains string |
| 18 | + expectLength string |
| 19 | + expectCode int |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "ok, serve index with Echo message", |
| 23 | + whenURL: "/", |
| 24 | + expectCode: http.StatusOK, |
| 25 | + expectContains: "<title>Echo</title>", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "ok, serve file from subdirectory", |
| 29 | + whenURL: "/images/walle.png", |
| 30 | + expectCode: http.StatusOK, |
| 31 | + expectLength: "219885", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "ok, when html5 mode serve index for any static file that does not exist", |
| 35 | + givenConfig: &StaticConfig{ |
| 36 | + Root: "../_fixture", |
| 37 | + HTML5: true, |
| 38 | + }, |
| 39 | + whenURL: "/random", |
| 40 | + expectCode: http.StatusOK, |
| 41 | + expectContains: "<title>Echo</title>", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "ok, serve index as directory index listing files directory", |
| 45 | + givenConfig: &StaticConfig{ |
| 46 | + Root: "../_fixture/certs", |
| 47 | + Browse: true, |
| 48 | + }, |
| 49 | + whenURL: "/", |
| 50 | + expectCode: http.StatusOK, |
| 51 | + expectContains: "cert.pem", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "ok, serve directory index with IgnoreBase and browse", |
| 55 | + givenConfig: &StaticConfig{ |
| 56 | + Root: "../_fixture/_fixture/", // <-- last `_fixture/` is overlapping with group path and needs to be ignored |
| 57 | + IgnoreBase: true, |
| 58 | + Browse: true, |
| 59 | + }, |
| 60 | + givenAttachedToGroup: "/_fixture", |
| 61 | + whenURL: "/_fixture/", |
| 62 | + expectCode: http.StatusOK, |
| 63 | + expectContains: `<a class="file" href="README.md">README.md</a>`, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "ok, serve file with IgnoreBase", |
| 67 | + givenConfig: &StaticConfig{ |
| 68 | + Root: "../_fixture/_fixture/", // <-- last `_fixture/` is overlapping with group path and needs to be ignored |
| 69 | + IgnoreBase: true, |
| 70 | + Browse: true, |
| 71 | + }, |
| 72 | + givenAttachedToGroup: "/_fixture", |
| 73 | + whenURL: "/_fixture/README.md", |
| 74 | + expectCode: http.StatusOK, |
| 75 | + expectContains: "This directory is used for the static middleware test", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "nok, file not found", |
| 79 | + whenURL: "/none", |
| 80 | + expectCode: http.StatusNotFound, |
| 81 | + expectContains: "{\"message\":\"Not Found\"}\n", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "nok, do not allow directory traversal (backslash - windows separator)", |
| 85 | + whenURL: `/..\\middleware/basic_auth.go`, |
| 86 | + expectCode: http.StatusNotFound, |
| 87 | + expectContains: "{\"message\":\"Not Found\"}\n", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "nok,do not allow directory traversal (slash - unix separator)", |
| 91 | + whenURL: `/../middleware/basic_auth.go`, |
| 92 | + expectCode: http.StatusNotFound, |
| 93 | + expectContains: "{\"message\":\"Not Found\"}\n", |
| 94 | + }, |
20 | 95 | }
|
21 | 96 |
|
22 |
| - // Directory |
23 |
| - h := StaticWithConfig(config)(echo.NotFoundHandler) |
24 |
| - |
25 |
| - assert := assert.New(t) |
26 |
| - |
27 |
| - if assert.NoError(h(c)) { |
28 |
| - assert.Contains(rec.Body.String(), "Echo") |
29 |
| - } |
| 97 | + for _, tc := range testCases { |
| 98 | + t.Run(tc.name, func(t *testing.T) { |
| 99 | + e := echo.New() |
30 | 100 |
|
31 |
| - // File found |
32 |
| - req = httptest.NewRequest(http.MethodGet, "/images/walle.png", nil) |
33 |
| - rec = httptest.NewRecorder() |
34 |
| - c = e.NewContext(req, rec) |
35 |
| - if assert.NoError(h(c)) { |
36 |
| - assert.Equal(http.StatusOK, rec.Code) |
37 |
| - assert.Equal(rec.Header().Get(echo.HeaderContentLength), "219885") |
38 |
| - } |
| 101 | + config := StaticConfig{Root: "../_fixture"} |
| 102 | + if tc.givenConfig != nil { |
| 103 | + config = *tc.givenConfig |
| 104 | + } |
| 105 | + middlewareFunc := StaticWithConfig(config) |
| 106 | + if tc.givenAttachedToGroup != "" { |
| 107 | + // middleware is attached to group |
| 108 | + subGroup := e.Group(tc.givenAttachedToGroup, middlewareFunc) |
| 109 | + // group without http handlers (routes) does not do anything. |
| 110 | + // Request is matched against http handlers (routes) that have group middleware attached to them |
| 111 | + subGroup.GET("", echo.NotFoundHandler) |
| 112 | + subGroup.GET("/*", echo.NotFoundHandler) |
| 113 | + } else { |
| 114 | + // middleware is on root level |
| 115 | + e.Use(middlewareFunc) |
| 116 | + } |
39 | 117 |
|
40 |
| - // File not found |
41 |
| - req = httptest.NewRequest(http.MethodGet, "/none", nil) |
42 |
| - rec = httptest.NewRecorder() |
43 |
| - c = e.NewContext(req, rec) |
44 |
| - he := h(c).(*echo.HTTPError) |
45 |
| - assert.Equal(http.StatusNotFound, he.Code) |
| 118 | + req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil) |
| 119 | + rec := httptest.NewRecorder() |
46 | 120 |
|
47 |
| - // HTML5 |
48 |
| - req = httptest.NewRequest(http.MethodGet, "/random", nil) |
49 |
| - rec = httptest.NewRecorder() |
50 |
| - c = e.NewContext(req, rec) |
51 |
| - config.HTML5 = true |
52 |
| - static := StaticWithConfig(config) |
53 |
| - h = static(echo.NotFoundHandler) |
54 |
| - if assert.NoError(h(c)) { |
55 |
| - assert.Equal(http.StatusOK, rec.Code) |
56 |
| - assert.Contains(rec.Body.String(), "Echo") |
57 |
| - } |
| 121 | + e.ServeHTTP(rec, req) |
58 | 122 |
|
59 |
| - // Browse |
60 |
| - req = httptest.NewRequest(http.MethodGet, "/", nil) |
61 |
| - rec = httptest.NewRecorder() |
62 |
| - c = e.NewContext(req, rec) |
63 |
| - config.Root = "../_fixture/certs" |
64 |
| - config.Browse = true |
65 |
| - static = StaticWithConfig(config) |
66 |
| - h = static(echo.NotFoundHandler) |
67 |
| - if assert.NoError(h(c)) { |
68 |
| - assert.Equal(http.StatusOK, rec.Code) |
69 |
| - assert.Contains(rec.Body.String(), "cert.pem") |
| 123 | + assert.Equal(t, tc.expectCode, rec.Code) |
| 124 | + if tc.expectContains != "" { |
| 125 | + responseBody := rec.Body.String() |
| 126 | + assert.Contains(t, responseBody, tc.expectContains) |
| 127 | + } |
| 128 | + if tc.expectLength != "" { |
| 129 | + assert.Equal(t, rec.Header().Get(echo.HeaderContentLength), tc.expectLength) |
| 130 | + } |
| 131 | + }) |
70 | 132 | }
|
71 |
| - |
72 |
| - // IgnoreBase |
73 |
| - req = httptest.NewRequest(http.MethodGet, "/_fixture", nil) |
74 |
| - rec = httptest.NewRecorder() |
75 |
| - config.Root = "../_fixture" |
76 |
| - config.IgnoreBase = true |
77 |
| - static = StaticWithConfig(config) |
78 |
| - c.Echo().Group("_fixture", static) |
79 |
| - e.ServeHTTP(rec, req) |
80 |
| - |
81 |
| - assert.Equal(http.StatusOK, rec.Code) |
82 |
| - assert.Equal(rec.Header().Get(echo.HeaderContentLength), "122") |
83 |
| - |
84 |
| - req = httptest.NewRequest(http.MethodGet, "/_fixture", nil) |
85 |
| - rec = httptest.NewRecorder() |
86 |
| - config.Root = "../_fixture" |
87 |
| - config.IgnoreBase = false |
88 |
| - static = StaticWithConfig(config) |
89 |
| - c.Echo().Group("_fixture", static) |
90 |
| - e.ServeHTTP(rec, req) |
91 |
| - |
92 |
| - assert.Equal(http.StatusOK, rec.Code) |
93 |
| - assert.Contains(rec.Body.String(), filepath.Join("..", "_fixture", "_fixture")) |
94 | 133 | }
|
0 commit comments