diff --git a/backend/internal/scraper/watcherguru/diagnostics_ticket20_test.go b/backend/internal/scraper/watcherguru/diagnostics_ticket20_test.go
new file mode 100644
index 0000000..7786b10
--- /dev/null
+++ b/backend/internal/scraper/watcherguru/diagnostics_ticket20_test.go
@@ -0,0 +1,78 @@
+package watcherguru
+
+import (
+ "context"
+ "io"
+ "net/http"
+ "os"
+ "strings"
+ "testing"
+)
+
+type ticket20RoundTrip func(*http.Request) (*http.Response, error)
+
+func (f ticket20RoundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
+
+func ticket20CaptureStdout(t *testing.T, run func()) string {
+ t.Helper()
+ reader, writer, err := os.Pipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ previous := os.Stdout
+ os.Stdout = writer
+ defer func() { os.Stdout = previous }()
+ run()
+ if err := writer.Close(); err != nil {
+ t.Fatal(err)
+ }
+ data, err := io.ReadAll(reader)
+ _ = reader.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+ return string(data)
+}
+
+func TestTicket20WatcherGuruDiagnosticsReportBothRSSAttemptsAndEmptyHTMLWithoutContent(t *testing.T) {
+ const rssPrivate = "PRIVATE_WATCHERGURU_RSS_RESPONSE_DO_NOT_LOG"
+ const htmlPrivate = "PRIVATE_WATCHERGURU_HTML_RESPONSE_DO_NOT_LOG"
+ var paths []string
+ watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
+ paths = append(paths, r.URL.Path)
+ switch r.URL.Path {
+ case "/feed/":
+ return &http.Response{StatusCode: http.StatusBadGateway, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`` + rssPrivate + ``)), Request: r}, nil
+ case "/news/feed/":
+ return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`
no candidate article
`)), Request: r}, nil + default: + return nil, io.ErrUnexpectedEOF + } + })}} + var count int + logs := ticket20CaptureStdout(t, func() { + articles, err := watcher.Scrape(context.Background(), nil) + if err != nil { + t.Fatalf("Scrape returned error: %v", err) + } + count = len(articles) + }) + if strings.Join(paths, ",") != "/feed/,/news/feed/,/news/" { + t.Fatalf("fake request path sequence=%v", paths) + } + for _, diagnostic := range []string{"rss feed=/feed/ status=502", "rss feed=/news/feed/ status=200 articles=0", "html status=200 articles=0"} { + if !strings.Contains(logs, diagnostic) { + t.Errorf("WatcherGuru diagnostic lacks %q: %q", diagnostic, logs) + } + } + if count != 0 { + t.Errorf("articles=%d, want 0", count) + } + for _, secret := range []string{rssPrivate, htmlPrivate, "no candidate article"} { + if strings.Contains(logs, secret) { + t.Errorf("WatcherGuru diagnostics leaked response/article content %q", secret) + } + } +} diff --git a/backend/internal/scraper/watcherguru/watcherguru.go b/backend/internal/scraper/watcherguru/watcherguru.go index 1d34e25..7097bcf 100644 --- a/backend/internal/scraper/watcherguru/watcherguru.go +++ b/backend/internal/scraper/watcherguru/watcherguru.go @@ -4,7 +4,6 @@ import ( "context" "encoding/xml" "fmt" - "io" "net/http" "strings" "time" @@ -45,7 +44,6 @@ func (w *WatcherGuru) Scrape(ctx context.Context, _ []string) ([]scraper.Article } { articles, err := w.fetchRSS(ctx, feedURL) if err == nil && len(articles) > 0 { - fmt.Printf("watcherguru rss: %d articles\n", len(articles)) return articles, nil } } @@ -55,11 +53,24 @@ func (w *WatcherGuru) Scrape(ctx context.Context, _ []string) ([]scraper.Article if err != nil { return nil, fmt.Errorf("watcherguru: %w", err) } - fmt.Printf("watcherguru html: %d articles\n", len(articles)) return articles, nil } -func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.Article, error) { +func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) (articles []scraper.Article, resultErr error) { + status := 0 + feedLabel := "unknown" + if feedURL == baseURL+"/feed/" { + feedLabel = "/feed/" + } else if feedURL == baseURL+"/news/feed/" { + feedLabel = "/news/feed/" + } + defer func() { + outcome := "ok" + if resultErr != nil { + outcome = "failed" + } + fmt.Printf("watcherguru rss feed=%s status=%d articles=%d outcome=%s\n", feedLabel, status, len(articles), outcome) + }() req, err := http.NewRequestWithContext(ctx, http.MethodGet, feedURL, nil) if err != nil { return nil, err @@ -72,6 +83,7 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A } defer resp.Body.Close() + status = resp.StatusCode if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("HTTP %d", resp.StatusCode) } @@ -81,7 +93,6 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A return nil, fmt.Errorf("parse RSS: %w", err) } - var articles []scraper.Article for _, item := range feed.Channel.Items { title := strings.TrimSpace(item.Title) link := strings.TrimSpace(item.Link) @@ -109,7 +120,15 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A return articles, nil } -func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error) { +func (w *WatcherGuru) scrapeHTML(ctx context.Context) (articles []scraper.Article, resultErr error) { + status := 0 + defer func() { + outcome := "ok" + if resultErr != nil { + outcome = "failed" + } + fmt.Printf("watcherguru html status=%d articles=%d outcome=%s\n", status, len(articles), outcome) + }() req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/news/", nil) if err != nil { return nil, err @@ -123,9 +142,9 @@ func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error) } defer resp.Body.Close() + status = resp.StatusCode if resp.StatusCode != http.StatusOK { - body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) - return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body) + return nil, fmt.Errorf("HTTP %d", resp.StatusCode) } doc, err := html.Parse(resp.Body) @@ -133,7 +152,6 @@ func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error) return nil, fmt.Errorf("parse HTML: %w", err) } - var articles []scraper.Article seen := make(map[string]bool) now := time.Now() diff --git a/backend/internal/scraper/yahoofinance/diagnostics_ticket20_test.go b/backend/internal/scraper/yahoofinance/diagnostics_ticket20_test.go new file mode 100644 index 0000000..bcd8e06 --- /dev/null +++ b/backend/internal/scraper/yahoofinance/diagnostics_ticket20_test.go @@ -0,0 +1,99 @@ +package yahoofinance + +import ( + "context" + "fmt" + "io" + "net/http" + "os" + "reflect" + "strings" + "testing" +) + +type ticket20RoundTrip func(*http.Request) (*http.Response, error) + +func (f ticket20RoundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } + +func ticket20CaptureStdout(t *testing.T, run func()) string { + t.Helper() + reader, writer, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + previous := os.Stdout + os.Stdout = writer + defer func() { os.Stdout = previous }() + run() + if err := writer.Close(); err != nil { + t.Fatal(err) + } + data, err := io.ReadAll(reader) + _ = reader.Close() + if err != nil { + t.Fatal(err) + } + return string(data) +} + +func TestTicket20Yahoo400DiagnosticConnectsSymbolsStatusAndCountWithoutResponseBody(t *testing.T) { + const privateHTML = `