This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_test.go
129 lines (102 loc) · 2.93 KB
/
render_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
package congo
import (
"html/template"
"net/http"
"testing"
)
type AugmentedContext struct {
Context
}
func (ac *AugmentedContext) Augmented() string {
return "DAMN RIGHT!"
}
type MockResponseWriter struct {
StatusCode int
Content string
}
func (mw *MockResponseWriter) Header() http.Header {
return make(map[string][]string)
}
func (mw *MockResponseWriter) Write(b []byte) (int, error) {
if mw.StatusCode == 0 {
mw.WriteHeader(http.StatusOK)
}
mw.Content = mw.Content + string(b)
return len(b), nil
}
func (mw *MockResponseWriter) WriteHeader(statusCode int) {
mw.StatusCode = statusCode
}
func createBasicTemplates() *template.Template {
root, _ := template.New("layout").Parse("LAYOUT: {{.Content}}")
root.New("inner").Parse("INNER: HAI!")
return root
}
func ActionRenderTemplate(c Context) (Context, interface{}) {
return c, &RenderResponse{"inner", "layout"}
}
func createContextUsingTemplates() *template.Template {
root, _ := template.New("layout").Parse("LAYOUT: {{.Content}}")
root.New("inner").Parse("INNER: HAI! {{.Augmented}}")
return root
}
func ActionAugmentContextRenderTemplate(c Context) (Context, interface{}) {
aug := &AugmentedContext{c}
return aug, &RenderResponse{"inner", "layout"}
}
func ActionUndefinedTemplate(c Context) (Context, interface{}) {
return c, &RenderResponse{"undefined_inner", "layout"}
}
func ActionUndefinedLayout(c Context) (Context, interface{}) {
return c, &RenderResponse{"inner", "undefined_layout"}
}
func TestTemplateAndLayoutRender(t *testing.T) {
responseWriter := &MockResponseWriter{}
tpls := createBasicTemplates()
handler := NewHandler().SetTemplateStore(tpls)
handler.Actions(ActionRenderTemplate)
handlerFn := MuxHandler(handler)
handlerFn(responseWriter, nil)
expected := "LAYOUT: INNER: HAI!"
if responseWriter.Content != expected {
t.Fail()
}
}
func TestTemplateAndLayoutRenderWithAugmentedContext(t *testing.T) {
responseWriter := &MockResponseWriter{}
tpls := createContextUsingTemplates()
handler := NewHandler().SetTemplateStore(tpls)
handler.Actions(ActionAugmentContextRenderTemplate)
handlerFn := MuxHandler(handler)
handlerFn(responseWriter, nil)
expected := "LAYOUT: INNER: HAI! DAMN RIGHT!"
if responseWriter.Content != expected {
t.Fail()
}
}
func TestRenderingUndefinedTemplatePanics(t *testing.T) {
responseWriter := &MockResponseWriter{}
tpls := createBasicTemplates()
handler := NewHandler().SetTemplateStore(tpls)
handler.Actions(ActionUndefinedTemplate)
handlerFn := MuxHandler(handler)
defer func() {
if e := recover(); e == nil {
t.Fail()
}
}()
handlerFn(responseWriter, nil)
}
func TestRenderingUndefinedLayoutPanics(t *testing.T) {
responseWriter := &MockResponseWriter{}
tpls := createBasicTemplates()
handler := NewHandler().SetTemplateStore(tpls)
handler.Actions(ActionUndefinedLayout)
handlerFn := MuxHandler(handler)
defer func() {
if e := recover(); e == nil {
t.Fail()
}
}()
handlerFn(responseWriter, nil)
}