forked from SlyMarbo/rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss_test.go
118 lines (100 loc) · 3.07 KB
/
rss_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package rss
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"reflect"
"testing"
)
func TestParseTitle(t *testing.T) {
tests := map[string]string{
"rss_0.92": "Dave Winer: Grateful Dead",
"rss_1.0": "Golem.de",
"rss_2.0": "RSS Title",
"rss_2.0-1": "Liftoff News",
"atom_1.0": "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 TestEnclosure(t *testing.T) {
tests := map[string][]*Enclosure{
"rss_1.0": []*Enclosure{{Url: "http://foo.bar/baz.mp3", Type: "audio/mpeg", Length: 65535}},
"rss_2.0": []*Enclosure{{Url: "http://example.com/file.mp3", Type: "audio/mpeg", Length: 65535}},
"rss_2.0-1": []*Enclosure{{Url: "http://gdb.voanews.com/6C49CA6D-C18D-414D-8A51-2B7042A81010_cx0_cy29_cw0_w800_h450.jpg", Type: "image/jpeg", Length: 3123}},
"atom_1.0": []*Enclosure{{Url: "http://example.org/audio.mp3", Type: "audio/mpeg", Length: 1234}},
}
for test, want := range tests {
data, err := ioutil.ReadFile("testdata/" + test + "_enclosure")
if err != nil {
t.Fatalf("Reading %s: %v", test, err)
}
feed, err := Parse(data)
if err != nil {
t.Fatalf("Parsing %s: %v", test, err)
}
for _, item := range feed.Items {
if !reflect.DeepEqual(item.Enclosures, want) {
t.Errorf("%s: expected %#v, got %#v", test, want, item.Enclosures)
}
}
}
}
func MakeTestdataFetchFunc(file string) FetchFunc {
return func(url string) (resp *http.Response, err error) {
// Create mock http.Response
resp = new(http.Response)
resp.Body, err = os.Open("testdata/" + file)
return
}
}
func TestFeedUnmarshalUpdate(t *testing.T) {
fetch1 := MakeTestdataFetchFunc("rssupdate-1")
fetch2 := MakeTestdataFetchFunc("rssupdate-2")
feed, err := FetchByFunc(fetch1, "http://localhost/dummyrss")
if err != nil {
t.Fatalf("Failed fetching testdata 'rssupdate-2': %v", err)
}
if 1 != feed.Unread {
t.Errorf("Expected one unread item initially, got %v", feed.Unread)
}
jsonBlob, err := json.Marshal(feed)
if err != nil {
t.Fatalf("Failed to marshal Feed %+v\n", feed)
}
var unmarshalledFeed Feed
err = json.Unmarshal(jsonBlob, &unmarshalledFeed)
var defaultFetchFuncCalled = 0
DefaultFetchFunc = func(url string) (resp *http.Response, err error) {
defaultFetchFuncCalled++
return nil, errors.New("No network in test")
}
err = unmarshalledFeed.Update()
if err != nil {
t.Logf("Expected failure updating via http in test: %v", err)
}
if defaultFetchFuncCalled < 1 {
t.Error("DefaultFetchFunc was not called during Update()")
}
err = unmarshalledFeed.UpdateByFunc(fetch2)
if err != nil {
t.Fatalf("Failed updating the feed from testdata 'rssupdate-2': %v", err)
}
if 2 != unmarshalledFeed.Unread {
t.Errorf("Expected two unread items after update, got %v", unmarshalledFeed.Unread)
}
}