forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher_test.go
100 lines (72 loc) · 1.87 KB
/
fetcher_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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
)
func TestNewFetcher(t *testing.T) {
newFetcher(1)
}
func TestFetcherCache(t *testing.T) {
f := newFetcher(1)
p, err := f.Fetch(rootURL)
assert.NotEqual(t, (*page)(nil), p)
assert.Nil(t, err)
p, err = f.Fetch(nonExistentURL)
assert.Equal(t, (*page)(nil), p)
assert.NotNil(t, err)
p, err = f.Fetch(rootURL)
assert.Equal(t, (*page)(nil), p)
assert.Nil(t, err)
p, err = f.Fetch(nonExistentURL)
assert.Equal(t, (*page)(nil), p)
assert.NotNil(t, err)
}
func TestFetcherFetchError(t *testing.T) {
f := newFetcher(1)
for _, s := range []string{nonExistentURL, ":"} {
p, err := f.Fetch(s)
assert.Equal(t, (*page)(nil), p)
assert.NotNil(t, err)
}
}
func TestFetcherFetchHTML(t *testing.T) {
f := newFetcher(1)
for _, s := range []string{rootURL, existentURL, fragmentURL, erroneousURL} {
n, err := f.fetchHTML(s, "")
assert.NotEqual(t, (*html.Node)(nil), n)
assert.Nil(t, err)
}
}
func TestFetcherFetchHTMLWithFragment(t *testing.T) {
f := newFetcher(1)
n, err := f.fetchHTML(fragmentURL, "foo")
assert.NotEqual(t, (*html.Node)(nil), n)
assert.Nil(t, err)
n, err = f.fetchHTML(fragmentURL, "bar")
assert.Equal(t, (*html.Node)(nil), n)
assert.NotNil(t, err)
}
func TestFetcherFetchHTMLError(t *testing.T) {
f := newFetcher(1)
for _, s := range []string{":", nonExistentURL} {
n, err := f.fetchHTML(s, "")
assert.Equal(t, (*html.Node)(nil), n)
assert.NotNil(t, err)
}
}
func TestSeparateFragment(t *testing.T) {
for _, ss := range [][3]string{
{"http://foo.com#bar", "http://foo.com", "bar"},
{"#bar", "", "bar"},
} {
u, id, err := separateFragment(ss[0])
assert.Nil(t, err)
assert.Equal(t, ss[1], u)
assert.Equal(t, ss[2], id)
}
}
func TestSeparateFragmentError(t *testing.T) {
_, _, err := separateFragment(":")
assert.NotNil(t, err)
}