Files
TradarrV2/backend/internal/ingestionbench/offline.go

68 lines
2.4 KiB
Go

package ingestionbench
import (
"embed"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
//go:embed testdata/body.html
var fixtureFiles embed.FS
// OfflineTransport serves synthetic responses only, rejecting every unlisted
// destination. It never delegates to a real network transport.
type OfflineTransport struct{}
func (OfflineTransport) RoundTrip(r *http.Request) (*http.Response, error) {
if r.Method != "GET" || r.URL.Scheme != "https" {
return nil, errors.New("offline_request_rejected")
}
urls := []string{}
root := "caas-body"
switch r.URL.Host {
case "feeds.finance.yahoo.com", "finance.yahoo.com":
urls = []string{"https://finance.yahoo.com/news/bench-complete.html", "https://finance.yahoo.com/news/bench-partial.html", "https://finance.yahoo.com/news/bench-absent.html"}
case "www.cnbc.com":
root = "ArticleBody-articleBody"
urls = []string{"https://www.cnbc.com/2026/01/02/bench-complete.html", "https://www.cnbc.com/2026/01/02/bench-partial.html", "https://www.cnbc.com/2026/01/02/bench-absent.html"}
default:
return nil, errors.New("offline_request_rejected")
}
body := ""
if r.URL.Host == "feeds.finance.yahoo.com" && r.URL.Path == "/rss/2.0/headline" || r.URL.Host == "www.cnbc.com" && r.URL.Path == "/id/10001147/device/rss/rss.html" {
body = "<rss><channel>"
for i, url := range urls {
date := ""
if i > 0 {
date = "<pubDate>Fri, 02 Jan 2026 03:04:05 +0000</pubDate>"
}
body += fmt.Sprintf("<item><title>Synthetic benchmark headline %d</title><link>%s</link><description>Synthetic RSS teaser.</description>%s</item>", i, url, date)
}
body += "</channel></rss>"
} else {
index := -1
for i, url := range urls {
if strings.Replace(url, "http://", "https://", 1) == r.URL.String() {
index = i
}
}
if index < 0 {
return nil, errors.New("offline_request_rejected")
}
raw, _ := fixtureFiles.ReadFile("testdata/body.html")
body = strings.ReplaceAll(strings.ReplaceAll(string(raw), "{{URL}}", r.URL.String()), "{{ROOT}}", root)
if index == 1 {
// Restriction belongs to the synthetic editorial body, not site chrome.
body = strings.Replace(body, "</div>", `<div class="paywall"></div></div>`, 1)
body += `<meta property="article:published_time" content="2026-01-02T03:04:05Z">`
}
if index == 2 {
body = `<div id="px-captcha"></div>`
}
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
}