feat(model): enhance LoadFeed with timeout support and add tests
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
)
|
||||
@@ -59,16 +60,19 @@ type Podcast struct {
|
||||
Medium Medium `json:"medium,omitempty"`
|
||||
}
|
||||
|
||||
// LoadFeed 从URL加载播客RSS订阅
|
||||
func LoadFeed(url string) (*Podcast, error) {
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
|
||||
// LoadFeed 从URL加载播客RSS订阅,timeout为超时时间,如果timeout<=0则使用默认值30秒
|
||||
func LoadFeed(url string, timeout time.Duration) (*Podcast, error) {
|
||||
if timeout <= 0 {
|
||||
timeout = 30 * time.Second
|
||||
}
|
||||
|
||||
if feed == nil {
|
||||
return nil, errors.New("解析播客订阅失败: 空订阅")
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
fp := gofeed.NewParser()
|
||||
feed, err := fp.ParseURLWithContext(url, ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("解析播客订阅失败: %w", err)
|
||||
}
|
||||
|
||||
imageURL := ""
|
||||
|
||||
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