forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_test.go
70 lines (60 loc) · 1.68 KB
/
static_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestStatic(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
config := StaticConfig{
Root: "../_fixture",
}
// Directory
h := StaticWithConfig(config)(echo.NotFoundHandler)
assert := assert.New(t)
if assert.NoError(h(c)) {
assert.Contains(rec.Body.String(), "Echo")
}
// File found
req = httptest.NewRequest(http.MethodGet, "/images/walle.png", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
if assert.NoError(h(c)) {
assert.Equal(http.StatusOK, rec.Code)
assert.Equal(rec.Header().Get(echo.HeaderContentLength), "219885")
}
// File not found
req = httptest.NewRequest(http.MethodGet, "/none", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
he := h(c).(*echo.HTTPError)
assert.Equal(http.StatusNotFound, he.Code)
// HTML5
req = httptest.NewRequest(http.MethodGet, "/random", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.HTML5 = true
static := StaticWithConfig(config)
h = static(echo.NotFoundHandler)
if assert.NoError(h(c)) {
assert.Equal(http.StatusOK, rec.Code)
assert.Contains(rec.Body.String(), "Echo")
}
// Browse
req = httptest.NewRequest(http.MethodGet, "/", nil)
rec = httptest.NewRecorder()
c = e.NewContext(req, rec)
config.Root = "../_fixture/certs"
config.Browse = true
static = StaticWithConfig(config)
h = static(echo.NotFoundHandler)
if assert.NoError(h(c)) {
assert.Equal(http.StatusOK, rec.Code)
assert.Contains(rec.Body.String(), "cert.pem")
}
}