fix: handle nil response request in fetch document

This commit is contained in:
2026-06-12 13:40:19 +02:00
parent ffe42a352b
commit 45b4a01801
2 changed files with 54 additions and 3 deletions

41
pkg/net/document_test.go Normal file
View File

@@ -0,0 +1,41 @@
package netutil
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
type roundTripFunc func(*http.Request) (*http.Response, error)
func (f roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) {
return f(request)
}
func TestFetchHTMLDocumentFallsBackToOriginalURLWhenResponseRequestMissing(t *testing.T) {
client := &http.Client{
Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("<!doctype html><html><body><main>ok</main></body></html>")),
}, nil
}),
}
url := "https://example.test/watch-order"
document, finalURL, err := FetchHTMLDocument(context.Background(), client, url, nil, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
if finalURL != url {
t.Fatalf("expected final url %q, got %q", url, finalURL)
}
if got := strings.TrimSpace(document.Find("main").Text()); got != "ok" {
t.Fatalf("expected document text ok, got %q", got)
}
}