feat(model): enhance LoadFeed with timeout support and add tests
This commit is contained in:
94
model/podcast_test.go
Normal file
94
model/podcast_test.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLoadFeed_Success(t *testing.T) {
|
||||
// Mock RSS feed
|
||||
rssFeed := `
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
<title>Test Podcast</title>
|
||||
<description>This is a test podcast</description>
|
||||
<link>http://example.com</link>
|
||||
<image>
|
||||
<url>http://example.com/image.jpg</url>
|
||||
</image>
|
||||
<copyright>Test Copyright</copyright>
|
||||
<language>en</language>
|
||||
<item>
|
||||
<title>Episode 1</title>
|
||||
<link>http://example.com/episode1</link>
|
||||
<description>Episode 1 description</description>
|
||||
</item>
|
||||
</channel>
|
||||
</rss>
|
||||
`
|
||||
|
||||
// Create a test server
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(rssFeed))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Call LoadFeed
|
||||
podcast, err := LoadFeed(server.URL, 10*time.Second)
|
||||
|
||||
// Assertions
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, podcast)
|
||||
assert.Equal(t, "Test Podcast", podcast.Title)
|
||||
assert.Equal(t, "This is a test podcast", podcast.Description)
|
||||
assert.Equal(t, "http://example.com", podcast.Link)
|
||||
assert.Equal(t, "http://example.com/image.jpg", podcast.Image)
|
||||
assert.Equal(t, "Test Copyright", podcast.Copyright)
|
||||
assert.Equal(t, "en", podcast.Language)
|
||||
assert.Len(t, podcast.Episodes, 1)
|
||||
assert.Equal(t, "Episode 1", podcast.Episodes[0].Title)
|
||||
}
|
||||
|
||||
func TestLoadFeed_Timeout(t *testing.T) {
|
||||
// Create a test server that delays response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(2 * time.Second)
|
||||
w.Write([]byte("<rss></rss>"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Call LoadFeed with a short timeout
|
||||
_, err := LoadFeed(server.URL, 1*time.Second)
|
||||
|
||||
// Assertions
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "context deadline exceeded")
|
||||
}
|
||||
|
||||
func TestLoadFeed_InvalidURL(t *testing.T) {
|
||||
// Call LoadFeed with an invalid URL
|
||||
_, err := LoadFeed("http://invalid-url", 10*time.Second)
|
||||
|
||||
// Assertions
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "解析播客订阅失败")
|
||||
}
|
||||
|
||||
func TestLoadFeed_EmptyFeed(t *testing.T) {
|
||||
// Create a test server with an empty feed
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(""))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Call LoadFeed
|
||||
_, err := LoadFeed(server.URL, 10*time.Second)
|
||||
|
||||
// Assertions
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "解析播客订阅失败")
|
||||
}
|
||||
Reference in New Issue
Block a user