File: rss_test.go

package info (click to toggle)
golang-github-tideland-golib 4.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,144 kB
  • sloc: makefile: 4
file content (138 lines) | stat: -rw-r--r-- 3,616 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Tideland Go Library - RSS Feed - Unit Tests
//
// Copyright (C) 2012-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package rss_test

//--------------------
// IMPORTS
//--------------------

import (
	"bytes"
	"net/url"
	"testing"
	"time"

	"github.com/tideland/golib/audit"
	"github.com/tideland/golib/feed/rss"
)

//--------------------
// TESTS
//--------------------

// Test parsing and composing of date/times.
func TestParseComposeTime(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	nowOne := time.Now()
	strOne := rss.ComposeTime(nowOne)

	nowTwo, err := rss.ParseTime(strOne)
	strTwo := rss.ComposeTime(nowTwo)

	assert.Nil(err)
	assert.Equal(strOne, strTwo)

	// Now some tests with different date formats.
	_, err = rss.ParseTime("21 Jun 2012 23:00 CEST")
	assert.Nil(err, "No error during time parsing.")
	_, err = rss.ParseTime("Thu, 21 Jun 2012 23:00 CEST")
	assert.Nil(err, "No error during time parsing.")
	_, err = rss.ParseTime("Thu, 21 Jun 2012 23:00 +0100")
	assert.Nil(err, "No error during time parsing.")
}

// Test encoding and decoding a doc.
func TestEncodeDecode(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	r1 := &rss.RSS{
		Version: rss.Version,
		Channel: rss.Channel{
			Title:       "Test Encode/Decode",
			Link:        "http://www.tideland.biz/rss",
			Description: "A test document.",
			Categories: []*rss.Category{
				{"foo", ""},
				{"bar", "baz"},
			},
			Items: []*rss.Item{
				{
					Title:       "Item 1",
					Description: "This is item 1",
					GUID:        &rss.GUID{"http://www.tideland.biz/rss/item-1", false},
				},
				{
					Title:       "Item 2",
					Description: "This is item 2",
					GUID:        &rss.GUID{"http://www.tideland.biz/rss/item-2", true},
				},
			},
		},
	}
	b := &bytes.Buffer{}

	err := rss.Encode(b, r1)
	assert.Nil(err, "Encoding returns no error.")
	assert.Substring("<title>Test Encode/Decode</title>", b.String(), "Title has been encoded correctly.")

	r2, err := rss.Decode(b)
	assert.Nil(err, "Decoding returns no error.")
	assert.Equal(r2.Channel.Title, "Test Encode/Decode", "Title has been decoded correctly.")
	assert.Length(r2.Channel.Items, 2, "Decoded document has the right number of items.")
}

// Test validating a doc.
func TestValidate(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	r := &rss.RSS{
		Version: "1.2.3",
	}
	err := r.Validate()
	assert.ErrorMatch(err, `.* invalid RSS document version: "1.2.3"`)
	r = &rss.RSS{
		Version: rss.Version,
	}
	err = r.Validate()
	assert.ErrorMatch(err, `.* channel title must not be empty`)
	r = &rss.RSS{
		Version: rss.Version,
		Channel: rss.Channel{
			Title: "Test Title",
		},
	}
	err = r.Validate()
	assert.ErrorMatch(err, `.* channel description must not be empty`)
	r = &rss.RSS{
		Version: rss.Version,
		Channel: rss.Channel{
			Title:       "Test Title",
			Description: "Test Description",
		},
	}
	err = r.Validate()
	assert.ErrorMatch(err, `.* channel link must not be empty`)
}

// Test getting a doc.
func TestGet(t *testing.T) {
	if testing.Short() {
		t.Skip("Network test skipped in short mode")
	}

	assert := audit.NewTestingAssertion(t, true)
	u, _ := url.Parse("http://www.rssboard.org/files/sample-rss-2.xml")
	r, err := rss.Get(u)
	assert.Nil(err, "Getting the RSS document returns no error.")
	err = r.Validate()
	assert.Nil(err, "Validating returns no error.")
	b := &bytes.Buffer{}
	err = rss.Encode(b, r)
	assert.Nil(err, "Encoding returns no error.")
	assert.Logf("--- RSS ---\n%s", b)
}

// EOF