/
feedfinder_test.go
82 lines (64 loc) · 2.31 KB
/
feedfinder_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
package feedfinder
import (
"bytes"
"net/url"
"testing"
"golang.org/x/net/html"
assert "github.com/pilu/miniassert"
)
func TestNewFinder(t *testing.T) {
f, err := newFinder("http://example.com")
expectedURL, _ := url.Parse("http://example.com")
assert.Nil(t, err)
assert.Equal(t, expectedURL, f.URL)
}
func TestFinder_FindFromReader(t *testing.T) {
html := `<html>
<head>
<link rel="alternate" type="application/atom+xml" title="Atom Feed" href="http://feeds.example.com/feed">
<link rel="alternate" type="application/rss+xml" title="Rss Feed" href="/daily.xml" />
</head>
<body>
</body>
</html>`
f, _ := newFinder("http://example.com")
links, _ := f.findFromReader(bytes.NewBufferString(html))
assert.Equal(t, 2, len(links))
expectedURL, _ := url.Parse("http://feeds.example.com/feed")
assert.Equal(t, "http://feeds.example.com/feed", links[0].Href)
assert.Equal(t, expectedURL, links[0].URL)
assert.Equal(t, "application/atom+xml", links[0].Type)
assert.Equal(t, "Atom Feed", links[0].Title)
expectedURL, _ = url.Parse("http://example.com/daily.xml")
assert.Equal(t, "/daily.xml", links[1].Href)
assert.Equal(t, expectedURL, links[1].URL)
assert.Equal(t, "application/rss+xml", links[1].Type)
assert.Equal(t, "Rss Feed", links[1].Title)
}
func TestNewFeedLink_WithRelativeUrl(t *testing.T) {
baseURL, _ := url.Parse("http://example.com")
link := newFeedLink("application/rss+xml", "/feed", "RSS", baseURL)
assert.Equal(t, "application/rss+xml", link.Type)
assert.Equal(t, "/feed", link.Href)
assert.Equal(t, "RSS", link.Title)
expectedURL, _ := url.Parse("http://example.com/feed")
assert.Equal(t, expectedURL, link.URL)
}
func TestNewFeedLink_WithAbsoluteUrl(t *testing.T) {
baseURL, _ := url.Parse("http://example.com")
link := newFeedLink("application/rss+xml", "http://absolute.example.com/feed", "RSS", baseURL)
assert.Equal(t, "application/rss+xml", link.Type)
assert.Equal(t, "http://absolute.example.com/feed", link.Href)
assert.Equal(t, "RSS", link.Title)
expectedURL, _ := url.Parse("http://absolute.example.com/feed")
assert.Equal(t, expectedURL, link.URL)
}
func TestMakeAttributesMap(t *testing.T) {
attrs := []html.Attribute{
{"", "href", "/feed"},
{"", "foo", "/bar"},
}
m := makeAttributesMap(attrs)
assert.Equal(t, "/feed", m["href"])
assert.Equal(t, "/bar", m["foo"])
}