-
Notifications
You must be signed in to change notification settings - Fork 1
/
js_test.go
110 lines (99 loc) · 2.31 KB
/
js_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
package jaws
import (
"bytes"
"compress/gzip"
_ "embed"
"hash/fnv"
"net/url"
"strconv"
"strings"
"testing"
)
func Test_Javascript(t *testing.T) {
const prefix = "/jaws/.jaws."
const suffix = ".js"
th := newTestHelper(t)
h := fnv.New64a()
_, err := h.Write(JavascriptText)
th.NoErr(err)
th.Equal(JavascriptPath, prefix+strconv.FormatUint(h.Sum64(), 36)+suffix)
th.True(len(JavascriptText) > 0)
th.True(len(JavascriptGZip) > 0)
th.True(len(JavascriptGZip) < len(JavascriptText))
b := bytes.Buffer{}
gw := gzip.NewWriter(&b)
_, err = gw.Write(JavascriptText)
th.NoErr(err)
th.NoErr(gw.Close())
th.Equal(b.Bytes(), JavascriptGZip)
}
func Test_PreloadHTML(t *testing.T) {
const extraScript = "someExtraScript.js"
const extraStyle = "someExtraStyle.css"
const extraImage = "favicon.png"
const extraFont = "someExtraFont.woff2"
th := newTestHelper(t)
txt := PreloadHTML()
th.Equal(strings.Contains(txt, JavascriptPath), false)
th.Equal(strings.Count(txt, "<script>"), strings.Count(txt, "</script>"))
mustParseUrl := func(urlstr string) *url.URL {
u, err := url.Parse(urlstr)
if err != nil {
t.Fatal(err)
}
return u
}
txt = PreloadHTML(
mustParseUrl(JavascriptPath),
mustParseUrl(extraScript),
mustParseUrl(extraStyle),
mustParseUrl(extraImage),
mustParseUrl(extraFont))
th.Equal(strings.Contains(txt, JavascriptPath), true)
th.Equal(strings.Contains(txt, extraScript), true)
th.Equal(strings.Contains(txt, extraStyle), true)
th.Equal(strings.Contains(txt, extraImage), true)
th.Equal(strings.Contains(txt, extraFont), true)
th.Equal(strings.Count(txt, "<script>"), strings.Count(txt, "</script>"))
t.Log(txt)
}
func TestJawsKeyString(t *testing.T) {
th := newTestHelper(t)
th.Equal(JawsKeyString(0), "")
th.Equal(JawsKeyString(1), "1")
}
func TestJawsKeyValue(t *testing.T) {
tests := []struct {
name string
jawsKey string
want uint64
}{
{
name: "blank",
jawsKey: "",
want: 0,
},
{
name: "1",
jawsKey: "1",
want: 1,
},
{
name: "-1",
jawsKey: "-1",
want: 0,
},
{
name: "2/",
jawsKey: "2/",
want: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := JawsKeyValue(tt.jawsKey); got != tt.want {
t.Errorf("JawsKeyValue() = %v, want %v", got, tt.want)
}
})
}
}