mirror of
https://github.com/xlionjuan/searxng-mcp-go.git
synced 2026-11-17 04:17:51 +00:00
* chore(deps): update dependency go to v1.27.1 * Fixed all lints; just verify passes. Co-authored-by: xlionjuan <[email protected]> * Fixed `.golangci.yml` paths→path; `just verify` passes. Co-authored-by: xlionjuan <[email protected]> * Parallelized golden & error-path tests; verify passes. Co-authored-by: xlionjuan <[email protected]> * Verify passes; HTTPClient/Logger nil set; exhaustruct covers Config. Co-authored-by: xlionjuan <[email protected]> --------- Co-authored-by: xlion-bot-renovate[bot] <218815870+xlion-bot-renovate[bot]@users.noreply.github.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: xlionjuan <[email protected]>
1272 lines
34 KiB
Go
1272 lines
34 KiB
Go
package searxng
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"searxng-mcp-go/internal/testhelper"
|
|
)
|
|
|
|
var (
|
|
errTestNestedFallbackTransport = errors.New("nested fallback transport failure")
|
|
errTestSecondaryFallbackTransport = errors.New("secondary fallback transport failure")
|
|
errTestNoQueryTransport = errors.New("transport failed without a query")
|
|
)
|
|
|
|
// closeCounter wraps an io.ReadCloser and counts Close calls into a shared
|
|
// counter. Use in regression tests to detect double-close bugs — each HTTP
|
|
// response body should be closed exactly once.
|
|
type closeCounter struct {
|
|
io.ReadCloser
|
|
|
|
mu *sync.Mutex
|
|
total *int
|
|
}
|
|
|
|
func (c *closeCounter) Close() error {
|
|
c.mu.Lock()
|
|
*c.total++
|
|
c.mu.Unlock()
|
|
|
|
return c.ReadCloser.Close()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Search function tests (full flow via mock HTTP client)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func TestSearch_NilSearcherCtx(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("nil searcherCtx does not panic", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
endpoint, err := computeSearchEndpoint("https://search.example.com")
|
|
if err != nil {
|
|
t.Fatalf("computeSearchEndpoint() error = %v", err)
|
|
}
|
|
|
|
s := &SearXNGSearcher{
|
|
client: &http.Client{
|
|
Transport: testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}),
|
|
},
|
|
searchEndpoint: endpoint,
|
|
retryStrategy: newExponentialBackoffStrategy(0, time.Microsecond, time.Microsecond),
|
|
}
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
}
|
|
|
|
if result.Query != "test" {
|
|
t.Fatalf("Query = %q, want %q", result.Query, "test")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_ValidationError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("empty query returns validation error", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return nil, errTransportNotExpected
|
|
}), 0)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: ""})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want validation error")
|
|
}
|
|
|
|
if _, ok := errors.AsType[*ValidationError](err); !ok {
|
|
t.Fatalf("error type = %T, want *ValidationError", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_Success(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("successful search returns result", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 0)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if result.Query != "test" {
|
|
t.Fatalf("Query = %q, want %q", result.Query, "test")
|
|
}
|
|
})
|
|
|
|
t.Run("search with args passes through all fields", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := 5
|
|
pageno := 1
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(3)), nil
|
|
}), 0)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{
|
|
Query: "golang testing",
|
|
Language: "en",
|
|
SafeSearch: 1,
|
|
TimeRange: "month",
|
|
Categories: "general",
|
|
Engines: "google,bing",
|
|
Pageno: &pageno,
|
|
Limit: &limit,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if len(result.Results) != 3 {
|
|
t.Fatalf("len(Results) = %d, want 3", len(result.Results))
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_SuccessWithResults(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("search with results returns limited results", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
limit := 2
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(5)), nil
|
|
}), 0)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{
|
|
Query: "test results",
|
|
Limit: &limit,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if len(result.Results) != 2 {
|
|
t.Fatalf("len(Results) = %d, want 2 (limited)", len(result.Results))
|
|
}
|
|
})
|
|
}
|
|
|
|
//nolint:gocognit // subtests cover distinct retry, exhaustion, and cancellation branches
|
|
func TestSearch_RetryOnError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("transient error is retried and succeeds", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
if callCount == 1 {
|
|
return nil, errRetryTestConnectionReset
|
|
}
|
|
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 2)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if callCount != 2 {
|
|
t.Fatalf("callCount = %d, want 2 (initial + retry)", callCount)
|
|
}
|
|
})
|
|
|
|
t.Run("all retries exhausted returns error", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
maxRetries := 2
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return nil, errRetryTestConnectionReset
|
|
}), maxRetries)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error after exhausting retries")
|
|
}
|
|
|
|
expectedCalls := maxRetries + 1 // loop runs maxRetries+1 times
|
|
if callCount != expectedCalls {
|
|
t.Fatalf("callCount = %d, want %d (maxRetries+1 attempts)", callCount, expectedCalls)
|
|
}
|
|
})
|
|
|
|
t.Run("context cancellation stops retry", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return nil, errRetryTestConnectionReset
|
|
}), 5)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel() // Already canceled
|
|
|
|
_, err := s.Search(ctx, &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error for canceled context")
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("callCount = %d, want 1 (first attempt stops when context is canceled)", callCount)
|
|
}
|
|
})
|
|
|
|
t.Run("canceled context with successful response surfaces context.Canceled", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 0)
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel() // Pre-cancel: searchCtx.Err() is non-nil when classifyAttempt runs
|
|
|
|
_, err := s.Search(ctx, &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want context.Canceled in chain")
|
|
}
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("Search() error chain missing context.Canceled: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_RetryOnStatusCode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("retryable status code 429 triggers retry", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
if callCount == 1 {
|
|
return &http.Response{
|
|
StatusCode: http.StatusTooManyRequests,
|
|
Header: http.Header{"Content-Type": []string{"text/plain"}},
|
|
Body: io.NopCloser(strings.NewReader("rate limited")),
|
|
}, nil
|
|
}
|
|
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 2)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if callCount != 2 {
|
|
t.Fatalf("callCount = %d, want 2 (initial + retry)", callCount)
|
|
}
|
|
})
|
|
|
|
t.Run("non-retryable status code 404 does not retry", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusNotFound,
|
|
Header: http.Header{"Content-Type": []string{"text/plain"}},
|
|
Body: io.NopCloser(strings.NewReader("not found")),
|
|
}, nil
|
|
}), 2)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error for 404")
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("callCount = %d, want 1 (no retry for 404)", callCount)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_RetryOnEmptyResponse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("empty response retries and gets data", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
if callCount == 1 {
|
|
// Return empty results — will retry
|
|
return makeJSONResponse(`{"query":"test","results":[],"suggestions":[],"answers":[],"infoboxes":[]}`), nil
|
|
}
|
|
|
|
// Return with results
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 2)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if callCount != 2 {
|
|
t.Fatalf("callCount = %d, want 2 (first empty, second with results)", callCount)
|
|
}
|
|
|
|
if len(result.Results) != 1 {
|
|
t.Fatalf("len(Results) = %d, want 1", len(result.Results))
|
|
}
|
|
})
|
|
}
|
|
|
|
//nolint:gocyclo // assertions cover redaction, diagnostics, identity, and request count independently
|
|
func TestSearch_RedactsPOSTRedirectErrorURL(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const secret = "post-location-secret"
|
|
|
|
var requestCount atomic.Int32
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
requestCount.Add(1)
|
|
w.Header().Set("Location", "/canonical?q="+secret)
|
|
w.WriteHeader(http.StatusFound)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
s, err := NewSearXNGSearcher(&Config{
|
|
SearXNGURL: server.URL,
|
|
HTTPClient: server.Client(),
|
|
MaxRetries: 2,
|
|
}, false)
|
|
if err != nil {
|
|
t.Fatalf("NewSearXNGSearcher() error = %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
closeErr := s.Close()
|
|
if closeErr != nil {
|
|
t.Errorf("Close() error = %v", closeErr)
|
|
}
|
|
})
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want redirect policy error")
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
errText := err.Error()
|
|
if strings.Contains(errText, secret) || strings.Contains(errText, "?q=") {
|
|
t.Fatalf("Search() error exposed redirect query: %q", errText)
|
|
}
|
|
|
|
if !strings.Contains(errText, "/canonical") {
|
|
t.Fatalf("Search() error = %q, want redirect path diagnostic", errText)
|
|
}
|
|
|
|
if !strings.Contains(errText, "redirect would change POST to GET") {
|
|
t.Fatalf("Search() error = %q, want redirect method context", errText)
|
|
}
|
|
|
|
if !errors.Is(err, errRedirectMethodChanged) {
|
|
t.Fatalf("Search() error = %v, want errRedirectMethodChanged in chain", err)
|
|
}
|
|
|
|
var urlErr *url.Error
|
|
if !errors.As(err, &urlErr) {
|
|
t.Fatalf("Search() error type = %T, want *url.Error in chain", err)
|
|
}
|
|
|
|
if !strings.Contains(urlErr.URL, secret) {
|
|
t.Fatalf("original url.Error.URL = %q, want original redirect query preserved internally", urlErr.URL)
|
|
}
|
|
|
|
if _, ok := errors.AsType[*SearXNGError](err); !ok {
|
|
t.Fatalf("Search() error type = %T, want *SearXNGError in chain", err)
|
|
}
|
|
|
|
if got := requestCount.Load(); got != 1 {
|
|
t.Fatalf("HTTP request count = %d, want 1 (redirect rejected before follow)", got)
|
|
}
|
|
}
|
|
|
|
//nolint:gocyclo,cyclop // assertions cover every nested/joined redaction and traversal invariant
|
|
func TestSearch_RedactsNestedGETFallbackErrorURLs(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const (
|
|
outerSecret = "fallback-outer-secret"
|
|
nestedSecret = "fallback-nested-secret"
|
|
)
|
|
|
|
nestedURLError := &url.Error{
|
|
Op: "dial",
|
|
URL: "https://nested.example/failure?q=" + nestedSecret,
|
|
Err: errTestNestedFallbackTransport,
|
|
}
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
if req.Method == http.MethodPost {
|
|
return &http.Response{
|
|
StatusCode: http.StatusMethodNotAllowed,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("POST not allowed")),
|
|
Request: req,
|
|
}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf(
|
|
"wrapped fallback failure: %w",
|
|
errors.Join(nestedURLError, errTestSecondaryFallbackTransport),
|
|
)
|
|
}), 0)
|
|
s.allowGETFallback = true
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: outerSecret})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want GET fallback error")
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
errText := err.Error()
|
|
for _, secret := range []string{outerSecret, nestedSecret} {
|
|
if strings.Contains(errText, secret) {
|
|
t.Fatalf("Search() error exposed %q: %q", secret, errText)
|
|
}
|
|
}
|
|
|
|
if strings.Contains(errText, "?q=") || strings.Contains(errText, "format=") {
|
|
t.Fatalf("Search() error exposed search query parameters: %q", errText)
|
|
}
|
|
|
|
for _, diagnostic := range []string{
|
|
"GET fallback was used",
|
|
"search method rejected",
|
|
"https://search.example.com/search",
|
|
"https://nested.example/failure",
|
|
} {
|
|
if !strings.Contains(errText, diagnostic) {
|
|
t.Fatalf("Search() error = %q, want diagnostic %q", errText, diagnostic)
|
|
}
|
|
}
|
|
|
|
if !errors.Is(err, errSearchMethodRejected) {
|
|
t.Fatalf("Search() error = %v, want errSearchMethodRejected in chain", err)
|
|
}
|
|
|
|
if !errors.Is(err, errTestNestedFallbackTransport) {
|
|
t.Fatalf("Search() error = %v, want nested transport sentinel in chain", err)
|
|
}
|
|
|
|
if !errors.Is(err, errTestSecondaryFallbackTransport) {
|
|
t.Fatalf("Search() error = %v, want joined transport sentinel in chain", err)
|
|
}
|
|
|
|
if !errors.Is(err, nestedURLError) {
|
|
t.Fatalf("Search() error = %v, want original nested *url.Error identity in chain", err)
|
|
}
|
|
|
|
var urlErr *url.Error
|
|
if !errors.As(err, &urlErr) {
|
|
t.Fatalf("Search() error type = %T, want *url.Error in chain", err)
|
|
}
|
|
|
|
if !strings.Contains(urlErr.URL, outerSecret) {
|
|
t.Fatalf("original outer url.Error.URL = %q, want fallback query preserved internally", urlErr.URL)
|
|
}
|
|
|
|
var searxErr *SearXNGError
|
|
if !errors.As(err, &searxErr) {
|
|
t.Fatalf("Search() error type = %T, want *SearXNGError in chain", err)
|
|
}
|
|
|
|
if searxErr.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Fatalf("SearXNGError.StatusCode = %d, want %d", searxErr.StatusCode, http.StatusMethodNotAllowed)
|
|
}
|
|
}
|
|
|
|
func TestDoHTTPPreservesNoQueryURLDiagnostic(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := &SearXNGSearcher{
|
|
client: &http.Client{
|
|
Transport: testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return nil, errTestNoQueryTransport
|
|
}),
|
|
},
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
t.Context(),
|
|
http.MethodGet,
|
|
"https://search.example.com/health",
|
|
http.NoBody,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("http.NewRequestWithContext() error = %v", err)
|
|
}
|
|
|
|
resp, err := s.doHTTP(req)
|
|
closeBody(resp)
|
|
|
|
if err == nil {
|
|
t.Fatal("doHTTP() error = nil, want transport error")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "https://search.example.com/health") {
|
|
t.Fatalf("doHTTP() error = %q, want no-query URL diagnostic", err.Error())
|
|
}
|
|
|
|
if strings.Contains(err.Error(), "[REDACTED]") {
|
|
t.Fatalf("doHTTP() error unnecessarily redacted no-query URL: %q", err.Error())
|
|
}
|
|
|
|
if !errors.Is(err, errTestNoQueryTransport) {
|
|
t.Fatalf("doHTTP() error = %v, want transport sentinel in chain", err)
|
|
}
|
|
|
|
if _, ok := errors.AsType[*url.Error](err); !ok {
|
|
t.Fatalf("doHTTP() error type = %T, want *url.Error in chain", err)
|
|
}
|
|
}
|
|
|
|
// Regression test: http.Client closes a response body before returning a
|
|
// CheckRedirect error, so ownership must not transfer to the retry loop.
|
|
func TestSearch_RedirectPolicyErrorBodyOwnership(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
closeCalls int
|
|
transportCalls int
|
|
)
|
|
|
|
client := &http.Client{
|
|
Transport: testhelper.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
transportCalls++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusTemporaryRedirect,
|
|
Header: http.Header{"Location": []string{"https://other.example.com/redirected"}},
|
|
Body: &closeCounter{
|
|
ReadCloser: io.NopCloser(strings.NewReader("redirect")),
|
|
mu: &mu,
|
|
total: &closeCalls,
|
|
},
|
|
Request: req,
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
s, err := NewSearXNGSearcher(&Config{
|
|
SearXNGURL: "https://search.example.com",
|
|
HTTPClient: client,
|
|
MaxRetries: 2,
|
|
}, false)
|
|
if err != nil {
|
|
t.Fatalf("NewSearXNGSearcher() error = %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
closeErr := s.Close()
|
|
if closeErr != nil {
|
|
t.Errorf("Close() error = %v", closeErr)
|
|
}
|
|
})
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want redirect error")
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
if !errors.Is(err, errRedirectDifferentHost) {
|
|
t.Fatalf("Search() error = %v, want errRedirectDifferentHost in chain", err)
|
|
}
|
|
|
|
if _, ok := errors.AsType[*url.Error](err); !ok {
|
|
t.Fatalf("Search() error type = %T, want *url.Error in chain", err)
|
|
}
|
|
|
|
if transportCalls != 1 {
|
|
t.Fatalf("RoundTrip call count = %d, want 1", transportCalls)
|
|
}
|
|
|
|
mu.Lock()
|
|
gotCloseCalls := closeCalls
|
|
mu.Unlock()
|
|
|
|
if gotCloseCalls != 1 {
|
|
t.Fatalf("response body Close() calls = %d, want 1 from http.Client", gotCloseCalls)
|
|
}
|
|
}
|
|
|
|
// Regression test: the GET fallback must preserve the same ownership boundary
|
|
// when its redirect is rejected by http.Client's CheckRedirect policy.
|
|
func TestSearch_GETFallbackRedirectPolicyErrorBodyOwnership(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
getCloseCalls int
|
|
postCalls int
|
|
getCalls int
|
|
)
|
|
|
|
client := &http.Client{
|
|
Transport: testhelper.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
|
|
switch req.Method {
|
|
case http.MethodPost:
|
|
postCalls++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusMethodNotAllowed,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("POST not allowed")),
|
|
Request: req,
|
|
}, nil
|
|
case http.MethodGet:
|
|
getCalls++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusTemporaryRedirect,
|
|
Header: http.Header{"Location": []string{"https://other.example.com/redirected"}},
|
|
Body: &closeCounter{
|
|
ReadCloser: io.NopCloser(strings.NewReader("redirect")),
|
|
mu: &mu,
|
|
total: &getCloseCalls,
|
|
},
|
|
Request: req,
|
|
}, nil
|
|
default:
|
|
return nil, errTransportNotExpected
|
|
}
|
|
}),
|
|
}
|
|
|
|
s, err := NewSearXNGSearcher(&Config{
|
|
SearXNGURL: "https://search.example.com",
|
|
HTTPClient: client,
|
|
MaxRetries: 2,
|
|
AllowGETFallback: true,
|
|
}, false)
|
|
if err != nil {
|
|
t.Fatalf("NewSearXNGSearcher() error = %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
closeErr := s.Close()
|
|
if closeErr != nil {
|
|
t.Errorf("Close() error = %v", closeErr)
|
|
}
|
|
})
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want GET fallback redirect error")
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
if !errors.Is(err, errSearchMethodRejected) {
|
|
t.Fatalf("Search() error = %v, want errSearchMethodRejected in chain", err)
|
|
}
|
|
|
|
if !errors.Is(err, errRedirectDifferentHost) {
|
|
t.Fatalf("Search() error = %v, want errRedirectDifferentHost in chain", err)
|
|
}
|
|
|
|
if _, ok := errors.AsType[*url.Error](err); !ok {
|
|
t.Fatalf("Search() error type = %T, want *url.Error in chain", err)
|
|
}
|
|
|
|
mu.Lock()
|
|
gotPostCalls := postCalls
|
|
gotGETCalls := getCalls
|
|
gotCloseCalls := getCloseCalls
|
|
mu.Unlock()
|
|
|
|
if gotPostCalls != 1 {
|
|
t.Fatalf("POST RoundTrip call count = %d, want 1", gotPostCalls)
|
|
}
|
|
|
|
if gotGETCalls != 1 {
|
|
t.Fatalf("GET RoundTrip call count = %d, want 1", gotGETCalls)
|
|
}
|
|
|
|
if gotCloseCalls != 1 {
|
|
t.Fatalf("GET fallback response body Close() calls = %d, want 1 from http.Client", gotCloseCalls)
|
|
}
|
|
}
|
|
|
|
func TestSearch_ErrUseLastResponseBodyOwnership(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
closeCalls int
|
|
)
|
|
|
|
client := &http.Client{
|
|
Transport: testhelper.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusTemporaryRedirect,
|
|
Header: http.Header{"Location": []string{"https://search.example.com/redirected"}},
|
|
Body: &closeCounter{
|
|
ReadCloser: io.NopCloser(strings.NewReader("redirect")),
|
|
mu: &mu,
|
|
total: &closeCalls,
|
|
},
|
|
Request: req,
|
|
}, nil
|
|
}),
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
return http.ErrUseLastResponse
|
|
},
|
|
}
|
|
|
|
s, err := NewSearXNGSearcher(&Config{
|
|
SearXNGURL: "https://search.example.com",
|
|
HTTPClient: client,
|
|
}, false)
|
|
if err != nil {
|
|
t.Fatalf("NewSearXNGSearcher() error = %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
closeErr := s.Close()
|
|
if closeErr != nil {
|
|
t.Errorf("Close() error = %v", closeErr)
|
|
}
|
|
})
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want status error")
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
var searxErr *SearXNGError
|
|
if !errors.As(err, &searxErr) {
|
|
t.Fatalf("Search() error type = %T, want *SearXNGError", err)
|
|
}
|
|
|
|
if searxErr.StatusCode != http.StatusTemporaryRedirect {
|
|
t.Fatalf("SearXNGError.StatusCode = %d, want %d", searxErr.StatusCode, http.StatusTemporaryRedirect)
|
|
}
|
|
|
|
mu.Lock()
|
|
gotCloseCalls := closeCalls
|
|
mu.Unlock()
|
|
|
|
if gotCloseCalls != 1 {
|
|
t.Fatalf("response body Close() calls = %d, want 1 from searcher", gotCloseCalls)
|
|
}
|
|
}
|
|
|
|
func TestSearch_NonOKStatus(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("400 Bad Request returns error directly", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusBadRequest,
|
|
Header: http.Header{"Content-Type": []string{"text/plain"}},
|
|
Body: io.NopCloser(strings.NewReader("invalid params")),
|
|
}, nil
|
|
}), 2)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error for 400")
|
|
}
|
|
|
|
var searxErr *SearXNGError
|
|
if !errors.As(err, &searxErr) {
|
|
t.Fatalf("error type = %T, want *SearXNGError", err)
|
|
}
|
|
|
|
if searxErr.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("StatusCode = %d, want %d", searxErr.StatusCode, http.StatusBadRequest)
|
|
}
|
|
})
|
|
|
|
t.Run("HTML response returns HTMLResponseError", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("<html><body>JSON not enabled</body></html>")),
|
|
}, nil
|
|
}), 0)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want HTMLResponseError")
|
|
}
|
|
|
|
if _, ok := errors.AsType[*HTMLResponseError](err); !ok {
|
|
t.Fatalf("error type = %T, want *HTMLResponseError", err)
|
|
}
|
|
})
|
|
|
|
t.Run("HTML response with retries enabled does not retry", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("<html><body>JSON not enabled</body></html>")),
|
|
}, nil
|
|
}), 2)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want HTMLResponseError")
|
|
}
|
|
|
|
if _, ok := errors.AsType[*HTMLResponseError](err); !ok {
|
|
t.Fatalf("error type = %T, want *HTMLResponseError", err)
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("callCount = %d, want 1 (no retry for HTML response)", callCount)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_DebugMode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("debug mode does not affect search result", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 0)
|
|
s.debug = true
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if !result.Debug {
|
|
t.Fatal("Debug = false, want true")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_GETFallbackFlow(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("POST 405 triggers GET fallback during Search", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
if callCount == 1 {
|
|
return &http.Response{
|
|
StatusCode: http.StatusMethodNotAllowed,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("")),
|
|
}, nil
|
|
}
|
|
|
|
// GET fallback succeeds
|
|
return makeJSONResponse(makeSearchResponseJSON(1)), nil
|
|
}), 0)
|
|
s.allowGETFallback = true
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err != nil {
|
|
t.Fatalf("Search() error = %v, want nil", err)
|
|
}
|
|
|
|
if result == nil {
|
|
t.Fatal("Search() result = nil, want non-nil")
|
|
|
|
return
|
|
}
|
|
|
|
if callCount != 2 {
|
|
t.Fatalf("callCount = %d, want 2 (POST 405 + GET fallback)", callCount)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_GETfallbackErrorPreservesStatusCode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("GET fallback error preserves original 405 status code in final SearXNGError", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
if req.Method == http.MethodPost {
|
|
return &http.Response{
|
|
StatusCode: http.StatusMethodNotAllowed,
|
|
Header: http.Header{"Content-Type": []string{"text/html"}},
|
|
Body: io.NopCloser(strings.NewReader("")),
|
|
}, nil
|
|
}
|
|
|
|
// GET fallback fails with a transport error
|
|
return nil, errRetryTestConnectionReset
|
|
}), 0)
|
|
s.allowGETFallback = true
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error")
|
|
}
|
|
|
|
// The error must contain errSearchRequestFailed (from Search's final wrapping)
|
|
if !strings.Contains(err.Error(), "failed to execute search request") {
|
|
t.Fatalf("error = %q, want 'failed to execute search request'", err.Error())
|
|
}
|
|
|
|
// The SearXNGError inside the error chain must preserve the original 405 status code
|
|
// (not 0 from an unnecessary outer NewSearXNGError wrapper).
|
|
var searxErr *SearXNGError
|
|
if !errors.As(err, &searxErr) {
|
|
t.Fatalf("error type = %T, want *SearXNGError accessible via errors.As", err)
|
|
}
|
|
|
|
if searxErr.StatusCode != http.StatusMethodNotAllowed {
|
|
t.Fatalf("StatusCode = %d, want %d (original 405 preserved, not 0)",
|
|
searxErr.StatusCode, http.StatusMethodNotAllowed)
|
|
}
|
|
|
|
// The error must contain the original method-rejected hint
|
|
if !strings.Contains(err.Error(), "search method rejected") {
|
|
t.Fatalf("error = %q, want 'search method rejected' hint", err.Error())
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_RetryWithEmptyResponseFallback(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("retry on error then empty response retry also fires", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
if callCount == 1 {
|
|
return nil, errRetryTestConnectionReset
|
|
}
|
|
|
|
// Second and subsequent attempts return empty
|
|
return makeJSONResponse(`{"query":"test","results":[],"suggestions":[],"answers":[],"infoboxes":[]}`), nil
|
|
}), 2)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want empty-results error")
|
|
}
|
|
|
|
if !errors.Is(err, errSearchEmptyResults) {
|
|
t.Fatalf("Search() error = %v, want errSearchEmptyResults in chain", err)
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
// attempt=0: error -> retry; attempt=1: empty -> retry; attempt=2: empty -> error
|
|
if callCount != 3 {
|
|
t.Fatalf("callCount = %d, want 3 (attempt 0=err, attempt 1=empty+retry, attempt 2=empty)", callCount)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_BodyTooLarge(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("very large response body triggers error", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
largeBody := strings.Repeat("x", int(MaxResponseBodySize)+100)
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(largeBody)),
|
|
}, nil
|
|
}), 0)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error for oversized body")
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), "response body exceeded maximum size limit") {
|
|
t.Fatalf("error = %q, want 'response body exceeded maximum size limit'", err.Error())
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_SearXNGErrorIsRetryable(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("SearXNGError from non-retryable status is not retried", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusBadRequest,
|
|
Header: http.Header{"Content-Type": []string{"text/plain"}},
|
|
Body: io.NopCloser(strings.NewReader("bad request")),
|
|
}, nil
|
|
}), 2)
|
|
|
|
_, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want error")
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("callCount = %d, want 1 (no retry for non-retryable SearXNGError)", callCount)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearch_EmptyResponseRetryDoesNotSpin(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("empty response with 0 retries returns error immediately", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
callCount := 0
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
callCount++
|
|
|
|
return makeJSONResponse(`{"query":"test","results":[],"suggestions":[],"answers":[],"infoboxes":[]}`), nil
|
|
}), 0)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want empty-results error")
|
|
}
|
|
|
|
if !errors.Is(err, errSearchEmptyResults) {
|
|
t.Fatalf("Search() error = %v, want errSearchEmptyResults in chain", err)
|
|
}
|
|
|
|
if callCount != 1 {
|
|
t.Fatalf("callCount = %d, want 1", callCount)
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Regression test: the empty-response retry path must not close a response
|
|
// body that finishResponse already closed. Each HTTP response body should be
|
|
// closed exactly once.
|
|
func TestSearch_EmptyResponseRetryClosesBodyOnce(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("each empty body is closed exactly once across retries", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
totalCloseCalls int
|
|
)
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
body := &closeCounter{
|
|
ReadCloser: io.NopCloser(strings.NewReader(minimalJSONBody)),
|
|
mu: &mu,
|
|
total: &totalCloseCalls,
|
|
}
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
Body: body,
|
|
}, nil
|
|
}), 2)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want empty-results error")
|
|
}
|
|
|
|
if !errors.Is(err, errSearchEmptyResults) {
|
|
t.Fatalf("Search() error = %v, want errSearchEmptyResults in chain", err)
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
// 3 HTTP requests (initial + 2 retries of empty responses).
|
|
// Without the fix, attempts 0 and 1 each get double-closed
|
|
// (finishResponse + retry loop), producing 5 total Close calls.
|
|
// With the fix each body is closed exactly once → 3 calls.
|
|
if totalCloseCalls != 3 {
|
|
t.Fatalf("total body Close() calls = %d, want 3 (one per HTTP request)", totalCloseCalls)
|
|
}
|
|
})
|
|
|
|
t.Run("zero retries closes body once", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
totalCloseCalls int
|
|
)
|
|
|
|
s := newTestSearcher(t, testhelper.RoundTripperFunc(func(_ *http.Request) (*http.Response, error) {
|
|
body := &closeCounter{
|
|
ReadCloser: io.NopCloser(strings.NewReader(minimalJSONBody)),
|
|
mu: &mu,
|
|
total: &totalCloseCalls,
|
|
}
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{"Content-Type": []string{"application/json"}},
|
|
Body: body,
|
|
}, nil
|
|
}), 0)
|
|
|
|
result, err := s.Search(t.Context(), &SearchArgs{Query: "test"})
|
|
if err == nil {
|
|
t.Fatal("Search() error = nil, want empty-results error")
|
|
}
|
|
|
|
if !errors.Is(err, errSearchEmptyResults) {
|
|
t.Fatalf("Search() error = %v, want errSearchEmptyResults in chain", err)
|
|
}
|
|
|
|
if result != nil {
|
|
t.Fatalf("Search() result = %#v, want nil", result)
|
|
}
|
|
|
|
if totalCloseCalls != 1 {
|
|
t.Fatalf("total body Close() calls = %d, want 1", totalCloseCalls)
|
|
}
|
|
})
|
|
}
|