feat(sources): add managed RSS feeds (#41)
This commit is contained in:
126
backend/internal/scraper/quanwenrss/collector_test.go
Normal file
126
backend/internal/scraper/quanwenrss/collector_test.go
Normal file
@ -0,0 +1,126 @@
|
||||
package quanwenrss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/sourcefeeds"
|
||||
)
|
||||
|
||||
type loaderFunc func(context.Context, string) ([]string, error)
|
||||
|
||||
func (f loaderFunc) SourceFeedURLs(ctx context.Context, key string) ([]string, error) {
|
||||
return f(ctx, key)
|
||||
}
|
||||
|
||||
type fixtureTransport func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f fixtureTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
func fixtureRSS(id, date, body string) string {
|
||||
return `<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><item><title>Synthetic ` + id + `</title><link>https://www.bloomberg.com/news/articles/2026-01-02/` + id + `</link><guid>` + id + `</guid><pubDate>` + date + `</pubDate><content:encoded><![CDATA[<article><p>` + body + `</p></article>]]></content:encoded></item></channel></rss>`
|
||||
}
|
||||
func TestCollectorMergesAllConfiguredFeedsWithoutArticleRequestsAndPreservesPartialSuccess(t *testing.T) {
|
||||
urls := sourcefeeds.Defaults(Bloomberg)
|
||||
calls := 0
|
||||
client := &http.Client{Transport: fixtureTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if r.URL.Host != sourcefeeds.Host {
|
||||
t.Fatal("article URL or external host was fetched")
|
||||
}
|
||||
status := 200
|
||||
body := fixtureRSS("shared", "Fri, 02 Jan 2026 03:04:05 GMT", "Synthetic complete shared body.")
|
||||
if r.URL.String() == urls[1] {
|
||||
status = 503
|
||||
body = "PRIVATE raw upstream response"
|
||||
}
|
||||
if r.URL.String() == urls[2] {
|
||||
body = fixtureRSS("newer", "Fri, 02 Jan 2026 04:04:05 GMT", "Synthetic complete newer body.")
|
||||
}
|
||||
return &http.Response{StatusCode: status, Header: make(http.Header), Request: r, Body: io.NopCloser(strings.NewReader(body))}, nil
|
||||
})}
|
||||
c := New(Bloomberg, loaderFunc(func(context.Context, string) ([]string, error) { return urls, nil }), client)
|
||||
got, stats, err := c.ScrapeWithDiagnostics(context.Background(), nil)
|
||||
if err != nil || calls != 5 || stats != (scraper.FeedCounts{Attempted: 5, Succeeded: 4, Failed: 1}) || len(got) != 2 || got[0].Title != "Synthetic newer" || scraper.ContentStatus(got[0]) != scraper.ContentComplete {
|
||||
t.Fatal("multi-feed merge/partial outcome failed")
|
||||
}
|
||||
}
|
||||
func TestStoredFeedPolicyIsRevalidatedBeforeAnyTransport(t *testing.T) {
|
||||
for _, urls := range [][]string{nil, {"https://127.0.0.1/private"}, {sourcefeeds.Defaults(Investing)[0]}, {"https://feed.quanwenrss.com/feed/unknown.xml"}} {
|
||||
calls := 0
|
||||
c := New(Bloomberg, loaderFunc(func(context.Context, string) ([]string, error) { return urls, nil }), &http.Client{Transport: fixtureTransport(func(*http.Request) (*http.Response, error) { calls++; return nil, errors.New("PRIVATE") })})
|
||||
if _, _, err := c.ScrapeWithDiagnostics(context.Background(), nil); err == nil || calls != 0 {
|
||||
t.Fatal("invalid saved configuration reached transport")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestFeedRedirectsAndFailuresCannotLeakOrFetchArticleHosts(t *testing.T) {
|
||||
allowed := sourcefeeds.Defaults(Bloomberg)[0]
|
||||
for _, destination := range []string{"https://127.0.0.1/private", "https://www.bloomberg.com/news/articles/private", sourcefeeds.Defaults(Investing)[0], "https://feed.quanwenrss.com/feed/unknown.xml"} {
|
||||
calls := 0
|
||||
c := New(Bloomberg, loaderFunc(func(context.Context, string) ([]string, error) { return []string{allowed}, nil }), &http.Client{Transport: fixtureTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return &http.Response{StatusCode: 302, Header: http.Header{"Location": []string{destination}}, Request: r, Body: io.NopCloser(strings.NewReader("PRIVATE"))}, nil
|
||||
})})
|
||||
_, s, err := c.ScrapeWithDiagnostics(context.Background(), nil)
|
||||
if err == nil || calls != 1 || s.Failed != 1 || err.Error() != "rss_source_unavailable" {
|
||||
t.Fatal("unsafe redirect or raw upstream error escaped")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestGUIDMergeBridgesAliasesAndSourceSortingKeepsNullDatesLast(t *testing.T) {
|
||||
pools := [][]scraper.Article{}
|
||||
for i := 0; i < 3; i++ {
|
||||
raw := fixtureRSS(fmt.Sprintf("fixture-%d", i), "Fri, 02 Jan 2026 03:04:05 GMT", "Synthetic body.")
|
||||
raw = strings.Replace(raw, fmt.Sprintf("<guid>fixture-%d</guid>", i), "<guid>shared</guid>", 1)
|
||||
got, err := Parse([]byte(raw), Bloomberg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pools = append(pools, got)
|
||||
}
|
||||
got := Merge(pools)
|
||||
if len(got) != 1 || got[0].Title != "Synthetic fixture-0" {
|
||||
t.Fatal("stable GUID did not deduplicate aliases")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSourceBudgetAndAllFailedFeedsAreExplicitWithoutSilentTruncation(t *testing.T) {
|
||||
calls := 0
|
||||
urls := sourcefeeds.Defaults(Bloomberg)
|
||||
c := New(Bloomberg, loaderFunc(func(context.Context, string) ([]string, error) { return urls, nil }), &http.Client{Transport: fixtureTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return &http.Response{StatusCode: 200, Header: make(http.Header), Request: r, Body: io.NopCloser(strings.NewReader(strings.Repeat("x", MaxFeedBytes+1)))}, nil
|
||||
})})
|
||||
got, s, err := c.ScrapeWithDiagnostics(context.Background(), nil)
|
||||
if err == nil || got != nil || s.Succeeded != 0 || s.Failed != 5 || calls != 3 {
|
||||
t.Fatal("source byte budget did not stop body reads/requests or return explicit unavailable")
|
||||
}
|
||||
}
|
||||
func TestApprovedSameHostRedirectRemainsBoundedToFeedPaths(t *testing.T) {
|
||||
urls := sourcefeeds.Defaults(Bloomberg)
|
||||
calls := 0
|
||||
c := New(Bloomberg, loaderFunc(func(context.Context, string) ([]string, error) { return urls[:1], nil }), &http.Client{Transport: fixtureTransport(func(r *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
header := make(http.Header)
|
||||
status := 200
|
||||
body := fixtureRSS("fixture", "", "Synthetic complete body.")
|
||||
if calls == 1 {
|
||||
status = 302
|
||||
header.Set("Location", urls[1])
|
||||
}
|
||||
if calls > 2 || r.URL.Host != sourcefeeds.Host {
|
||||
t.Fatal("unbounded/external redirect")
|
||||
}
|
||||
return &http.Response{StatusCode: status, Header: header, Request: r, Body: io.NopCloser(strings.NewReader(body))}, nil
|
||||
})})
|
||||
got, s, err := c.ScrapeWithDiagnostics(context.Background(), nil)
|
||||
if err != nil || calls != 2 || s.Succeeded != 1 || len(got) != 1 || got[0].PublishedAt != nil {
|
||||
t.Fatal("approved redirect or nullable RSS date failed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user