From 70c0278fe36e58839125a12ee38642e9ff790726 Mon Sep 17 00:00:00 2001 From: Lutz Horn Date: Tue, 6 Jun 2017 23:55:24 +0200 Subject: [PATCH] implemented support for RSS --- atom_test.go | 54 ++++++++++++++++++++++++++++++++ rss_1.0_test.go | 28 +++++++++++++++++ rss_2.0.go | 2 +- rss_2.0_test.go | 28 +++++++++++++++++ testdata/rss_2.0_content_encoded | 24 ++++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 atom_test.go create mode 100644 rss_1.0_test.go create mode 100644 rss_2.0_test.go create mode 100644 testdata/rss_2.0_content_encoded diff --git a/atom_test.go b/atom_test.go new file mode 100644 index 0000000..6120b69 --- /dev/null +++ b/atom_test.go @@ -0,0 +1,54 @@ +package rss + +import ( + "io/ioutil" + "testing" +) + +func TestParseAtomTitle(t *testing.T) { + tests := map[string]string{ + "atom_1.0": "Titel des Weblogs", + "atom_1.0_enclosure": "Titel des Weblogs", + "atom_1.0-1": "Golem.de", + } + + for test, want := range tests { + data, err := ioutil.ReadFile("testdata/" + test) + if err != nil { + t.Fatalf("Reading %s: %v", test, err) + } + + feed, err := Parse(data) + if err != nil { + t.Fatalf("Parsing %s: %v", test, err) + } + + if feed.Title != want { + t.Fatalf("%s: expected %s, got %s", test, want, feed.Title) + } + } +} + +func TestParseAtomContent(t *testing.T) { + tests := map[string]string{ + "atom_1.0": "Volltext des Weblog-Eintrags", + "atom_1.0_enclosure": "Volltext des Weblog-Eintrags", + "atom_1.0-1": "", + } + + for test, want := range tests { + data, err := ioutil.ReadFile("testdata/" + test) + if err != nil { + t.Fatalf("Reading %s: %v", test, err) + } + + feed, err := Parse(data) + if err != nil { + t.Fatalf("Parsing %s: %v", test, err) + } + + if feed.Items[0].Content != want { + t.Fatalf("%s: expected %s, got %s", test, want, feed.Items[0].Content) + } + } +} diff --git a/rss_1.0_test.go b/rss_1.0_test.go new file mode 100644 index 0000000..2230f43 --- /dev/null +++ b/rss_1.0_test.go @@ -0,0 +1,28 @@ +package rss + +import ( + "io/ioutil" + "testing" +) + +func TestParseRSS(t *testing.T) { + tests := map[string]string{ + "rss_1.0": "Golem.de", + } + + for test, want := range tests { + data, err := ioutil.ReadFile("testdata/" + test) + if err != nil { + t.Fatalf("Reading %s: %v", test, err) + } + + feed, err := Parse(data) + if err != nil { + t.Fatalf("Parsing %s: %v", test, err) + } + + if feed.Title != want { + t.Fatalf("%s: expected %s, got %s", test, want, feed.Title) + } + } +} diff --git a/rss_2.0.go b/rss_2.0.go index 3ac94a2..bacb6e3 100644 --- a/rss_2.0.go +++ b/rss_2.0.go @@ -154,7 +154,7 @@ type rss2_0Item struct { XMLName xml.Name `xml:"item"` Title string `xml:"title"` Description string `xml:"description"` - Content string `xml:"encoded"` + Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` Category string `xml:"category"` Link string `xml:"link"` PubDate string `xml:"pubDate"` diff --git a/rss_2.0_test.go b/rss_2.0_test.go new file mode 100644 index 0000000..d184850 --- /dev/null +++ b/rss_2.0_test.go @@ -0,0 +1,28 @@ +package rss + +import ( + "io/ioutil" + "testing" +) + +func TestParseContent(t *testing.T) { + tests := map[string]string{ + "rss_2.0_content_encoded": "

Example.com is an example site.

", + } + + for test, want := range tests { + data, err := ioutil.ReadFile("testdata/" + test) + if err != nil { + t.Fatalf("Reading %s: %v", test, err) + } + + feed, err := Parse(data) + if err != nil { + t.Fatalf("Parsing %s: %v", test, err) + } + + if feed.Items[0].Content != want { + t.Fatalf("%s: expected %s, got %s", test, want, feed.Items[0].Content) + } + } +} diff --git a/testdata/rss_2.0_content_encoded b/testdata/rss_2.0_content_encoded new file mode 100644 index 0000000..983932b --- /dev/null +++ b/testdata/rss_2.0_content_encoded @@ -0,0 +1,24 @@ + + + + RSS Title + This is an example of an RSS feed + http://www.someexamplerssdomain.com/main.html + Mon, 06 Sep 2010 00:01:00 +0000 + Mon, 06 Sep 2009 16:45:00 +0000 + 1800 + + + Example entry + Here is some text containing an interesting +description. + http://www.wikipedia.org/ + unique string per item + Mon, 06 Sep 2009 16:45:00 +0000 + Example.com is an example site.

]]>
+
+ +
+