This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer_test.go
118 lines (105 loc) · 3.49 KB
/
renderer_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
package template_test
import (
"io/ioutil"
"strings"
"testing"
"github.com/lpar/template"
)
func testRender(rdr template.Renderer, tmplname string, data interface{}) (string, error) {
var result strings.Builder
err := rdr.Execute("test", &result, tmplname, data)
return result.String(), err
}
const expectedButton = `<button class=ds-button>Submit</button>`
const expectedPage = `<!doctype html><html lang=en><meta charset=utf-8><title>Hello world</title><button class=ds-button>Submit</button>`
const expectedCSS = `button{border:solid red 3px;min-width:10em}`
const expectedJS = `function nth(o){return o+(['st','nd','rd'][(o+'').match(/1?\d\b/)-1]||'th');}`
const expectedSafety = `<p>Written by <script>alert('You have been pwned');</script>.`
func TestRenderHTML(t *testing.T) {
rdr := template.NewRenderer("testdata")
rdr.Minify = true
err := rdr.Load("test", "*", "subdir/*")
if err != nil {
t.Fatal(err)
}
btn, err := testRender(rdr, "subdir/button.html", nil)
if err != nil {
t.Errorf("button.html render failed: %v", err)
}
if btn != expectedButton {
t.Errorf("button.html render failed, expected '%s' got '%s'", expectedButton, btn)
}
html, err := testRender(rdr, "index.html", "Hello world")
if err != nil {
t.Errorf("index.html render failed: %v", err)
}
if html != expectedPage {
t.Errorf("index.html render failed, expected '%s' got '%s'", expectedPage, html)
}
}
func TestRenderSafety(t *testing.T) {
rdr := template.NewRenderer("testdata")
rdr.Minify = true
err := rdr.Load("test", "*", "subdir/*")
html, err := testRender(rdr, "footer.html", "<script>alert('You have been pwned');</script>")
if err != nil {
t.Errorf("footer.html render with injected tags failed: %v", err)
}
if html != expectedSafety {
t.Errorf("footer.html render with injected tags failed, expected '%s' got '%s'", expectedSafety, html)
}
}
func TestRenderCSS(t *testing.T) {
rdr := template.NewRenderer("testdata")
rdr.Minify = true
err := rdr.Load("test", "*", "subdir/*")
css, err := testRender(rdr, "button.css", nil)
if err != nil {
t.Errorf("button.css render failed: %v", err)
}
if css != expectedCSS {
t.Errorf("button.css render failed, expected '%s' got '%s'", expectedCSS, css)
}
}
func TestRenderJS(t *testing.T) {
rdr := template.NewRenderer("testdata")
rdr.Minify = true
err := rdr.Load("test", "*", "subdir/*")
js, err := testRender(rdr, "nth.js", nil)
if err != nil {
t.Errorf("nth.js render failed: %v", err)
}
if js != expectedJS {
t.Errorf("nth.js render failed, expected '%s' got '%s'", expectedJS, js)
}
}
const TMP1 = "<button class=\"btn ds-button\">Primary</button>"
const TMP2 = "<a class=\"btn ds-button\">Secondary</a>"
func TestReload(t *testing.T) {
rdr := template.NewRenderer("testdata")
rdr.Minify = true
err := rdr.Load("test", "*", "subdir/*")
rdr.Live = true
err = ioutil.WriteFile("testdata/reload.html", []byte(TMP1), 0644)
if err != nil {
t.Errorf("reload test failed: %v", err)
}
html, err := testRender(rdr, "reload.html", nil)
if err != nil {
t.Errorf("reload test render failed: %v", err)
}
if html != TMP1 {
t.Errorf("reload test part 1 failed, expected %s got %s", TMP1, html)
}
err = ioutil.WriteFile("testdata/reload.html", []byte(TMP2), 0644)
if err != nil {
t.Errorf("reload test failed: %v", err)
}
html, err = testRender(rdr, "reload.html", nil)
if err != nil {
t.Errorf("reload test render failed: %v", err)
}
if html != TMP2 {
t.Errorf("reload test part 2 failed, expected %s got %s", TMP2, html)
}
}