// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved. // SPDX-License-Identifier: Apache-2.0 package opml // import "miniflux.app/v2/internal/reader/opml" import ( "bytes" "testing" ) func TestParseOpmlWithoutCategories(t *testing.T) { data := ` mySubscriptions.opml ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/", Description: "Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media."}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 13 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13) } if !subscriptions[0].Equals(expected[0]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[0], expected[0]) } } func TestParseOpmlWithCategories(t *testing.T) { data := ` mySubscriptions.opml ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"}) expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"}) expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 3 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) { data := ` mySubscriptions.opml ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) expected = append(expected, &Subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 2 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseOpmlVersion1(t *testing.T) { data := ` mySubscriptions.opml Wed, 13 Mar 2019 11:51:41 GMT ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Category 1"}) expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Category 2"}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 2 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) { data := ` mySubscriptions.opml Wed, 13 Mar 2019 11:51:41 GMT ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 2 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseOpmlVersion1WithSeveralNestedOutlines(t *testing.T) { data := ` RSSOwl Subscriptions 星期二, 26 四月 2022 00:12:04 CST ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "Some Category"}) expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "Some Category"}) expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "Another Category"}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 3 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseOpmlWithInvalidCharacterEntity(t *testing.T) { data := ` mySubscriptions.opml ` var expected SubcriptionList expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/a&b", SiteURL: "http://example.org/c&d", CategoryName: "Feed 1"}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Fatal(err) } if len(subscriptions) != 1 { t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1) } for i := range len(subscriptions) { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i]) } } } func TestParseInvalidXML(t *testing.T) { data := `garbage` _, err := Parse(bytes.NewBufferString(data)) if err == nil { t.Error("Parse should generate an error") } }