mirror of
https://github.com/black-ant/Ant-Browser.git
synced 2026-07-14 18:48:55 +08:00
channel: master version: 1.1.0 source-ref: master published-at-utc: 2026-03-29T11:32:04Z
30 lines
806 B
Go
30 lines
806 B
Go
package launchcode
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildHandlerRejectsNonLocalRequestBeforeAPIAuth(t *testing.T) {
|
|
srv := NewLaunchServer(NewLaunchCodeService(NewMemoryLaunchCodeDAO()), nil, nil, 0)
|
|
srv.SetAPIAuthConfig(APIAuthConfig{
|
|
Enabled: true,
|
|
APIKey: "secret-key",
|
|
Header: "X-Test-Api-Key",
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/health", nil)
|
|
req.RemoteAddr = "10.0.0.8:3456"
|
|
w := httptest.NewRecorder()
|
|
srv.buildHandler(true).ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("非 localhost 请求应优先返回 403: got=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if !strings.Contains(w.Body.String(), "forbidden: only localhost is allowed") {
|
|
t.Fatalf("错误信息不正确: %s", w.Body.String())
|
|
}
|
|
}
|