refactor(compression): 移除 TryServeFile 和 DefaultExtensions 公开函数

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-10 13:22:35 +08:00
parent 5e86f47650
commit 7f4c8f547c
2 changed files with 7 additions and 27 deletions

View File

@ -119,23 +119,6 @@ func (g *GzipStatic) ServeFile(ctx *fasthttp.RequestCtx, filePath string) bool {
return false
}
// TryServeFile 尝试发送预压缩文件的静态方法。
//
// 用于在静态文件处理器中调用。
//
// 参数:
// - ctx: FastHTTP 请求上下文
// - root: 静态文件根目录
// - filePath: 请求的文件路径
// - extensions: 支持的扩展名
//
// 返回值:
// - bool: true 表示已发送预压缩文件
func TryServeFile(ctx *fasthttp.RequestCtx, root, filePath string, extensions []string) bool {
g := NewGzipStatic(true, root, extensions)
return g.ServeFile(ctx, filePath)
}
// matchExtension 检查文件扩展名是否匹配。
func (g *GzipStatic) matchExtension(filePath string) bool {
ext := strings.ToLower(filepath.Ext(filePath))
@ -157,11 +140,6 @@ func (g *GzipStatic) Extensions() []string {
return g.extensions
}
// DefaultExtensions 返回默认支持的扩展名。
func DefaultExtensions() []string {
return []string{".html", ".css", ".js", ".json", ".xml", ".svg", ".txt"}
}
// supportsEncoding 检查客户端是否支持指定编码。
//
// 简单检查,忽略 q-value固定优先级由遍历顺序决定。

View File

@ -363,10 +363,11 @@ func TestGzipStatic_Enabled(t *testing.T) {
}
}
// TestDefaultExtensions 测试 DefaultExtensions 函数
// TestDefaultExtensions 测试默认扩展名(通过 NewGzipStatic 间接测试)
func TestDefaultExtensions(t *testing.T) {
expected := []string{".html", ".css", ".js", ".json", ".xml", ".svg", ".txt"}
got := DefaultExtensions()
g := NewGzipStatic(true, "/tmp", nil)
got := g.Extensions()
if len(got) != len(expected) {
t.Errorf("默认扩展名数量 = %d, want %d", len(got), len(expected))
@ -448,7 +449,7 @@ func TestSupportsEncoding(t *testing.T) {
}
}
// TestTryServeFile 测试 TryServeFile 静态方法
// TestTryServeFile 测试预压缩文件服务(通过 ServeFile 间接测试)
func TestTryServeFile(t *testing.T) {
tmpDir := t.TempDir()
@ -461,10 +462,11 @@ func TestTryServeFile(t *testing.T) {
ctx := &fasthttp.RequestCtx{}
ctx.Request.Header.Set("Accept-Encoding", "br")
served := TryServeFile(ctx, tmpDir, "test.js", nil)
g := NewGzipStatic(true, tmpDir, nil)
served := g.ServeFile(ctx, "test.js")
if !served {
t.Error("TryServeFile() = false, want true")
t.Error("ServeFile() = false, want true")
}
encoding := ctx.Response.Header.Peek("Content-Encoding")