This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
grender_test.go
130 lines (103 loc) · 3.21 KB
/
grender_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
package grender
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type Greeting struct {
One string `json:"one"`
Two string `json:"two"`
}
func TestGrenderDefaultCharset(t *testing.T) {
r := New(Options{})
if r.Options.Charset != DefaultCharset {
t.Errorf("invalid default charset: expected %#v, got %#v", DefaultCharset, r.Options.Charset)
}
}
func TestGrenderJSON(t *testing.T) {
render := New(Options{
Charset: "ASCII",
})
w := httptest.NewRecorder()
err := render.JSON(w, 299, Greeting{"hello", "world"})
res := w.Result()
if err != nil {
t.Errorf("expected %#v, got %#v", nil, err)
}
if res.StatusCode != 299 {
t.Errorf("invalid status code: expected %#v, got %#v", 299, res.StatusCode)
}
e := ContentJSON + "; charset=" + render.Options.Charset
if v := res.Header.Get(ContentType); v != e {
t.Errorf("invalid content type: expected %#v, got %#v", e, v)
}
body, _ := ioutil.ReadAll(res.Body)
if v := string(body); v != "{\"one\":\"hello\",\"two\":\"world\"}\n" {
t.Errorf("invalid response body: expected %#v, got %#v", "{\"one\":\"hello\",\"two\":\"world\"}\n", v)
}
}
func TestGrenderHTML(t *testing.T) {
render := New(Options{
TemplatesGlob: "examples/*.tmpl",
})
w := httptest.NewRecorder()
err := render.HTML(w, http.StatusOK, "hello.tmpl", "world")
res := w.Result()
if err != nil {
t.Errorf("expected %#v, got %#v", nil, err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("invalid status code: expected %#v, got %#v", http.StatusOK, res.StatusCode)
}
e := ContentHTML + "; charset=" + render.Options.Charset
if v := res.Header.Get(ContentType); v != e {
t.Errorf("invalid content type: expected %#v, got %#v", e, v)
}
body, _ := ioutil.ReadAll(res.Body)
if v := string(body); v != "Hello world!\n" {
t.Errorf("invalid body: expected %#v, got %#v", "Hello world!\n", v)
}
}
func TestGrenderXML(t *testing.T) {
render := New()
w := httptest.NewRecorder()
err := render.XML(w, 299, Greeting{"hello", "world"})
res := w.Result()
if err != nil {
t.Errorf("expected %#v, got %#v", nil, err)
}
if res.StatusCode != 299 {
t.Errorf("invalid status code: expected %#v, got %#v", 299, res.StatusCode)
}
e := ContentXML + "; charset=" + render.Options.Charset
if v := res.Header.Get(ContentType); v != e {
t.Errorf("invalid content type: expected %#v, got %#v", e, v)
}
body, _ := ioutil.ReadAll(res.Body)
expected := "<Greeting><One>hello</One><Two>world</Two></Greeting>"
if v := string(body); v != expected {
t.Errorf("invalid response body: expected %#v, got %#v", expected, v)
}
}
func TestGrenderText(t *testing.T) {
render := New()
w := httptest.NewRecorder()
err := render.Text(w, 200, "Hello world!")
res := w.Result()
if err != nil {
t.Errorf("expected %#v, got %#v", nil, err)
}
if res.StatusCode != 200 {
t.Errorf("invalid status code: expected %#v, got %#v", 200, res.StatusCode)
}
e := ContentText + "; charset=" + render.Options.Charset
if v := res.Header.Get(ContentType); v != e {
t.Errorf("invalid content type: expected %#v, got %#v", e, v)
}
body, _ := ioutil.ReadAll(res.Body)
expected := "Hello world!"
if v := string(body); v != expected {
t.Errorf("invalid response body: expected %#v, got %#v", expected, v)
}
}