refactor: 适配核心模块类型重命名

适配 variable.NewContext/ReleaseContext
适配 resolver.DNSCacheEntry
适配 logging/formatJSON 常量

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-10 09:41:07 +08:00
parent 2af3176507
commit 4e826925ac
9 changed files with 47 additions and 42 deletions

View File

@ -478,6 +478,7 @@ func (a *App) gracefulUpgrade() {
// sigName 返回信号名称(用于日志输出)。
func sigName(sig syscall.Signal) string {
//nolint:exhaustive
switch sig {
case syscall.SIGTERM:
return "SIGTERM"

View File

@ -483,7 +483,7 @@ func TestHandleSignal_Unknown(t *testing.T) {
}
// TestShutdownHTTP3_NilServer 测试 HTTP/3 服务器为 nil 时关闭
func TestShutdownHTTP3_NilServer(t *testing.T) {
func TestShutdownHTTP3_NilServer(_ *testing.T) {
app := NewApp("")
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -492,7 +492,7 @@ func TestShutdownHTTP3_NilServer(t *testing.T) {
}
// TestReopenLogs 测试重开日志
func TestReopenLogs(t *testing.T) {
func TestReopenLogs(_ *testing.T) {
app := NewApp("")
app.cfg = &config.Config{
Logging: config.LoggingConfig{
@ -508,7 +508,7 @@ func TestReopenLogs(t *testing.T) {
}
// TestReloadConfig_FileNotFound 测试重载不存在的配置
func TestReloadConfig_FileNotFound(t *testing.T) {
func TestReloadConfig_FileNotFound(_ *testing.T) {
app := NewApp("/nonexistent/config.yaml")
app.logger = logging.NewAppLogger(&config.LoggingConfig{})
@ -549,7 +549,7 @@ logging:
}
// TestSetupSignalHandlers 测试信号处理设置
func TestSetupSignalHandlers(t *testing.T) {
func TestSetupSignalHandlers(_ *testing.T) {
app := NewApp("")
app.cfg = &config.Config{
Server: config.ServerConfig{
@ -585,7 +585,7 @@ func TestHandleSignal_SIGUSR2(t *testing.T) {
}
// TestGracefulUpgrade_NoListener 测试无监听器时的热升级
func TestGracefulUpgrade_NoListener(t *testing.T) {
func TestGracefulUpgrade_NoListener(_ *testing.T) {
app := NewApp("")
app.cfg = &config.Config{
Server: config.ServerConfig{
@ -652,7 +652,7 @@ func TestAppFields(t *testing.T) {
}
// TestShutdownHTTP3_WithServer 测试有 HTTP3 服务器时的关闭
func TestShutdownHTTP3_WithServer(t *testing.T) {
func TestShutdownHTTP3_WithServer(_ *testing.T) {
app := NewApp("")
app.cfg = &config.Config{
Server: config.ServerConfig{
@ -673,7 +673,7 @@ func TestShutdownHTTP3_WithServer(t *testing.T) {
}
// TestReopenLogs_WithNilConfig 测试配置为 nil 时重开日志
func TestReopenLogs_WithNilConfig(t *testing.T) {
func TestReopenLogs_WithNilConfig(_ *testing.T) {
app := NewApp("")
app.logger = logging.NewAppLogger(&config.LoggingConfig{})

View File

@ -165,7 +165,7 @@ func (bc *BenchmarkContext) MockResponse() []byte {
//
// 返回值:
// - []byte: 模拟的响应数据
func (bc *BenchmarkContext) MockResponseWithContentType(statusCode int, contentType string) []byte {
func (bc *BenchmarkContext) MockResponseWithContentType(_, _ string) []byte {
// 返回响应体,调用者负责设置状态码和 Content-Type
return GenerateTestData(bc.ResponseSize)
}

View File

@ -76,6 +76,10 @@ func (mb *MockBackend) handler(ctx *fasthttp.RequestCtx) {
mb.mu.RUnlock()
switch config.Mode {
case ModeFixed:
ctx.SetStatusCode(config.StatusCode)
_, _ = ctx.Write(config.Body)
case ModeDelay:
time.Sleep(config.Delay)
ctx.SetStatusCode(config.StatusCode)

View File

@ -160,7 +160,7 @@ func platformSendfile(conn net.Conn, file *os.File, offset, length int64) error
//
// 返回值:
// - error: 系统调用错误
func linuxSendfile(conn net.Conn, fileFd uintptr, offset, length int64) error {
func linuxSendfile(conn net.Conn, fileFd uintptr, _, length int64) error {
socketFd, err := getSocketFd(conn)
if err != nil {
return err

View File

@ -187,14 +187,14 @@ func TestGetSocketFd_UnsupportedType(t *testing.T) {
// mockConn 是一个不实现 TCPConn/UnixConn 的连接
type mockConn struct{}
func (m *mockConn) Read(b []byte) (n int, err error) { return 0, nil }
func (m *mockConn) Write(b []byte) (n int, err error) { return 0, nil }
func (m *mockConn) Close() error { return nil }
func (m *mockConn) LocalAddr() net.Addr { return nil }
func (m *mockConn) RemoteAddr() net.Addr { return nil }
func (m *mockConn) SetDeadline(t time.Time) error { return nil }
func (m *mockConn) SetReadDeadline(t time.Time) error { return nil }
func (m *mockConn) SetWriteDeadline(t time.Time) error { return nil }
func (m *mockConn) Read([]byte) (n int, err error) { return 0, nil }
func (m *mockConn) Write([]byte) (n int, err error) { return 0, nil }
func (m *mockConn) Close() error { return nil }
func (m *mockConn) LocalAddr() net.Addr { return nil }
func (m *mockConn) RemoteAddr() net.Addr { return nil }
func (m *mockConn) SetDeadline(time.Time) error { return nil }
func (m *mockConn) SetReadDeadline(time.Time) error { return nil }
func (m *mockConn) SetWriteDeadline(time.Time) error { return nil }
// TestSendFile_SmallFile 测试小文件发送(使用 fallback
func TestSendFile_SmallFile(t *testing.T) {
@ -287,7 +287,7 @@ func TestSendFile_ZeroLength(t *testing.T) {
}
// TestGetNetConn 测试获取底层连接
func TestGetNetConn(t *testing.T) {
func TestGetNetConn(_ *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Init(&fasthttp.Request{}, nil, nil)

View File

@ -111,7 +111,7 @@ func TestStaticHandlerHandle(t *testing.T) {
},
{
name: "文件不存在",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
// 不创建任何文件
},
@ -121,7 +121,7 @@ func TestStaticHandlerHandle(t *testing.T) {
},
{
name: "空路径访问根目录无索引",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
// root 目录没有索引文件
},
@ -188,7 +188,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
}{
{
name: "文件名包含双点 - 安全检查拦截",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
// 不创建任何文件
},
@ -198,7 +198,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "路径末尾双点 - 安全检查拦截",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
},
path: "/foo/..",
@ -207,7 +207,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "隐藏文件 .hidden - 文件不存在",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
},
path: "/.hidden",
@ -216,7 +216,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "文件名包含多点 ...txt - 安全检查拦截",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
},
path: "/file...txt",
@ -225,7 +225,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "fasthttp 规范化后的路径 - 文件不存在",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
// fasthttp 将 /../secret.txt 规范化为 /secret.txt
},
@ -235,7 +235,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "URL 编码路径遍历 - fasthttp 规范化",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
// fasthttp 解码 %2e%2e 为 .. 并规范化路径
},
@ -245,7 +245,7 @@ func TestStaticHandlerHandle_PathTraversalSecurity(t *testing.T) {
},
{
name: "混合 URL 编码 - fasthttp 规范化",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
t.Helper()
},
path: "/%2e%2e%2fsecret.txt",
@ -764,7 +764,7 @@ func TestStaticHandler_handleTryFiles(t *testing.T) {
},
{
name: "所有 try_files 都未找到",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
// 不创建任何文件
},
tryFiles: []string{"$uri", "$uri/", "/index.html"},
@ -879,7 +879,7 @@ func TestStaticHandler_handleInternalRedirect(t *testing.T) {
},
{
name: "内部重定向目标不存在",
setup: func(t *testing.T, root string) {
setup: func(_ *testing.T, _ string) {
// 不创建 fallback 文件
},
tryFiles: []string{"$uri", "/fallback.html"},
@ -1156,7 +1156,7 @@ func TestStaticHandler_Alias(t *testing.T) {
},
{
name: "alias 文件不存在",
setup: func(t *testing.T, aliasDir string) {
setup: func(_ *testing.T, _ string) {
// 不创建任何文件
},
alias: "/alias/images/",

View File

@ -57,8 +57,8 @@ func TestVariableInProxyHeaders(t *testing.T) {
ctx.Request.Header.Set("X-Custom-Header", "original")
// 测试变量展开
vc := variable.NewVariableContext(ctx)
defer variable.ReleaseVariableContext(vc)
vc := variable.NewContext(ctx)
defer variable.ReleaseContext(vc)
// 模拟代理配置中的 header 设置
tests := []struct {
@ -126,7 +126,7 @@ func TestVariableInRewriteRules(t *testing.T) {
ctx.Request.SetRequestURI("/redirect/old-page")
ctx.Request.Header.SetMethod("GET")
handler := mw.Process(func(c *fasthttp.RequestCtx) {
handler := mw.Process(func(_ *fasthttp.RequestCtx) {
t.Error("should not reach next handler for redirect")
})
handler(ctx)
@ -150,8 +150,8 @@ func TestVariableCompatibilityWithNginx(t *testing.T) {
// 设置响应信息
variable.SetResponseInfoInContext(ctx, 201, 2048, 100000000) // 100ms
vc := variable.NewVariableContext(ctx)
defer variable.ReleaseVariableContext(vc)
vc := variable.NewContext(ctx)
defer variable.ReleaseContext(vc)
// 设置 HTTP 头变量logging 中会自动设置这些)
vc.Set("http_referer", "http://referrer.com")
@ -190,8 +190,8 @@ func TestVariablePerformance(t *testing.T) {
ctx.Request.Header.SetMethod("GET")
ctx.Request.Header.SetHost("api.example.com")
vc := variable.NewVariableContext(ctx)
defer variable.ReleaseVariableContext(vc)
vc := variable.NewContext(ctx)
defer variable.ReleaseContext(vc)
// 常见日志格式模板
template := "$remote_addr - $remote_user [$time_local] \"$request_method $request_uri $scheme\" $status $body_bytes_sent \"$http_user_agent\""
@ -271,8 +271,8 @@ func TestVariableEdgeCases(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
tt.setup(ctx)
vc := variable.NewVariableContext(ctx)
defer variable.ReleaseVariableContext(vc)
vc := variable.NewContext(ctx)
defer variable.ReleaseContext(vc)
result := vc.Expand(tt.template)
if result != tt.expected {
@ -292,8 +292,8 @@ func TestVariableAllBuiltins(t *testing.T) {
// 设置响应信息
variable.SetResponseInfoInContext(ctx, 200, 1024, 50000000) // 50ms
vc := variable.NewVariableContext(ctx)
defer variable.ReleaseVariableContext(vc)
vc := variable.NewContext(ctx)
defer variable.ReleaseContext(vc)
// 测试所有内置变量
builtinVars := []string{

View File

@ -120,7 +120,7 @@ func TestExtractClientIPNet(t *testing.T) {
}
}
func TestGetRemoteAddrIP(t *testing.T) {
func TestGetRemoteAddrIP(_ *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Init(&fasthttp.Request{}, nil, nil)