2009-11-23 12:46:12 +00:00
|
|
|
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
|
2009-11-23 12:46:12 +00:00
|
|
|
|
|
|
|
func TestFeed(t *testing.T) {
|
|
|
|
urilist := []string{
|
2012-02-29 10:31:01 +00:00
|
|
|
//"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.
|
2009-11-23 12:46:12 +00:00
|
|
|
"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",
|
2010-05-23 14:21:30 +00:00
|
|
|
}
|
2009-11-23 12:46:12 +00:00
|
|
|
|
2010-05-25 23:41:44 +00:00
|
|
|
var feed *Feed
|
2011-11-02 15:51:04 +00:00
|
|
|
var err error
|
2010-05-25 23:41:44 +00:00
|
|
|
|
2009-11-23 12:46:12 +00:00
|
|
|
for _, uri := range urilist {
|
2010-12-17 23:25:16 +00:00
|
|
|
feed = New(5, true, chanHandler, itemHandler)
|
2010-05-25 23:41:44 +00:00
|
|
|
|
2012-02-29 10:31:01 +00:00
|
|
|
if err = feed.Fetch(uri, nil); err != nil {
|
2010-05-23 14:21:30 +00:00
|
|
|
t.Errorf("%s >>> %s", uri, err)
|
2011-02-01 14:30:39 +00:00
|
|
|
return
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-17 23:25:16 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2013-12-05 13:22:03 +00:00
|
|
|
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
|
|
|
|
2013-12-05 13:22:03 +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
|
|
|
}
|
|
|
|
|
2014-01-23 16:57:26 +00:00
|
|
|
func Test_ItemExtensions(t *testing.T) {
|
2014-01-18 14:30:56 +00:00
|
|
|
content, _ := ioutil.ReadFile("testdata/extension.rss")
|
|
|
|
feed := New(1, true, chanHandler, itemHandler)
|
|
|
|
feed.FetchBytes("http://example.com", content, nil)
|
|
|
|
|
2014-01-23 19:37:18 +00:00
|
|
|
edgarExtensionxbrlFiling := feed.Channels[0].Items[0].Extensions["http://www.sec.gov/Archives/edgar"]["xbrlFiling"][0].Childrens
|
2014-01-18 14:30:56 +00:00
|
|
|
companyExpected := "Cellular Biomedicine Group, Inc."
|
2014-01-23 19:37:18 +00:00
|
|
|
companyName := edgarExtensionxbrlFiling["companyName"][0]
|
2014-01-18 14:30:56 +00:00
|
|
|
if companyName.Value != companyExpected {
|
|
|
|
t.Errorf("Expected company to be %s but found %s", companyExpected, companyName.Value)
|
|
|
|
}
|
|
|
|
|
2014-01-23 19:37:18 +00:00
|
|
|
files := edgarExtensionxbrlFiling["xbrlFiles"][0].Childrens["xbrlFile"]
|
2014-01-18 14:30:56 +00:00
|
|
|
fileSizeExpected := 10
|
2014-01-23 16:57:26 +00:00
|
|
|
if len(files) != 10 {
|
|
|
|
t.Errorf("Expected files size to be %s but found %s", fileSizeExpected, len(files))
|
2014-01-18 14:30:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 16:57:26 +00:00
|
|
|
file := files[0]
|
2014-01-18 14:30:56 +00:00
|
|
|
fileExpected := "cbmg_10qa.htm"
|
|
|
|
if file.Attrs["file"] != fileExpected {
|
|
|
|
t.Errorf("Expected file to be %s but found %s", fileExpected, len(file.Attrs["file"]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 20:07:22 +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)
|
|
|
|
|
2013-12-05 13:22:03 +00:00
|
|
|
item := feed.Channels[0].Items[0]
|
2013-03-28 20:07:22 +00:00
|
|
|
expected := `<p>abc<div>"def"</div>ghi`
|
2013-11-28 09:23:49 +00:00
|
|
|
if item.Description != expected {
|
2013-03-28 20:07:22 +00:00
|
|
|
t.Errorf("Expected item.Description to be [%s] but item.Description=[%s]", expected, item.Description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 23:25:16 +00:00
|
|
|
func chanHandler(feed *Feed, newchannels []*Channel) {
|
2011-02-01 14:30:39 +00:00
|
|
|
println(len(newchannels), "new channel(s) in", feed.Url)
|
2010-12-17 23:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func itemHandler(feed *Feed, ch *Channel, newitems []*Item) {
|
2013-11-28 09:23:49 +00:00
|
|
|
items = newitems
|
2011-02-01 14:30:39 +00:00
|
|
|
println(len(newitems), "new item(s) in", ch.Title, "of", feed.Url)
|
2009-11-23 12:46:12 +00:00
|
|
|
}
|