-
Notifications
You must be signed in to change notification settings - Fork 2
/
weaver_test.go
159 lines (148 loc) · 3.56 KB
/
weaver_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
package weaver_test
import (
"context"
"io"
"log"
"net/http"
"net/http/httptest"
"testing"
"testing/fstest"
"github.com/bitfield/weaver"
"github.com/google/go-cmp/cmp"
"golang.org/x/time/rate"
)
func TestCrawlReturnsExpectedResults(t *testing.T) {
t.Parallel()
ts := httptest.NewTLSServer(
http.FileServerFS(testFS),
)
defer ts.Close()
c := weaver.NewChecker()
c.HTTPClient = ts.Client()
c.Output = io.Discard
c.Limiter.SetLimit(rate.Inf)
c.Check(context.Background(), ts.URL)
want := []weaver.Result{
{
Link: ts.URL,
Status: weaver.StatusOK,
Message: "200 OK",
Referrer: "START",
},
{
Link: ts.URL + "/go/sucks.html",
Status: weaver.StatusOK,
Message: "200 OK",
Referrer: ts.URL,
},
{
Link: ts.URL + "/bogus",
Status: weaver.StatusError,
Message: "404 Not Found",
Referrer: ts.URL + "/go/sucks.html",
},
{
Link: ts.URL + "/go/post.html",
Status: weaver.StatusOK,
Message: "200 OK",
Referrer: ts.URL + "/go/sucks.html",
},
{
Link: ts.URL + "/rust_rules.html",
Status: weaver.StatusError,
Message: "404 Not Found",
Referrer: ts.URL,
},
{
Link: ts.URL + "/invalid_links.html",
Status: weaver.StatusOK,
Message: "200 OK",
Referrer: ts.URL,
},
{
Link: "httq://invalid_scheme.html",
Status: weaver.StatusError,
Message: `Get "httq://invalid_scheme.html": unsupported protocol scheme "httq"`,
Referrer: ts.URL + "/invalid_links.html",
},
{
Link: "http:// /",
Status: weaver.StatusError,
Message: `parse "http:// /": invalid character " " in host name`,
Referrer: ts.URL + "/invalid_links.html",
},
}
got := c.Results()
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestReduceLimit_SetsCorrectLimit(t *testing.T) {
t.Parallel()
a := weaver.NewAdaptiveRateLimiter()
a.SetLimit(4)
a.ReduceLimit()
want := rate.Limit(2)
got := a.Limit()
if want != got {
t.Errorf("want %.2f, got %.2f", want, got)
}
}
func TestCertVerifyFailuresAreRecordedAsWarnings(t *testing.T) {
t.Parallel()
ts := httptest.NewTLSServer(nil)
defer ts.Close()
ts.Config.ErrorLog = log.New(io.Discard, "", 0)
c := weaver.NewChecker()
c.Output = io.Discard
c.Check(context.Background(), ts.URL)
got := c.Results()
if len(got) != 1 {
t.Fatalf("unexpected result set %v", got)
}
res := got[0]
if res.Link != ts.URL {
t.Errorf("want URL %q, got %q", ts.URL, res.Link)
}
if res.Status != weaver.StatusWarning {
t.Errorf("want status %q, got %q", weaver.StatusWarning, res.Status)
}
}
var testFS = fstest.MapFS{
"go/sucks.html": {
Data: []byte(`<html><head><title>Why Go Sucks</title></head>
<body>
Something something error handling (source: <a href="/bogus">Hacker News</a>)
<a href="/">Index</a>
<a href="post.html">Post in same subfolder</a>
</body>
</html>`),
},
"go/post.html": {},
"index.html": {
Data: []byte(`<html><head><title>Start page</title></head>
<body>
My latest interesting opinions:
<ul>
<li><a href="go/sucks.html">Why Go Sucks</a></li>
<li><a href="rust_rules.html">Why Rust Rules</a></li>
<li><a href="invalid_links.html">Invalid Links</a></li>
</ul>
<a href="mailto:[email protected]">Contact me</a>
</body>
</html>
`),
},
"invalid_links.html": {
Data: []byte(`<html><head><title>Invalid links</title></head>
<body>
My latest interesting opinions:
<ul>
<li><a href="httq://invalid_scheme.html">Invalid scheme example</a></li>
<li><a href="http:// /">Invalid path</a></li>
</ul>
</body>
</html>
`),
},
}