From 7f4c8f547c70d43d54ed2a8624ab1a2cf5447574 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 13:22:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(compression):=20=E7=A7=BB=E9=99=A4=20T?= =?UTF-8?q?ryServeFile=20=E5=92=8C=20DefaultExtensions=20=E5=85=AC?= =?UTF-8?q?=E5=BC=80=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.6 --- .../middleware/compression/gzip_static.go | 22 ------------------- .../compression/gzip_static_test.go | 12 +++++----- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/internal/middleware/compression/gzip_static.go b/internal/middleware/compression/gzip_static.go index ea4948e..428d8cf 100644 --- a/internal/middleware/compression/gzip_static.go +++ b/internal/middleware/compression/gzip_static.go @@ -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,固定优先级由遍历顺序决定。 diff --git a/internal/middleware/compression/gzip_static_test.go b/internal/middleware/compression/gzip_static_test.go index 5bec097..6192595 100644 --- a/internal/middleware/compression/gzip_static_test.go +++ b/internal/middleware/compression/gzip_static_test.go @@ -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")