-
Notifications
You must be signed in to change notification settings - Fork 1
/
rewrite_test.go
196 lines (167 loc) · 4.89 KB
/
rewrite_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
package rewrite
import (
"bytes"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
"io/ioutil"
"regexp"
"strings"
"testing"
)
func validHTML(t *testing.T, s string) {
nodes, err := html.ParseFragment(strings.NewReader(s), nil)
if err != nil {
t.Fatalf("non valid html: %v", err)
}
for _, n := range nodes {
if err := html.Render(ioutil.Discard, n); err != nil {
t.Fatalf("non valid html: %v", err)
}
}
}
const BaseHTMLTemplate = `
<html>
<head>
</head>
<body>
<div id="content">
%s
</div>
</body>
</html>
`
const TestNode = `<p>Very Cool</p>`
func stdLibIdBasedTests(t *testing.T) {
t.Run("Set", func(t *testing.T) {
createdPNode := `<p id="time">Maybe this time.</p>`
initialHTML := fmt.Sprintf(BaseHTMLTemplate, TestNode)
w, err := Load(strings.NewReader(initialHTML))
assert.Nil(t, err)
assert.NotNil(t, w)
err = w.Set("id=content", createdPNode)
assert.Nil(t, err)
newHtml := w.String()
assert.NotContains(t, newHtml, TestNode)
assert.Contains(t, newHtml, createdPNode)
})
t.Run("Append", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, "")
w, err := Load(strings.NewReader(initialHTML))
assert.Nil(t, err)
assert.NotNil(t, w)
amount := 3
for i := 0; i < amount; i++ {
err = w.Append("id=content", TestNode)
assert.Nil(t, err)
}
newHTML := w.String()
re := regexp.MustCompile(TestNode)
matches := re.FindAllString(newHTML, -1)
assert.Equal(t, len(matches), amount)
})
}
const DivsWithClasses = `
<div class="great-name"></div>
<div class="great-name second-great-name"></div>
<div id="good stuff" class="great-name"></div>
`
const DivsWithClassesAndContent = `
<div class="great-name">
<h3>the best place</h3>
</div>
<div class="great-name second-great-name">
<h3>to go</h3>
</div>
<div id="good stuff" class="great-name">
<h3>shopping</h3>
</div>
`
func stdLibClassBasedTests(t *testing.T) {
t.Run("Set", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, DivsWithClasses)
w, err := Load(strings.NewReader(initialHTML))
assert.Nil(t, err)
assert.NotNil(t, w)
injectedPNode := `<p>Great Content Also!</p>`
err = w.Set("class=great-name", injectedPNode)
assert.Nil(t, err)
newHtmlString := w.String()
assert.NotNil(t, newHtmlString)
re := regexp.MustCompile(injectedPNode)
matches := re.FindAllString(newHtmlString, -1)
assert.Equal(t, len(matches), 3)
})
t.Run("Append", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, DivsWithClassesAndContent)
w, err := Load(strings.NewReader(initialHTML))
assert.Nil(t, err)
assert.NotNil(t, w)
injectedPNode := `<p>Come visit Kenyon Lev Hadera</p>`
err = w.Append("class=great-name", injectedPNode)
assert.Nil(t, err)
newHtmlString := w.String()
assert.NotNil(t, newHtmlString)
re := regexp.MustCompile(injectedPNode)
matches := re.FindAllString(newHtmlString, -1)
assert.Equal(t, len(matches), 3)
assert.Contains(t, newHtmlString, "the best place")
assert.Contains(t, newHtmlString, "to go")
assert.Contains(t, newHtmlString, "shopping")
})
}
func streamIdBasedTests(t *testing.T) {
t.Run("Set", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, `<p id="time">Maybe this time.</p>`)
outputHTML := &bytes.Buffer{}
err := Set(strings.NewReader(initialHTML), outputHTML, "id=time", "This time for sure.")
assert.Nil(t, err)
output := outputHTML.String()
validHTML(t, output)
assert.Contains(t, output, "This time for sure")
})
t.Run("Append", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, `<p>Maybe this time.</p>`)
outputHTML := &bytes.Buffer{}
appendedValue := "<p>Another one</p>"
err := Set(strings.NewReader(initialHTML), outputHTML, "id=content", appendedValue)
assert.Nil(t, err)
output := outputHTML.String()
validHTML(t, output)
assert.Contains(t, output, appendedValue)
})
}
func streamTagBasedTests(t *testing.T) {
t.Run("Append", func(t *testing.T) {
initialHTML := fmt.Sprintf(BaseHTMLTemplate, "")
outputHTML := &bytes.Buffer{}
injectedValue := "<script>alert(1)</script>"
err := Append(strings.NewReader(initialHTML), outputHTML, "tag=head", injectedValue)
assert.Nil(t, err)
output := outputHTML.String()
validHTML(t, output)
assert.Contains(t, output, injectedValue)
})
}
type ErrReader struct{ Error error }
func (e *ErrReader) Read([]byte) (int, error) {
return 0, e.Error
}
func TestHtmlMutations(t *testing.T) {
t.Run("std lib based", func(t *testing.T) {
t.Run("bad inputs", func(t *testing.T) {
t.Run("err reader", func(t *testing.T) {
r := &ErrReader{Error: errors.New("random failure")}
_, err := Load(r)
assert.NotNil(t, err)
})
})
t.Run("id based tests", stdLibIdBasedTests)
t.Run("class based tests", stdLibClassBasedTests)
})
t.Run("stream based", func(t *testing.T) {
t.Run("id based tests", streamIdBasedTests)
t.Run("tag based tests", streamTagBasedTests)
})
}