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 := ` Test Podcast This is a test podcast http://example.com http://example.com/image.jpg Test Copyright en Episode 1 http://example.com/episode1 Episode 1 description ` // 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("")) })) 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(), "解析播客订阅失败") }