searxng-mcp-go/error_path_test.go
xlion-bot-renovate[bot] 6e4c553a9e
chore(deps): update dependency go to v1.27.1 (#588)
* 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]>
2026-09-06 23:14:09 +08:00

1031 lines
30 KiB
Go

package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"searxng-mcp-go/internal/searxng"
"searxng-mcp-go/internal/testhelper"
)
// ============================================================================
// Error Path Tests - Network Failures
// ============================================================================
// TestSearch_DNSFailure tests that DNS failures are properly wrapped with context.
func TestSearch_DNSFailure(t *testing.T) {
t.Parallel()
client := &http.Client{
Transport: testhelper.RoundTripperFunc(func(*http.Request) (*http.Response, error) {
return nil, &net.DNSError{
Err: "no such host",
Name: "nonexistent.invalid-domain.test",
IsNotFound: true,
}
}),
}
cfg := &searxng.Config{
SearXNGURL: "http://example.com",
Timeout: 5 * time.Second,
HTTPClient: client,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected DNS failure error, got nil")
}
// Verify error is wrapped with context (not just a sentinel error)
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Errorf("expected *searxng.SearXNGError, got type %T: %v", err, err)
}
// Verify there's underlying error information
if searxngErr != nil && searxngErr.UnderlyingErr == nil {
t.Errorf("SearXNGError.UnderlyingErr is nil for DNS failure")
}
// Check error message contains useful context
if !strings.Contains(err.Error(), "failed to execute search request") {
t.Errorf("error should contain 'failed to execute search request', got: %v", err)
}
}
// TestSearch_ConnectionRefused tests connection refused errors.
func TestSearch_ConnectionRefused(t *testing.T) {
t.Parallel()
// Create a server and immediately close it to simulate connection refused
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
}))
server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 5 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected connection refused error, got nil")
}
// Verify error is wrapped with context
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Errorf("expected *searxng.SearXNGError, got type %T: %v", err, err)
}
// Should have underlying error
if searxngErr != nil && searxngErr.UnderlyingErr == nil {
t.Errorf("SearXNGError.UnderlyingErr is nil")
}
}
// ============================================================================
// Error Path Tests - HTTP Status Codes
// ============================================================================
// TestSearch_EmptyResponse tests handling of empty response body.
func TestSearch_EmptyResponse(t *testing.T) {
t.Parallel()
server := newJSONRawTestServer(t, nil)
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
// Empty JSON body should cause a JSON parse error
if err == nil {
t.Fatal("expected error for empty response body, got nil")
}
if !strings.Contains(err.Error(), "failed to parse JSON response") {
t.Errorf("expected JSON parse error, got: %v", err)
}
}
// TestSearch_EmptyBodyWith200 tests empty JSON object response.
func TestSearch_EmptyJSONObject(t *testing.T) {
t.Parallel()
server := newJSONRawTestServer(t, []byte("{}"))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
// Empty JSON object is valid but has no results — now returns an error
if err == nil {
t.Fatal("expected error for empty JSON response, got nil")
}
if !strings.Contains(err.Error(), "empty results") {
t.Errorf("error = %q, want empty-results error", err.Error())
}
}
// TestSearch_UnexpectedContentType tests response with unexpected content type.
func TestSearch_UnexpectedContentType(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("plain text response")) //nolint:errcheck // test fixture write best-effort
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected error for unexpected content type, got nil")
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Errorf("expected *searxng.SearXNGError, got type %T: %v", err, err)
return
}
if searxngErr.StatusCode != http.StatusOK {
t.Fatalf("StatusCode = %d, want %d", searxngErr.StatusCode, http.StatusOK)
}
if searxngErr.RespContentType != "text/plain" {
t.Fatalf("RespContentType = %q, want %q", searxngErr.RespContentType, "text/plain")
}
expectedErr := "unexpected content type: expected application/json"
if searxngErr.UnderlyingErr == nil || searxngErr.UnderlyingErr.Error() != expectedErr {
t.Fatalf("UnderlyingErr = %v, want unexpected content type error", searxngErr.UnderlyingErr)
}
want := "searxng error (status 200)" +
" - content-type text/plain:" +
" unexpected content type: expected application/json"
if got, want2 := err.Error(), want; got != want2 {
t.Fatalf("error = %q, want %q", got, want)
}
}
func TestSearch_EmptyHTMLBody(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 30 * time.Second}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected error, got nil")
}
var htmlErr *searxng.HTMLResponseError
if !errors.As(err, &htmlErr) {
t.Fatalf("expected *HTMLResponseError, got %T", err)
}
if htmlErr.Body != "" {
t.Fatalf("Body = %q, want empty string", htmlErr.Body)
}
if err.Error() != "searxng returned html instead of json - json output may not be enabled on the server" {
t.Fatalf("unexpected error text: %v", err)
}
}
func TestSearch_HTMLResponseError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
contentType string
body string
desc string
}{
{
name: "HTML content-type header",
contentType: "text/html",
body: "<!DOCTYPE html><html><head><title>Search</title></head><body>JSON output not enabled</body></html>",
desc: "HTMLResponseError triggered by text/html content-type",
},
{
name: "body starts with DOCTYPE",
contentType: "text/html; charset=utf-8",
body: "<!DOCTYPE html><html><body>Please enable JSON output</body></html>",
desc: "HTMLResponseError triggered by <!DOCTYPE prefix",
},
{
name: "body starts with html tag",
contentType: "application/octet-stream",
body: "<html><body>JSON output not enabled on this instance</body></html>",
desc: "HTMLResponseError triggered by <html> prefix even without HTML content-type",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", tt.contentType)
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(tt.body)) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected HTMLResponseError, got nil")
}
if _, ok := errors.AsType[*searxng.HTMLResponseError](err); !ok {
t.Errorf("expected HTMLResponseError, got: %v", err)
}
if !strings.Contains(err.Error(), "searxng returned html instead of json") {
t.Errorf("expected HTML response error message, got: %v", err)
}
})
}
}
//nolint:gocognit,gocyclo // table-driven test covers many edge cases for body size limits
func TestSearch_UnsupportedBodySizes(t *testing.T) {
t.Parallel()
t.Run("oversized error body", func(t *testing.T) {
t.Parallel()
body := strings.Repeat("e", searxng.MaxErrorBodySize+1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
t.Errorf("response writer does not support hijacking")
return
}
conn, _, err := hj.Hijack()
if err != nil {
t.Errorf("Hijack() failed: %v", err)
return
}
defer func() { _ = conn.Close() }() //nolint:errcheck // cleanup in defer; error is non-actionable
header := fmt.Sprintf(
"HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n",
len(body),
)
_, _ = conn.Write([]byte(header)) //nolint:errcheck // test fixture write; failure does not affect test outcome
_, _ = conn.Write([]byte(body)) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected error, got nil")
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Fatalf("expected *SearXNGError, got %T", err)
}
if searxngErr.StatusCode != http.StatusInternalServerError {
t.Fatalf("StatusCode = %d, want %d", searxngErr.StatusCode, http.StatusInternalServerError)
}
if len(searxngErr.ResponseBody) != searxng.MaxErrorDisplayBytes {
t.Fatalf("ResponseBody length = %d, want %d", len(searxngErr.ResponseBody), searxng.MaxErrorDisplayBytes)
}
if !strings.HasPrefix(searxngErr.ResponseBody, "eee") {
t.Fatalf("ResponseBody does not contain expected body preview: %q", searxngErr.ResponseBody)
}
})
t.Run("oversized success body", func(t *testing.T) {
t.Parallel()
body := `{"query":"test","number_of_results":1,"results":[{"title":"Result",` +
`"url":"https://example.com","content":"` +
strings.Repeat("s", searxng.MaxResponseBodySize+1) +
`","engine":"google"}],"suggestions":[]}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
t.Errorf("response writer does not support hijacking")
return
}
conn, _, err := hj.Hijack()
if err != nil {
t.Errorf("Hijack() failed: %v", err)
return
}
defer func() { _ = conn.Close() }() //nolint:errcheck // cleanup in defer; error is non-actionable
header := fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n", len(body))
_, _ = conn.Write([]byte(header)) //nolint:errcheck // test fixture write; failure does not affect test outcome
_, _ = conn.Write([]byte(body)) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected error, got nil")
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Fatalf("expected *SearXNGError, got %T", err)
}
if !strings.Contains(err.Error(), "response body exceeded maximum size limit") {
t.Fatalf("unexpected error: %v", err)
}
if len(searxngErr.ResponseBody) != searxng.MaxErrorDisplayBytes {
t.Fatalf("ResponseBody length = %d, want %d", len(searxngErr.ResponseBody), searxng.MaxErrorDisplayBytes)
}
})
}
// ============================================================================
// Error Path Tests - Malformed JSON
// ============================================================================
func TestSearch_MalformedJSON_Truncated(t *testing.T) {
t.Parallel()
truncatedJSON := []byte(`{"results": [{"title": "test",`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(truncatedJSON) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected JSON parse error, got nil")
}
if !strings.Contains(err.Error(), "failed to parse JSON response") {
t.Errorf("expected JSON parse error, got: %v", err)
}
}
func TestSearch_WrongJSONType(t *testing.T) {
t.Parallel()
// Valid JSON with wrong types: results should be an array, but we provide a string
// because "results" is expected to be an array, not a string
wrongTypeJSON := []byte(`{"results": "not an array", "number_of_results": "not a number"}`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(wrongTypeJSON) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
// Go's json.Unmarshal will fail when trying to unmarshal string into []SearchResult
if err == nil {
t.Fatal("expected JSON parse error for wrong-type JSON, got nil")
}
if !strings.Contains(err.Error(), "failed to parse JSON response") {
t.Errorf("expected JSON parse error, got: %v", err)
}
}
func TestSearch_TrailingGarbage(t *testing.T) {
t.Parallel()
// Valid JSON followed by garbage
garbageJSON := []byte(`{"results":[],"number_of_results":0,"query":"test"}TRAILING GARBAGE`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(garbageJSON) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 30 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected JSON parse error for trailing garbage, got nil")
}
if !strings.Contains(err.Error(), "failed to parse JSON response") {
t.Errorf("expected JSON parse error, got: %v", err)
}
}
// ============================================================================
// Error Path Tests - Error Wrapping Context
// ============================================================================
func TestSearch_NetworkError_ConnectionClose(t *testing.T) {
t.Parallel()
// Create a server and immediately close it to simulate network error
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
// Close immediately without responding
}))
server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 5 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected network error, got nil")
}
// Verify error is wrapped with context
errStr := err.Error()
if !strings.Contains(errStr, "failed to execute search request") {
t.Errorf("error %q does not contain expected context 'failed to execute search request'", errStr)
}
// Verify it's a proper wrapped error type
if _, ok := errors.AsType[*searxng.SearXNGError](err); !ok {
t.Errorf("expected *searxng.SearXNGError, got type %T", err)
}
}
// ============================================================================
// Error Path Tests - Timeout Behavior
// ============================================================================
func TestSearch_TimeoutExceeded(t *testing.T) {
t.Parallel()
// Use a custom RoundTripper that blocks until context cancellation,
// avoiding httptest.Server.Close() blocking on active connections.
client := &http.Client{
Transport: testhelper.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
<-r.Context().Done()
return nil, r.Context().Err()
}),
}
cfg := &searxng.Config{
SearXNGURL: "https://example.com",
Timeout: 30 * time.Second,
HTTPClient: client,
}
args := &searxng.SearchArgs{Query: "test"}
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
defer cancel()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected timeout error, got nil")
}
errStr := err.Error()
if !strings.Contains(errStr, "context deadline exceeded") &&
!strings.Contains(errStr, "request canceled") &&
!strings.Contains(errStr, "timeout") &&
!strings.Contains(errStr, "failed to execute") {
t.Errorf("expected timeout-related error, got: %v", err)
}
}
func TestSearch_ContextDeadlineExceeded(t *testing.T) {
t.Parallel()
// Use a custom RoundTripper that blocks until context cancellation,
// avoiding httptest.Server.Close() blocking on active connections.
client := &http.Client{
Transport: testhelper.RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
<-r.Context().Done()
return nil, r.Context().Err()
}),
}
cfg := &searxng.Config{
SearXNGURL: "https://example.com",
Timeout: 30 * time.Second,
HTTPClient: client,
}
args := &searxng.SearchArgs{Query: "test"}
ctx, cancel := context.WithTimeout(t.Context(), 100*time.Millisecond)
defer cancel()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatal("expected context deadline exceeded error, got nil")
}
// Verify error is wrapped
if _, ok := errors.AsType[*searxng.SearXNGError](err); !ok {
t.Errorf("expected *searxng.SearXNGError, got type %T: %v", err, err)
}
}
// ============================================================================
// Error Path Tests - HTTP Status Error Messages
// ============================================================================
func TestSearch_HTTPStatusErrors(t *testing.T) {
t.Parallel()
tests := []struct {
statusCode int
wantContains string
}{
{400, "searxng error (status 400)"},
{401, "searxng error (status 401)"},
{403, "searxng error (status 403)"},
{404, "searxng error (status 404)"},
{405, "searxng error (status 405)"},
{418, "searxng error (status 418)"},
{429, "searxng error (status 429)"},
{500, "searxng error (status 500)"},
{501, "searxng error (status 501)"},
{502, "searxng error (status 502)"},
{503, "searxng error (status 503)"},
{504, "searxng error (status 504)"},
}
for _, tc := range tests {
t.Run(http.StatusText(tc.statusCode), func(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tc.statusCode)
_, _ = w.Write([]byte(http.StatusText(tc.statusCode))) //nolint:errcheck // test fixture write best-effort
}))
defer server.Close()
cfg := &searxng.Config{
SearXNGURL: server.URL,
Timeout: 5 * time.Second,
}
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, cfg, args)
if err == nil {
t.Fatalf("expected error for status %d, got nil", tc.statusCode)
}
if !strings.Contains(err.Error(), tc.wantContains) {
t.Errorf("error %q does not contain %q", err.Error(), tc.wantContains)
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Fatalf("expected *searxng.SearXNGError, got type %T", err)
}
if searxngErr.StatusCode != tc.statusCode {
t.Errorf("SearXNGError.StatusCode = %d, want %d", searxngErr.StatusCode, tc.statusCode)
}
})
}
}
func TestSearch_RedirectStatus(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", "/search/redirected")
w.WriteHeader(http.StatusTemporaryRedirect)
_, _ = w.Write([]byte("redirect")) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
client := &http.Client{
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second, HTTPClient: client}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected redirect error, got nil")
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Fatalf("expected *searxng.SearXNGError, got %T", err)
}
if searxngErr.StatusCode != http.StatusTemporaryRedirect {
t.Fatalf("StatusCode = %d, want %d", searxngErr.StatusCode, http.StatusTemporaryRedirect)
}
}
func TestSearch_CustomHTTPClientWithoutRedirectPolicyBlocksCrossHost(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", "http://example.com/search")
w.WriteHeader(http.StatusFound)
}))
defer server.Close()
client := &http.Client{Timeout: 5 * time.Second}
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second, HTTPClient: client}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected cross-host redirect error, got nil")
}
if client.CheckRedirect != nil {
t.Fatal("NewSearXNGSearcher mutated caller's custom HTTP client")
}
if !strings.Contains(err.Error(), "redirect to different host blocked") {
t.Fatalf("error %q does not contain redirect block message", err.Error())
}
}
func TestSearch_CustomHTTPClientSameHostRedirectAllowed(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/search/redirected" {
w.Header().Set("Content-Type", "application/json")
//nolint:errcheck // test fixture write best-effort
_, _ = w.Write([]byte(`{"query":"test","number_of_results":1,` +
`"results":[{"title":"x","url":"https://x"}],"suggestions":[]}`))
return
}
w.Header().Set("Location", "/search/redirected")
w.WriteHeader(http.StatusTemporaryRedirect)
}))
defer server.Close()
client := &http.Client{Timeout: 5 * time.Second}
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second, HTTPClient: client}
result, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err != nil {
t.Fatalf("unexpected error for same-host redirect: %v", err)
}
if result.Query != "test" {
t.Fatalf("Query = %q, want test", result.Query)
}
if client.CheckRedirect != nil {
t.Fatal("NewSearXNGSearcher mutated caller's custom HTTP client")
}
}
func TestSearch_CustomHTTPClientCrossHostRedirectBlockedBeforeCustomPolicy(t *testing.T) {
t.Parallel()
customPolicyCalled := false
client := &http.Client{
Timeout: 5 * time.Second,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
customPolicyCalled = true
return nil
},
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", "http://example.com/search")
w.WriteHeader(http.StatusFound)
}))
defer server.Close()
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second, HTTPClient: client}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected cross-host redirect error, got nil")
}
if customPolicyCalled {
t.Fatal("custom redirect policy ran before cross-host redirect was blocked")
}
if !strings.Contains(err.Error(), "redirect to different host blocked") {
t.Fatalf("error %q does not contain redirect block message", err.Error())
}
}
func TestSearch_ConnectionResetMidResponse(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hj, ok := w.(http.Hijacker)
if !ok {
t.Fatal("response writer does not support hijacking")
}
conn, _, err := hj.Hijack()
if err != nil {
t.Fatalf("Hijack() failed: %v", err)
}
defer func() { _ = conn.Close() }() //nolint:errcheck // cleanup in defer; error is non-actionable
body := "HTTP/1.1 200 OK\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: 128\r\n" +
"\r\n" +
"{\"query\":\"test\",\"results\":[\""
_, _ = conn.Write([]byte(body)) //nolint:errcheck // test fixture write; failure does not affect test outcome
}))
defer server.Close()
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second}
_, err := testPerformSearch(t.Context(), t, cfg, &searxng.SearchArgs{Query: "test"})
if err == nil {
t.Fatal("expected connection reset error, got nil")
}
var searxngErr *searxng.SearXNGError
if !errors.As(err, &searxngErr) {
t.Fatalf("expected *searxng.SearXNGError, got %T", err)
}
if searxngErr.StatusCode != http.StatusOK {
t.Fatalf("StatusCode = %d, want %d", searxngErr.StatusCode, http.StatusOK)
}
if searxngErr.UnderlyingErr == nil {
t.Fatal("expected underlying read error, got nil")
}
}
// ============================================================================
// Error Path Tests - Verify All Errors Are Wrapped
// ============================================================================
// TestSearch_AllErrorTypesAreWrapped verifies that all error types
// returned by Search are properly wrapped with context.
func TestSearch_AllErrorTypesAreWrapped(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg *searxng.Config
}{
{
name: "invalid URL scheme",
cfg: &searxng.Config{SearXNGURL: "ftp://example.com", Timeout: 30 * time.Second},
},
{
name: "missing host",
cfg: &searxng.Config{SearXNGURL: "http://", Timeout: 30 * time.Second},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
args := &searxng.SearchArgs{Query: "test"}
ctx := t.Context()
_, err := testPerformSearch(ctx, t, tt.cfg, args)
if err == nil {
t.Fatal("expected error, got nil")
}
// Every error should be a wrapped error with context
// It should be either a SearXNGError or have context wrapped via %w
errStr := err.Error()
if errStr == "" {
t.Error("error message is empty")
}
// For URL errors, should still be wrapped in SearXNGError
if _, ok := errors.AsType[*searxng.SearXNGError](err); ok {
// Good - it's a proper wrapped error type
return
}
// If not a SearXNGError, it should at least have context via fmt.Errorf
if !strings.Contains(errStr, ":") && len(errStr) < 10 {
t.Errorf("error %q appears to be a bare sentinel error, expected wrapped error", errStr)
}
})
}
}
// ============================================================================
// Redirect Integration Tests — real HTTP server through the full client
// ============================================================================
//nolint:gocognit // table-driven integration test: redirect method preservation (301-308)
func TestSearch_RedirectMethodPreservation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
statusCode int
wantRedirect bool
wantErr bool
wantErrSubstr string
}{
{
name: "301 moved permanently blocks redirect",
statusCode: http.StatusMovedPermanently,
wantRedirect: false,
wantErr: true,
wantErrSubstr: "redirect would change POST to GET",
},
{
name: "302 found blocks redirect",
statusCode: http.StatusFound,
wantRedirect: false,
wantErr: true,
wantErrSubstr: "redirect would change POST to GET",
},
{
name: "303 see other blocks redirect",
statusCode: http.StatusSeeOther,
wantRedirect: false,
wantErr: true,
wantErrSubstr: "redirect would change POST to GET",
},
{
name: "307 temporary redirect preserves POST",
statusCode: http.StatusTemporaryRedirect,
wantRedirect: true,
wantErr: false,
},
{
name: "308 permanent redirect preserves POST",
statusCode: http.StatusPermanentRedirect,
wantRedirect: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var (
redirectReached bool
redirectedMethod string
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/search/redirected" {
redirectReached = true
redirectedMethod = r.Method
w.Header().Set("Content-Type", "application/json")
//nolint:errcheck // test fixture write best-effort
_, _ = w.Write([]byte(`{"query":"test","number_of_results":1,` +
`"results":[{"title":"x","url":"https://x"}],"suggestions":[]}`))
return
}
w.Header().Set("Location", "/search/redirected")
w.WriteHeader(tt.statusCode)
}))
defer server.Close()
cfg := &searxng.Config{SearXNGURL: server.URL, Timeout: 5 * time.Second}
args := &searxng.SearchArgs{Query: "test"}
_, err := testPerformSearch(t.Context(), t, cfg, args)
if tt.wantErr {
if err == nil {
t.Fatal("Search() error = nil, want error")
}
if !strings.Contains(err.Error(), tt.wantErrSubstr) {
t.Fatalf("error = %q, want containing %q", err.Error(), tt.wantErrSubstr)
}
} else if err != nil {
t.Fatalf("Search() error = %v, want nil", err)
}
if redirectReached != tt.wantRedirect {
t.Fatalf("redirect target reached = %v, want %v", redirectReached, tt.wantRedirect)
}
if tt.wantRedirect && redirectedMethod != http.MethodPost {
t.Fatalf("redirected request method = %s, want POST", redirectedMethod)
}
})
}
}