-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdetector_test.go
More file actions
68 lines (60 loc) · 1.94 KB
/
Copy pathdetector_test.go
File metadata and controls
68 lines (60 loc) · 1.94 KB
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
package gofeed_test
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"testing"
"testing/iotest"
"github.com/mmcdole/gofeed"
"github.com/mmcdole/gofeed/internal/testutil"
"github.com/stretchr/testify/assert"
)
func TestDetectFeedType(t *testing.T) {
var feedTypeTests = []struct {
file string
expected gofeed.FeedType
}{
{"atom03_feed.xml", gofeed.FeedTypeAtom},
{"atom10_feed.xml", gofeed.FeedTypeAtom},
{"rss_feed.xml", gofeed.FeedTypeRSS},
{"rss_feed_bom.xml", gofeed.FeedTypeRSS},
{"rss_feed_leading_spaces.xml", gofeed.FeedTypeRSS},
{"rdf_feed.xml", gofeed.FeedTypeRSS},
{"unknown_feed.xml", gofeed.FeedTypeUnknown},
{"empty_feed.xml", gofeed.FeedTypeUnknown},
{"json10_feed.json", gofeed.FeedTypeJSON},
}
for _, test := range feedTypeTests {
t.Run(test.file, func(t *testing.T) {
data := testutil.ReadFile(t, "testdata/parser/universal/"+test.file)
actual := gofeed.DetectFeedType(bytes.NewReader(data))
assert.Equal(t, test.expected, actual)
})
}
}
// Examples
func ExampleDetectFeedType() {
feedData := `<rss version="2.0">
<channel>
<title>Sample Feed</title>
</channel>
</rss>`
feedType := gofeed.DetectFeedType(strings.NewReader(feedData))
if feedType == gofeed.FeedTypeRSS {
fmt.Println("Wow! This is an RSS feed!")
}
}
// A reader that fails mid-stream must yield FeedTypeUnknown, not a type
// guessed from the partial prefix (issue #311).
func TestDetectFeedType_ReaderError(t *testing.T) {
r := io.MultiReader(strings.NewReader(`<rss version="2.0"></rss>`), iotest.ErrReader(errors.New("boom")))
assert.Equal(t, gofeed.FeedTypeUnknown, gofeed.DetectFeedType(r))
}
// JSON detection must work from a bounded prefix; the JSON parser validates
// the complete document after detection (issue #344).
func TestDetectFeedType_JSONPrefix(t *testing.T) {
prefix := `{"version":"https://jsonfeed.org/version/1.1","items":[{`
assert.Equal(t, gofeed.FeedTypeJSON, gofeed.DetectFeedType(strings.NewReader(prefix)))
}