forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html_test.go
138 lines (103 loc) · 3.34 KB
/
html_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package fuego
import (
"embed"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
//go:embed testdata/*.html
var testdata embed.FS
func TestRender(t *testing.T) {
s := NewServer(
WithTemplateFS(testdata),
WithTemplateGlobs("testdata/*.html"),
)
Get(s, "/test", func(ctx *ContextNoBody) (CtxRenderer, error) {
return ctx.Render("testdata/test.html", H{"Name": "test"})
})
t.Run("Render once", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), "<main>\n <h1>Test</h1>\n <p>Your name is: test</p>\n</main>\n")
})
t.Run("Render twice", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, w.Body.String(), "<main>\n <h1>Test</h1>\n <p>Your name is: test</p>\n</main>\n")
})
t.Run("cannot parse unexisting file", func(t *testing.T) {
Get(s, "/file-not-found", func(ctx ContextNoBody) (CtxRenderer, error) {
return ctx.Render("testdata/not-found.html", H{"Name": "test"})
})
r := httptest.NewRequest(http.MethodGet, "/file-not-found", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
require.Equal(t, http.StatusInternalServerError, w.Code)
})
t.Run("can execute template with missing variable in map", func(t *testing.T) {
Get(s, "/impossible", func(ctx ContextNoBody) (CtxRenderer, error) {
return ctx.Render("testdata/test.html", H{"NotName": "test"})
})
r := httptest.NewRequest(http.MethodGet, "/impossible", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
t.Log(w.Body.String())
require.Equal(t, http.StatusOK, w.Code)
})
t.Run("cannot execute template with missing variable in struct", func(t *testing.T) {
Get(s, "/impossible-struct", func(ctx ContextNoBody) (CtxRenderer, error) {
return ctx.Render("testdata/test.html", struct{}{})
})
r := httptest.NewRequest(http.MethodGet, "/impossible-struct", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
t.Log(w.Body.String())
require.Equal(t, http.StatusOK, w.Code)
require.Contains(t, w.Body.String(), "error executing template")
})
}
func BenchmarkRender(b *testing.B) {
s := NewServer(
WithTemplateFS(testdata),
WithTemplateGlobs("testdata/*.html"),
)
Get(s, "/test", func(ctx ContextNoBody) (CtxRenderer, error) {
return ctx.Render("testdata/test.html", H{"Name": "test"})
})
expected := "<main>\n <h1>Test</h1>\n <p>Your name is: test</p>\n</main>\n"
for range b.N {
r := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, r)
if w.Code != http.StatusOK {
b.Fail()
}
if w.Body.String() != expected {
b.Fail()
}
}
}
func TestServer_loadTemplates(t *testing.T) {
s := NewServer(
WithTemplateFS(testdata),
)
t.Run("no template", func(t *testing.T) {
err := s.loadTemplates()
require.Error(t, err)
})
t.Run("template not found", func(t *testing.T) {
err := s.loadTemplates("testdata/not-found.html")
require.Error(t, err)
err = s.loadTemplates("notfound")
require.Error(t, err)
})
t.Run("template found", func(t *testing.T) {
err := s.loadTemplates("testdata/test.html")
require.NoError(t, err)
})
}