diff options
author | 2024-03-19 21:24:30 -0700 | |
---|---|---|
committer | 2024-03-19 21:57:46 -0700 | |
commit | 08640b27d55f6382af6f08d0789b8109279ae825 (patch) | |
tree | e9b133e38bfb3d09b518d4b35a2292a06aaa661b /internal/reader/rss/parser_test.go | |
parent | 4be993e05537a2ed658f62060b6dd6b89304a346 (diff) | |
download | v2-08640b27d55f6382af6f08d0789b8109279ae825.tar.gz v2-08640b27d55f6382af6f08d0789b8109279ae825.tar.zst v2-08640b27d55f6382af6f08d0789b8109279ae825.zip |
Ensure enclosure URLs are always absolute
Diffstat (limited to '')
-rw-r--r-- | internal/reader/rss/parser_test.go | 170 |
1 files changed, 135 insertions, 35 deletions
diff --git a/internal/reader/rss/parser_test.go b/internal/reader/rss/parser_test.go index e3f8450f..8d84e582 100644 --- a/internal/reader/rss/parser_test.go +++ b/internal/reader/rss/parser_test.go @@ -1016,15 +1016,11 @@ func TestParseEntryWithEnclosures(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) - } - - if feed.Entries[0].URL != "http://www.example.org/entries/1" { - t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } if len(feed.Entries[0].Enclosures) != 1 { - t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" { @@ -1065,15 +1061,11 @@ func TestParseEntryWithIncorrectEnclosureLength(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) - } - - if feed.Entries[0].URL != "http://www.example.org/entries/1" { - t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } if len(feed.Entries[0].Enclosures) != 2 { - t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" { @@ -1093,6 +1085,39 @@ func TestParseEntryWithIncorrectEnclosureLength(t *testing.T) { } } +func TestParseEntryWithDuplicatedEnclosureURL(t *testing.T) { + data := `<?xml version="1.0" encoding="utf-8"?> + <rss version="2.0"> + <channel> + <title>My Podcast Feed</title> + <link>http://example.org</link> + <item> + <title>Podcasting with RSS</title> + <link>http://www.example.org/entries/1</link> + <enclosure url="http://www.example.org/myaudiofile.mp3" type="audio/mpeg" /> + <enclosure url=" http://www.example.org/myaudiofile.mp3 " type="audio/mpeg" /> + </item> + </channel> + </rss>` + + feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data))) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if len(feed.Entries[0].Enclosures) != 1 { + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + } + + if feed.Entries[0].Enclosures[0].URL != "http://www.example.org/myaudiofile.mp3" { + t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL) + } +} + func TestParseEntryWithEmptyEnclosureURL(t *testing.T) { data := `<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> @@ -1106,7 +1131,7 @@ func TestParseEntryWithEmptyEnclosureURL(t *testing.T) { <description>An overview of RSS podcasting</description> <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate> <guid isPermaLink="true">http://www.example.org/entries/1</guid> - <enclosure url="" length="0"/> + <enclosure url=" " length="0"/> </item> </channel> </rss>` @@ -1117,15 +1142,47 @@ func TestParseEntryWithEmptyEnclosureURL(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } - if feed.Entries[0].URL != "http://www.example.org/entries/1" { - t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + if len(feed.Entries[0].Enclosures) != 0 { + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } +} - if len(feed.Entries[0].Enclosures) != 0 { - t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) +func TestParseEntryWithRelativeEnclosureURL(t *testing.T) { + data := `<?xml version="1.0" encoding="utf-8"?> + <rss version="2.0"> + <channel> + <title>My Podcast Feed</title> + <link>http://example.org</link> + <author>some.email@example.org</author> + <item> + <title>Podcasting with RSS</title> + <link>http://www.example.org/entries/1</link> + <description>An overview of RSS podcasting</description> + <pubDate>Fri, 15 Jul 2005 00:00:00 -0500</pubDate> + <guid isPermaLink="true">http://www.example.org/entries/1</guid> + <enclosure url=" /files/file.mp3 "/> + </item> + </channel> + </rss>` + + feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data))) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if len(feed.Entries[0].Enclosures) != 1 { + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + } + + if feed.Entries[0].Enclosures[0].URL != "http://example.org/files/file.mp3" { + t.Errorf("Incorrect enclosure URL, got: %q", feed.Entries[0].Enclosures[0].URL) } } @@ -1154,15 +1211,11 @@ func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) - } - - if feed.Entries[0].URL != "http://www.example.org/entries/1" { - t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } if len(feed.Entries[0].Enclosures) != 1 { - t.Errorf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" { @@ -1178,6 +1231,42 @@ func TestParseEntryWithFeedBurnerEnclosures(t *testing.T) { } } +func TestParseEntryWithFeedBurnerEnclosuresAndRelativeURL(t *testing.T) { + data := `<?xml version="1.0" encoding="utf-8"?> + <rss version="2.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"> + <channel> + <title>My Example Feed</title> + <link>http://example.org</link> + <item> + <title>Example Item</title> + <link>http://www.example.org/entries/1</link> + <enclosure + url="http://feedproxy.google.com/~r/example/~5/lpMyFSCvubs/File.mp3" + length="76192460" + type="audio/mpeg" /> + <feedburner:origEnclosureLink>/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3</feedburner:origEnclosureLink> + </item> + </channel> + </rss>` + + feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data))) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if len(feed.Entries[0].Enclosures) != 1 { + t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) + } + + if feed.Entries[0].Enclosures[0].URL != "http://example.org/67ca416c-f22a-4228-a681-68fc9998ec10/File.mp3" { + t.Errorf("Incorrect enclosure URL, got: %s", feed.Entries[0].Enclosures[0].URL) + } +} + func TestParseEntryWithRelativeURL(t *testing.T) { data := `<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> @@ -1389,7 +1478,7 @@ func TestParseEntryWithMediaGroup(t *testing.T) { <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> <title>My Example Feed</title> - <link>http://example.org</link> + <link>https://example.org</link> <item> <title>Example Item</title> <link>http://www.example.org/entries/1</link> @@ -1400,7 +1489,9 @@ func TestParseEntryWithMediaGroup(t *testing.T) { <media:content type="application/x-bittorrent" url="https://example.org/file2.torrent" isDefault="true"></media:content> <media:content type="application/x-bittorrent" url="https://example.org/file3.torrent"></media:content> <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content> - <media:content type="application/x-bittorrent" url="https://example.org/file5.torrent" fileSize="42"></media:content> + <media:content type="application/x-bittorrent" url="https://example.org/file4.torrent"></media:content> + <media:content type="application/x-bittorrent" url=" file5.torrent " fileSize="42"></media:content> + <media:content type="application/x-bittorrent" url=" " fileSize="42"></media:content> <media:rating>nonadult</media:rating> </media:group> <media:thumbnail url="https://example.org/image.jpg" height="122" width="223"></media:thumbnail> @@ -1453,15 +1544,19 @@ func TestParseEntryWithMediaContent(t *testing.T) { <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> <title>My Example Feed</title> - <link>http://example.org</link> + <link>https://example.org</link> <item> <title>Example Item</title> <link>http://www.example.org/entries/1</link> <media:thumbnail url="https://example.org/thumbnail.jpg" /> + <media:thumbnail url="https://example.org/thumbnail.jpg" /> + <media:thumbnail url=" thumbnail.jpg " /> + <media:thumbnail url=" " /> <media:content url="https://example.org/media1.jpg" medium="image"> <media:title type="html">Some Title for Media 1</media:title> </media:content> - <media:content url="https://example.org/media2.jpg" medium="image" /> + <media:content url=" /media2.jpg " medium="image" /> + <media:content url=" " medium="image" /> </item> </channel> </rss>` @@ -1472,9 +1567,9 @@ func TestParseEntryWithMediaContent(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } - if len(feed.Entries[0].Enclosures) != 3 { + if len(feed.Entries[0].Enclosures) != 4 { t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } @@ -1484,6 +1579,7 @@ func TestParseEntryWithMediaContent(t *testing.T) { size int64 }{ {"https://example.org/thumbnail.jpg", "image/*", 0}, + {"https://example.org/thumbnail.jpg", "image/*", 0}, {"https://example.org/media1.jpg", "image/*", 0}, {"https://example.org/media2.jpg", "image/*", 0}, } @@ -1508,11 +1604,14 @@ func TestParseEntryWithMediaPeerLink(t *testing.T) { <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> <title>My Example Feed</title> - <link>http://example.org</link> + <link>https://website.example.org</link> <item> <title>Example Item</title> <link>http://www.example.org/entries/1</link> - <media:peerLink type="application/x-bittorrent" href="http://www.example.org/file.torrent" /> + <media:peerLink type="application/x-bittorrent" href="https://www.example.org/file.torrent" /> + <media:peerLink type="application/x-bittorrent" href="https://www.example.org/file.torrent" /> + <media:peerLink type="application/x-bittorrent" href=" file2.torrent " /> + <media:peerLink type="application/x-bittorrent" href=" " /> </item> </channel> </rss>` @@ -1523,10 +1622,10 @@ func TestParseEntryWithMediaPeerLink(t *testing.T) { } if len(feed.Entries) != 1 { - t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + t.Fatalf("Incorrect number of entries, got: %d", len(feed.Entries)) } - if len(feed.Entries[0].Enclosures) != 1 { + if len(feed.Entries[0].Enclosures) != 2 { t.Fatalf("Incorrect number of enclosures, got: %d", len(feed.Entries[0].Enclosures)) } @@ -1535,7 +1634,8 @@ func TestParseEntryWithMediaPeerLink(t *testing.T) { mimeType string size int64 }{ - {"http://www.example.org/file.torrent", "application/x-bittorrent", 0}, + {"https://www.example.org/file.torrent", "application/x-bittorrent", 0}, + {"https://website.example.org/file2.torrent", "application/x-bittorrent", 0}, } for index, enclosure := range feed.Entries[0].Enclosures { |