go-pkg-rss/feed_test.go

119 lines
3.4 KiB
Go
Raw Normal View History

package feeder
2013-03-28 03:05:27 +00:00
import (
"io/ioutil"
2013-11-28 09:23:49 +00:00
"testing"
2013-03-28 03:05:27 +00:00
)
var items []*Item
func TestFeed(t *testing.T) {
urilist := []string{
//"http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", // Non-utf8 encoding.
"http://store.steampowered.com/feeds/news.xml", // This feed violates the rss spec.
"http://cyber.law.harvard.edu/rss/examples/sampleRss092.xml",
"http://cyber.law.harvard.edu/rss/examples/rss2sample.xml",
"http://blog.case.edu/news/feed.atom",
}
var feed *Feed
2011-11-02 15:51:04 +00:00
var err error
for _, uri := range urilist {
feed = New(5, true, chanHandler, itemHandler)
if err = feed.Fetch(uri, nil); err != nil {
t.Errorf("%s >>> %s", uri, err)
return
}
}
}
2013-12-05 15:29:55 +00:00
func Test_NewItem(t *testing.T) {
content, _ := ioutil.ReadFile("testdata/initial.atom")
feed := New(1, true, chanHandler, itemHandler)
err := feed.FetchBytes("http://example.com", content, nil)
if err != nil {
t.Error(err)
}
content, _ = ioutil.ReadFile("testdata/initial_plus_one_new.atom")
feed.FetchBytes("http://example.com", content, nil)
expected := "Second title"
if len(items) != 1 {
t.Errorf("Expected %s new item, got %s", 1, len(items))
}
if expected != items[0].Title {
t.Errorf("Expected %s, got %s", expected, items[0].Title)
}
}
2013-03-28 03:05:27 +00:00
func Test_AtomAuthor(t *testing.T) {
content, err := ioutil.ReadFile("testdata/idownload.atom")
2013-11-28 09:23:49 +00:00
if err != nil {
2013-03-28 03:05:27 +00:00
t.Errorf("unable to load file")
2013-11-28 09:23:49 +00:00
}
2013-03-28 03:05:27 +00:00
feed := New(1, true, chanHandler, itemHandler)
err = feed.FetchBytes("http://example.com", content, nil)
item := feed.Channels[0].Items[0]
2013-03-28 03:05:27 +00:00
expected := "Cody Lee"
if item.Author.Name != expected {
t.Errorf("Expected author to be %s but found %s", expected, item.Author.Name)
}
}
func Test_RssAuthor(t *testing.T) {
2013-11-28 09:23:49 +00:00
content, _ := ioutil.ReadFile("testdata/boing.rss")
feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil)
2013-03-28 03:05:27 +00:00
item := feed.Channels[0].Items[0]
2013-11-28 09:23:49 +00:00
expected := "Cory Doctorow"
if item.Author.Name != expected {
t.Errorf("Expected author to be %s but found %s", expected, item.Author.Name)
}
2013-03-28 03:05:27 +00:00
}
func Test_CData(t *testing.T) {
content, _ := ioutil.ReadFile("testdata/iosBoardGameGeek.rss")
feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil)
item := feed.Channels[0].Items[0]
expected := `<p>abc<div>"def"</div>ghi`
2013-11-28 09:23:49 +00:00
if item.Description != expected {
t.Errorf("Expected item.Description to be [%s] but item.Description=[%s]", expected, item.Description)
}
}
2014-01-18 16:35:00 +00:00
func Test_Link(t *testing.T) {
content, _ := ioutil.ReadFile("testdata/nytimes.rss")
2014-01-18 16:35:00 +00:00
feed := New(1, true, chanHandler, itemHandler)
feed.FetchBytes("http://example.com", content, nil)
channel := feed.Channels[0]
item := channel.Items[0]
channelLinkExpected := "http://www.nytimes.com/services/xml/rss/nyt/US.xml"
itemLinkExpected := "http://www.nytimes.com/2014/01/18/technology/in-keeping-grip-on-data-pipeline-obama-does-little-to-reassure-industry.html?partner=rss&emc=rss"
2014-01-18 16:35:00 +00:00
if channel.Links[0].Href != channelLinkExpected {
t.Errorf("Expected author to be %s but found %s", channelLinkExpected, channel.Links[0].Href)
}
if item.Links[0].Href != itemLinkExpected {
t.Errorf("Expected author to be %s but found %s", itemLinkExpected, item.Links[0].Href)
}
}
func chanHandler(feed *Feed, newchannels []*Channel) {
println(len(newchannels), "new channel(s) in", feed.Url)
}
func itemHandler(feed *Feed, ch *Channel, newitems []*Item) {
2013-11-28 09:23:49 +00:00
items = newitems
println(len(newitems), "new item(s) in", ch.Title, "of", feed.Url)
}