mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
The admin "Test" button for web search emulation always showed "internal error". Manager.TestSearch dropped every provider's error with a bare `continue` and returned "no available provider", and the handler passed that plain error to response.ErrorFrom, which maps it to a 500 with the generic message. A wrong key, an exhausted plan, a proxy failure and an unsaved config all looked the same. Keep each provider's error in TestSearch and join them when every provider fails, and return the cause from the handler as a 400, the same way the SMTP connection test already does. TestSearch is admin only; the production SearchWithBestProvider path is unchanged. Refs #7201
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
//go:build unit
|
|
|
|
package admin
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTestWebSearchEmulationReturnsFailureCause(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
service.SetWebSearchManager(nil)
|
|
t.Cleanup(func() { service.SetWebSearchManager(nil) })
|
|
|
|
rec := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(rec)
|
|
c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/admin/settings/web-search-emulation/test", bytes.NewBufferString(`{"query":"golang"}`))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
(&SettingHandler{}).TestWebSearchEmulation(c)
|
|
|
|
require.Equal(t, http.StatusBadRequest, rec.Code)
|
|
var body struct {
|
|
Message string `json:"message"`
|
|
}
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body))
|
|
require.Contains(t, body.Message, "Web search test failed")
|
|
require.Contains(t, body.Message, "save config first", "the admin must see why the test failed, not a generic internal error")
|
|
}
|