Files
show-rss/src/feeds/http_test.go
2025-05-05 22:41:37 -06:00

99 lines
2.4 KiB
Go

package feeds_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"show-rss/src/feeds"
"testing"
"time"
)
func TestFeedFetch(t *testing.T) {
ctx := context.Background()
proxyU := feeds.ProxyU
feeds.ProxyU = nil
t.Cleanup(func() {
feeds.ProxyU = proxyU
})
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("%s", r.URL.String())
b, _ := os.ReadFile("testdata/slashdot.rss")
w.Write(b)
}))
t.Cleanup(s.Close)
created := time.Now().Add(-4 * time.Second)
feed := feeds.Feed{
Entry: feeds.Entry{
ID: "id",
Created: created,
Updated: created,
Deleted: time.Time{},
},
Version: feeds.Version{
Created: created,
URL: s.URL,
Cron: "* * * * *",
Pattern: ".*",
},
Execution: feeds.Execution{
Executed: created.Add(-2 * time.Second),
Version: created,
},
}
items, err := feed.Fetch(ctx)
if err != nil {
t.Fatalf("failed fetch: %v", err)
}
for i, item := range items {
t.Logf("[%d] %+v", i, item)
}
if len(items) != 15 {
t.Fatalf("expected 15 items but got %d", len(items))
}
if items[0].Body == "" {
t.Errorf("no body")
}
items[0].Body = ""
expect := feeds.Item{
Title: `Cheap 'Transforming' Electric Truck Announced by Jeff Bezos-Backed Startup`,
Link: `https://tech.slashdot.org/story/25/04/26/0425259/cheap-transforming-electric-truck-announced-by-jeff-bezos-backed-startup?utm_source=rss1.0mainlinkanon&utm_medium=feed`,
Links: []string{`https://tech.slashdot.org/story/25/04/26/0425259/cheap-transforming-electric-truck-announced-by-jeff-bezos-backed-startup?utm_source=rss1.0mainlinkanon&utm_medium=feed`},
Preview: `It's a pickup truck "that can change into whatever...`,
}
if fmt.Sprintf("%+v", items[0]) != fmt.Sprintf("%+v", expect) {
t.Errorf("expected\n\t%+v but got \n\t%+v", expect, items[0])
}
}
func TestFeedFetchURL(t *testing.T) {
cases := map[string]string{
"http://host/path?k=v": "http://host/path?k=v",
"https://host/path?k=v": "https://host/path?k=v",
"nyaa://?q=a b&u=c d": "https://nyaa.si/?c=0_0&f=0&page=rss&q=a+b&u=c+d",
}
for given, want := range cases {
given := given
want := want
t.Run(given, func(t *testing.T) {
f := feeds.Feed{}
f.Version.URL = given
got, _ := f.FetchURL()
if got := got.String(); got != want {
t.Errorf("expected %q but got %q", want, got)
}
})
}
}