-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
106 lines (93 loc) · 2.35 KB
/
server_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
package main
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
// Mock server
func TestServer(t *testing.T) {
firstPage := `
<!DOCTYPE html>
<html lang="en>"
<head>
<meta charset=utf-8">
<title>First TestPage</title>
</head>
<body>
The quick brown fox jumped over the lazy dog's parallelogram, er, tarantulas
<a href="anotherpage">another page</a>
</body>
</html>
`
anotherPage := `
<!DOCTYPE html>
<html lang="en>"
<head>
<meta charset=utf-8">
<title>First TestPage</title>
</head>
<body>
A parallelogram is a really cool shape! No kidding, a parallelogram.
But tarantulas are kind of cool too!
\u0041\u0042\u0043\u0044\u0045\u0046\u0047\u0048\u0049\u004a
</body>
</html>
`
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,
r *http.Request) {
ru := r.URL.String()
if ru == "/" {
w.Write([]byte(firstPage))
} else if ru == "/anotherpage" {
w.Write([]byte(anotherPage))
}
}))
defer ts.Close()
defer ts.CloseClientConnections()
u, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("URL parse failed: %v\n", err)
}
ctx := context.Background()
*minLen = 10
*maxLen = 0
finder := newWordFinder(u, newFormatter())
finder.run(ctx)
errs := finder.getErrors()
if len(errs) != 0 {
t.Fatalf("got %d unexpected errors\n", len(errs))
}
results := finder.getResults()
if len(results) != 3 {
t.Fatalf("expected 3 results, got %d\n", len(results))
}
if results[0].key != "parallelogram" || results[0].value != 3 {
t.Fatalf("unexpected frequency counts observed\n")
}
if results[1].key != "tarantulas" || results[1].value != 2 {
t.Fatalf("unexpected frequency counts observed\n")
}
if results[2].key != "ABCDEFGHIJ" || results[2].value != 1 {
t.Fatalf("unexpected frequency counts observed\n")
}
}
// Test locating unicode.
func TestConvertUnicode(t *testing.T) {
b := []byte{'A', '\\', 'u', '0', '0', '2', '2', 'H',
'\\', 'u', '2', '3', '1', '8', 'Z'}
dotestConvert(t, b, `A"H⌘Z`)
b = []byte{'\\', 'u', '0', '0', '2', '2',
'\\', 'u', '2', '3', '1', '8'}
dotestConvert(t, b, `"⌘`)
b = []byte{'\\', 'u', '0', '0', 'b', 'd'}
dotestConvert(t, b, `½`)
}
func dotestConvert(t *testing.T, data []byte, expected string) {
s := string(data)
res := convertUnicodeEscapes(s)
if convertUnicodeEscapes(s) != expected {
t.Fatalf("unexpected result: %s\n", res)
}
}