-
Notifications
You must be signed in to change notification settings - Fork 2
/
crawler_test.go
143 lines (112 loc) · 3.25 KB
/
crawler_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
package crawler
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestCrawl(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
expected := []expectedPage{
{url: s.URL + "/depth-one.html", totalLinks: 1},
{url: s.URL + "/depth-two.html", totalLinks: 2},
{url: s.URL + "/depth-three.html", totalLinks: 1},
{url: s.URL + "/", totalLinks: 5, totalAssets: 4},
{url: s.URL + "/depth-four.html", totalLinks: 1},
{url: "http://example.localhost/absolute/url", hasError: true},
{url: s.URL + "/absolute/path", hasError: true},
{url: s.URL + "/relative/path", hasError: true},
{url: s.URL + "/link-with-anchor/test", hasError: true},
}
testCrawl(t, s, "/depth-one.html", expected)
}
func TestMaxDepth(t *testing.T) {
c, err := New(WithMaxDepth(1))
require.NoError(t, err)
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
var n int
err = c.Crawl(s.URL+"/depth-one.html", func(url string, res *Response, err error) error {
n++
return nil
})
require.NoError(t, err)
require.Equal(t, 2, n)
}
func TestCheckFetch(t *testing.T) {
c, err := New(WithCheckFetch(func(req *Request) bool {
return req.URL.Path == "/depth-one.html"
}))
require.NoError(t, err)
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
var n int
err = c.Crawl(s.URL+"/depth-one.html", func(url string, res *Response, err error) error {
n++
return nil
})
require.NoError(t, err)
require.Equal(t, 1, n)
}
func TestAvoidVisitingTwice(t *testing.T) {
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
expected := []expectedPage{
{url: s.URL + "/start-cycle.html", totalLinks: 2},
{url: s.URL + "/intermediate-cycle.html", totalLinks: 1},
{url: s.URL + "/loop-cycle.html", totalLinks: 1},
}
testCrawl(t, s, "/start-cycle.html", expected)
}
func TestStopCrawl(t *testing.T) {
c, err := New()
require.NoError(t, err)
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
var n int
err = c.Crawl(s.URL+"/depth-one.html", func(url string, res *Response, err error) error {
require.Equal(t, 0, n)
n++
return errors.New("failed")
})
require.Error(t, err)
require.Equal(t, 1, n)
}
func TestCrawlInvalidStartHost(t *testing.T) {
c, err := New()
require.NoError(t, err)
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
err = c.Crawl(s.URL+"/non-existent", func(url string, res *Response, err error) error {
require.Error(t, err)
return nil
})
require.NoError(t, err)
}
type expectedPage struct {
url string
totalLinks int
totalAssets int
hasError bool
}
func testCrawl(t *testing.T, s *httptest.Server, startPath string, expected []expectedPage) {
c, err := New()
require.NoError(t, err)
var actual []expectedPage
err = c.Crawl(s.URL+startPath, func(url string, res *Response, err error) error {
result := expectedPage{url: url}
if err == nil {
result.hasError = false
result.totalLinks = len(res.Links)
result.totalAssets = len(res.Assets)
} else {
result.hasError = true
}
actual = append(actual, result)
return nil
})
require.NoError(t, err)
require.Equal(t, expected, actual)
}