// Copyright 2017 Frédéric Guillot. All rights reserved. // Use of this source code is governed by the Apache 2.0 // license that can be found in the LICENSE file. package opml // import "miniflux.app/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/"}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Error(err) } if len(subscriptions) != 13 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13) } if !subscriptions[0].Equals(expected[0]) { t.Errorf(`Subscription are 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.Error(err) } if len(subscriptions) != 3 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3) } for i := 0; i < len(subscriptions); i++ { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription are 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.Error(err) } if len(subscriptions) != 2 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := 0; i < len(subscriptions); i++ { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription are 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: ""}) 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.Error(err) } if len(subscriptions) != 2 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := 0; i < len(subscriptions); i++ { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription are 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.Error(err) } if len(subscriptions) != 2 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) } for i := 0; i < len(subscriptions); i++ { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription are 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: ""}) subscriptions, err := Parse(bytes.NewBufferString(data)) if err != nil { t.Error(err) } if len(subscriptions) != 1 { t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 1) } for i := 0; i < len(subscriptions); i++ { if !subscriptions[i].Equals(expected[i]) { t.Errorf(`Subscription are 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") } }