| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package subscription
- import (
- "strings"
- "testing"
- )
- func TestFindYoutubeFeed(t *testing.T) {
- type testResult struct {
- websiteURL string
- feedURL string
- discoveryError bool
- }
- scenarios := []testResult{
- // Video URL
- {
- websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
- feedURL: "",
- },
- // Video URL with position argument
- {
- websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=1",
- feedURL: "",
- },
- // Video URL with position argument
- {
- websiteURL: "https://www.youtube.com/watch?t=1&v=dQw4w9WgXcQ",
- feedURL: "",
- },
- // Channel URL
- {
- websiteURL: "https://www.youtube.com/channel/UC-Qj80avWItNRjkZ41rzHyw",
- feedURL: "https://www.youtube.com/feeds/videos.xml?channel_id=UC-Qj80avWItNRjkZ41rzHyw",
- },
- // Channel URL with name
- {
- websiteURL: "https://www.youtube.com/@ABCDEFG",
- feedURL: "",
- },
- // Playlist URL
- {
- websiteURL: "https://www.youtube.com/playlist?list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
- feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
- },
- // Playlist URL with video ID
- {
- websiteURL: "https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
- feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_N42HlCLhqyJ0ZBWr5K1QDM",
- },
- // Playlist URL with video ID and index argument
- {
- websiteURL: "https://www.youtube.com/watch?v=6IutBmRJNLk&list=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR&index=4",
- feedURL: "https://www.youtube.com/feeds/videos.xml?playlist_id=PLOOwEPgFWm_NHcQd9aCi5JXWASHO_n5uR",
- },
- // Empty playlist ID parameter
- {
- websiteURL: "https://www.youtube.com/playlist?list=",
- feedURL: "",
- },
- // Non-Youtube URL
- {
- websiteURL: "https://www.example.com/channel/UC-Qj80avWItNRjkZ41rzHyw",
- feedURL: "",
- },
- // Invalid URL
- {
- websiteURL: "https://example|org/",
- feedURL: "",
- discoveryError: true,
- },
- }
- for _, scenario := range scenarios {
- subscriptions, localizedError := NewSubscriptionFinder(nil).findSubscriptionsFromYouTube(scenario.websiteURL)
- if scenario.discoveryError {
- if localizedError == nil {
- t.Fatalf(`Parsing an invalid URL should return an error`)
- }
- }
- if scenario.feedURL == "" {
- if len(subscriptions) > 0 {
- t.Fatalf(`Parsing an invalid URL should not return any subscription: %q -> %v`, scenario.websiteURL, subscriptions)
- }
- } else {
- if localizedError != nil {
- t.Fatalf(`Parsing a correctly formatted YouTube playlist or channel page should not return any error: %v`, localizedError)
- }
- if len(subscriptions) != 1 {
- t.Fatalf(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].URL != scenario.feedURL {
- t.Errorf(`Unexpected feed, got %s, instead of %s`, subscriptions[0].URL, scenario.feedURL)
- }
- }
- }
- }
- func TestParseWebPageWithRssFeed(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/rss" rel="alternate" type="application/rss+xml" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Some Title" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/rss" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "rss" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithAtomFeed(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/atom.xml" rel="alternate" type="application/atom+xml" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Some Title" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/atom.xml" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "atom" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithJSONFeed(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/feed.json" rel="alternate" type="application/feed+json" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Some Title" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/feed.json" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "json" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithOldJSONFeedMimeType(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/feed.json" rel="alternate" type="application/json" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Some Title" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/feed.json" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "json" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithRelativeFeedURL(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="/feed.json" rel="alternate" type="application/feed+json" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Some Title" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/feed.json" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "json" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithEmptyTitle(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="/feed.json" rel="alternate" type="application/feed+json">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "http://example.org/feed.json" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/feed.json" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "json" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithMultipleFeeds(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/atom.xml" rel="alternate" type="application/atom+xml" title="Atom Feed">
- <link href="http://example.org/feed.json" rel="alternate" type="application/json" title="JSON Feed">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 2 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- }
- func TestParseWebPageWithDuplicatedFeeds(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href="http://example.org/feed.xml" rel="alternate" type="application/rss+xml" title="Feed A">
- <link href="http://example.org/feed.xml" rel="alternate" type="application/rss+xml" title="Feed B">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 1 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- if subscriptions[0].Title != "Feed A" {
- t.Errorf(`Incorrect subscription title: %q`, subscriptions[0].Title)
- }
- if subscriptions[0].URL != "http://example.org/feed.xml" {
- t.Errorf(`Incorrect subscription URL: %q`, subscriptions[0].URL)
- }
- if subscriptions[0].Type != "rss" {
- t.Errorf(`Incorrect subscription type: %q`, subscriptions[0].Type)
- }
- }
- func TestParseWebPageWithEmptyFeedURL(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link href rel="alternate" type="application/feed+json" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 0 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- }
- func TestParseWebPageWithNoHref(t *testing.T) {
- htmlPage := `
- <!doctype html>
- <html>
- <head>
- <link rel="alternate" type="application/feed+json" title="Some Title">
- </head>
- <body>
- </body>
- </html>`
- subscriptions, err := NewSubscriptionFinder(nil).findSubscriptionsFromWebPage("http://example.org/", "text/html", strings.NewReader(htmlPage))
- if err != nil {
- t.Fatalf(`Parsing a correctly formatted HTML page should not return any error: %v`, err)
- }
- if len(subscriptions) != 0 {
- t.Fatal(`Incorrect number of subscriptions returned`)
- }
- }
|