-
Notifications
You must be signed in to change notification settings - Fork 15
/
render_test.go
80 lines (61 loc) · 2.03 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
package render
import (
"io/ioutil"
"regexp"
"testing"
"net/http/httptest"
"net/textproto"
)
func TestExecute(t *testing.T) {
Render := New(nil, "test")
request := httptest.NewRequest("GET", "/test", nil)
responseWriter := httptest.NewRecorder()
var context interface{}
tmpl := Render.Layout("layout_for_test")
tmpl.Execute("test", context, request, responseWriter)
if textproto.TrimString(responseWriter.Body.String()) != "Template for test" {
t.Errorf("The template isn't rendered")
}
}
func TestErrorMessageWhenMissingLayout(t *testing.T) {
Render := New(nil, "test")
request := httptest.NewRequest("GET", "/test", nil)
responseWriter := httptest.NewRecorder()
var context interface{}
nonExistLayout := "ThePlant"
tmpl := Render.Layout(nonExistLayout)
err := tmpl.Execute("test", context, request, responseWriter)
if err != nil {
t.Error("we don't return error, we render the error on page instead")
}
bodyBytes, err1 := ioutil.ReadAll(responseWriter.Result().Body)
if err1 != nil {
t.Fatal(err1)
}
bodyString := string(bodyBytes)
errorRegexp := "Failed to render page.+" + nonExistLayout + ".*"
if matched, _ := regexp.MatchString(errorRegexp, bodyString); !matched {
t.Errorf("Missing layout error message is incorrect")
}
}
func TestErrorMessageWhenLayoutContainsError(t *testing.T) {
Render := New(nil, "test")
request := httptest.NewRequest("GET", "/test", nil)
responseWriter := httptest.NewRecorder()
var context interface{}
layoutContainsError := "layout_contains_error"
tmpl := Render.Layout(layoutContainsError)
err := tmpl.Execute("test", context, request, responseWriter)
if err != nil {
t.Error("we don't return error, we render the error on page instead")
}
bodyBytes, err1 := ioutil.ReadAll(responseWriter.Result().Body)
if err1 != nil {
t.Fatal(err1)
}
bodyString := string(bodyBytes)
errorRegexp := "Failed to render page.+" + layoutContainsError + ".*"
if matched, _ := regexp.MatchString(errorRegexp, bodyString); !matched {
t.Errorf("Missing layout error message is incorrect")
}
}