-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_editor_test.go
86 lines (58 loc) · 3.23 KB
/
token_editor_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
package rewritehtml_test
import (
"bytes"
"github.com/simon-engledew/rewritehtml"
"github.com/stretchr/testify/require"
"io"
"strings"
"testing"
)
func testWrites(w io.WriteCloser, values ...string) error {
for _, value := range values {
_, err := io.WriteString(w, value)
if err != nil {
return err
}
}
return w.Close()
}
func TestMiss(t *testing.T) {
output := new(bytes.Buffer)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.ErrorIs(t, testWrites(editor, `<html><p></p><pre>`, `moose</pre></html>`), io.EOF)
require.Equal(t, `<html><p></p><pre>moose</pre></html>`, output.String())
}
func TestHit(t *testing.T) {
output := new(bytes.Buffer)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.NoError(t, testWrites(editor, `<html><head></head><pre>`, `moose</pre></html>`))
require.Equal(t, `<html><head><meta name="rewritehtml" content="true" /></head><pre>moose</pre></html>`, output.String())
}
func TestShortCircuitWrite(t *testing.T) {
output := new(bytes.Buffer)
zeros := strings.Repeat(`0`, 1024)
script := strings.Repeat(`var moose; `, 512)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.NoError(t, testWrites(editor, `<!DOCTYPE html><html><head>`, `<link rel="icon" type="image/png" href="data:image/png;base64,`+zeros+`</link></head><script>`+script+`</script>`, `<script>`+script+`</script>`))
require.Equal(t, `<!DOCTYPE html><html><head><meta name="rewritehtml" content="true" /><link rel="icon" type="image/png" href="data:image/png;base64,`+zeros+`</link></head><script>`+script+`</script><script>`+script+`</script>`, output.String())
}
func TestShortWrite(t *testing.T) {
output := new(bytes.Buffer)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.NoError(t, testWrites(editor, `<he`, `ad></head><pre>`, `moose</pre>`))
require.Equal(t, `<head><meta name="rewritehtml" content="true" /></head><pre>moose</pre>`, output.String())
}
func TestConcat(t *testing.T) {
output := new(bytes.Buffer)
zeros := strings.Repeat(`0`, 1024)
script := strings.Repeat(`var moose; `, 512)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.NoError(t, testWrites(editor, `<!DOCTYPE html><html><head><link rel="icon" type="image/png" href="data:image/png;base64,`+zeros, `</link></head><script>`+script+`</script>`))
require.Equal(t, `<!DOCTYPE html><html><head><meta name="rewritehtml" content="true" /><link rel="icon" type="image/png" href="data:image/png;base64,`+zeros+`</link></head><script>`+script+`</script>`, output.String())
}
func TestCDataWrite(t *testing.T) {
output := new(bytes.Buffer)
editor := rewritehtml.NewTokenEditor(output, rewritehtml.AfterHead(`<meta name="rewritehtml" content="true" />`))
require.NoError(t, testWrites(editor, `<script>`, `javascript {} <head></head>`, `moose</script><head></head>`))
require.Equal(t, `<script>javascript {} <head></head>moose</script><head><meta name="rewritehtml" content="true" /></head>`, output.String())
}