forked from unrolled/render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_html_test.go
384 lines (314 loc) · 9.72 KB
/
render_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package render
import (
"errors"
"html/template"
"net/http"
"net/http/httptest"
"testing"
)
func TestHTMLBad(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "nope", nil)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNotNil(t, err)
expect(t, res.Code, 500)
expect(t, res.Body.String(), "html/template: \"nope\" is undefined\n")
}
func TestHTMLBadDisableHTTPErrorRendering(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
DisableHTTPErrorRendering: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "nope", nil)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNotNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Body.String(), "")
}
func TestHTMLBasic(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}
func TestHTMLXHTML(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
HTMLContentType: ContentXHTML,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentXHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}
func TestHTMLExtensions(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
Extensions: []string{".tmpl", ".html"},
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hypertext", nil)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "Hypertext!\n")
}
func TestHTMLFuncs(t *testing.T) {
render := New(Options{
Directory: "fixtures/custom_funcs",
Funcs: []template.FuncMap{
{
"myCustomFunc": func() string {
return "My custom function"
},
},
},
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "index", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Body.String(), "My custom function\n")
}
func TestRenderLayout(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
Layout: "layout",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "content", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Body.String(), "head\n<h1>gophers</h1>\n\nfoot\n")
}
func TestHTMLLayoutCurrent(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
Layout: "current_layout",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "content", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Body.String(), "content head\n<h1>gophers</h1>\n\ncontent foot\n")
}
func TestHTMLNested(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "admin/index", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Admin gophers</h1>\n")
}
func TestHTMLBadPath(t *testing.T) {
render := New(Options{
Directory: "../../../../../../../../../../../../../../../../fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNotNil(t, err)
expect(t, res.Code, 500)
}
func TestHTMLDelimiters(t *testing.T) {
render := New(Options{
Delims: Delims{"{[{", "}]}"},
Directory: "fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "delims", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>")
}
func TestHTMLDefaultCharset(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
// ContentLength should be deferred to the ResponseWriter and not Render
expect(t, res.Header().Get(ContentLength), "")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}
func TestHTMLOverrideLayout(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
Layout: "layout",
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "content", "gophers", HTMLOptions{
Layout: "another_layout",
})
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "another head\n<h1>gophers</h1>\n\nanother foot\n")
}
func TestHTMLNoRace(t *testing.T) {
// This test used to fail if run with -race
render := New(Options{
Directory: "fixtures/basic",
})
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := render.HTML(w, http.StatusOK, "hello", "gophers")
expectNil(t, err)
})
done := make(chan bool)
doreq := func() {
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
// ContentLength should be deferred to the ResponseWriter and not Render
expect(t, res.Header().Get(ContentLength), "")
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
done <- true
}
// Run two requests to check there is no race condition
go doreq()
go doreq()
<-done
<-done
}
func TestHTMLLoadFromAssets(t *testing.T) {
render := New(Options{
Asset: func(file string) ([]byte, error) {
switch file {
case "templates/test.tmpl":
return []byte("<h1>gophers</h1>\n"), nil
case "templates/layout.tmpl":
return []byte("head\n{{ yield }}\nfoot\n"), nil
default:
return nil, errors.New("file not found: " + file)
}
},
AssetNames: func() []string {
return []string{"templates/test.tmpl", "templates/layout.tmpl"}
},
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "test", "gophers", HTMLOptions{
Layout: "layout",
})
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "head\n<h1>gophers</h1>\n\nfoot\n")
}
func TestCompileTemplatesFromDir(t *testing.T) {
baseDir := "fixtures/template-dir-test"
fname0Rel := "0"
fname1Rel := "subdir/1"
fnameShouldParsedRel := "dedicated.tmpl/notbad"
dirShouldNotParsedRel := "dedicated"
r := New(Options{
Directory: baseDir,
Extensions: []string{".tmpl", ".html"},
})
r.compileTemplatesFromDir()
expect(t, r.TemplateLookup(fname1Rel) != nil, true)
expect(t, r.TemplateLookup(fname0Rel) != nil, true)
expect(t, r.TemplateLookup(fnameShouldParsedRel) != nil, true)
expect(t, r.TemplateLookup(dirShouldNotParsedRel) == nil, true)
}
func TestHTMLDisabledCharset(t *testing.T) {
render := New(Options{
Directory: "fixtures/basic",
DisableCharset: true,
})
var err error
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = render.HTML(w, http.StatusOK, "hello", "gophers")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
h.ServeHTTP(res, req)
expectNil(t, err)
expect(t, res.Code, http.StatusOK)
expect(t, res.Header().Get(ContentType), ContentHTML)
expect(t, res.Body.String(), "<h1>Hello gophers</h1>\n")
}