From dbc74939d870ed4b046b0afb19bc083b61ec5ad8 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 27 Apr 2026 17:16:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(e2e):=20=E4=BF=AE=E5=A4=8D=20E2E=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BD=BF=E7=94=A8=E6=AD=A3=E7=A1=AE=E7=9A=84?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 StartLolly 替代 StartLollyContainer - 简化 compression_e2e_test.go 的辅助函数 - 移除函数内的 import 语句 Co-Authored-By: Claude Opus 4.7 --- internal/e2e/access_e2e_test.go | 12 +++--- internal/e2e/compression_e2e_test.go | 63 ++++++++++------------------ internal/e2e/ratelimit_e2e_test.go | 8 ++-- internal/e2e/rewrite_e2e_test.go | 12 +++--- 4 files changed, 38 insertions(+), 57 deletions(-) diff --git a/internal/e2e/access_e2e_test.go b/internal/e2e/access_e2e_test.go index d42b0dc..ab9d464 100644 --- a/internal/e2e/access_e2e_test.go +++ b/internal/e2e/access_e2e_test.go @@ -51,7 +51,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -92,7 +92,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -133,7 +133,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -170,7 +170,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -213,7 +213,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -263,7 +263,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) diff --git a/internal/e2e/compression_e2e_test.go b/internal/e2e/compression_e2e_test.go index eb4c173..3df13c7 100644 --- a/internal/e2e/compression_e2e_test.go +++ b/internal/e2e/compression_e2e_test.go @@ -19,6 +19,7 @@ import ( "io" "net/http" "os" + "path/filepath" "testing" "time" @@ -38,9 +39,12 @@ func TestE2ECompressionGzip(t *testing.T) { } // 创建包含可压缩内容的 HTML 文件 - htmlContent := `` + repeatString("

Hello World

", 100) + `` + htmlContent := repeatString("

Hello World

", 100) + tmpDir := t.TempDir() + htmlPath := filepath.Join(tmpDir, "index.html") + require.NoError(t, os.WriteFile(htmlPath, []byte(htmlContent), 0o644)) - config := fmt.Sprintf(` + config := ` servers: - listen: ":8080" static: @@ -52,12 +56,12 @@ servers: - "text/html" - "text/css" - "application/json" -`) +` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config), - testutil.WithExtraMount(createTempHTMLFile(t, "index.html", htmlContent), "/var/www/html/index.html"), + testutil.WithExtraMount(tmpDir, "/var/www/html"), ) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -104,6 +108,9 @@ func TestE2ECompressionNoAcceptEncoding(t *testing.T) { } htmlContent := `

Test Content

` + tmpDir := t.TempDir() + htmlPath := filepath.Join(tmpDir, "index.html") + require.NoError(t, os.WriteFile(htmlPath, []byte(htmlContent), 0o644)) config := ` servers: @@ -116,9 +123,9 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config), - testutil.WithExtraMount(createTempHTMLFile(t, "index.html", htmlContent), "/var/www/html/index.html"), + testutil.WithExtraMount(tmpDir, "/var/www/html"), ) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -148,6 +155,9 @@ func TestE2ECompressionDisabled(t *testing.T) { } htmlContent := repeatString("

Hello World

", 100) + tmpDir := t.TempDir() + htmlPath := filepath.Join(tmpDir, "index.html") + require.NoError(t, os.WriteFile(htmlPath, []byte(htmlContent), 0o644)) config := ` servers: @@ -160,9 +170,9 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config), - testutil.WithExtraMount(createTempHTMLFile(t, "index.html", htmlContent), "/var/www/html/index.html"), + testutil.WithExtraMount(tmpDir, "/var/www/html"), ) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -200,8 +210,8 @@ func TestE2ECompressionPrecompressed(t *testing.T) { gzContent := gzipCompress(t, originalContent) tmpDir := t.TempDir() - writeFile(t, tmpDir+"/test.js", originalContent) - writeFile(t, tmpDir+"/test.js.gz", gzContent) + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "test.js"), []byte(originalContent), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(tmpDir, "test.js.gz"), gzContent, 0o644)) config := ` servers: @@ -213,7 +223,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config), testutil.WithExtraMount(tmpDir, "/var/www/html"), ) @@ -249,35 +259,6 @@ func repeatString(s string, n int) string { return buf.String() } -func createTempHTMLFile(t *testing.T, filename, content string) string { - t.Helper() - tmpDir := t.TempDir() - filePath := tmpDir + "/" + filename - writeFile(t, filePath, content) - return filePath -} - -func writeFile(t *testing.T, path, content string) { - t.Helper() - require.NoError(t, writeFileErr(path, content)) -} - -func writeFileErr(path, content string) error { - return writeFileBytes(path, []byte(content)) -} - -func writeFileBytes(path string, content []byte) error { - return writeFileBytesWithPerm(path, content, 0o644) -} - -func writeFileBytesWithPerm(path string, content []byte, perm uint32) error { - return writeFileWithPerm(path, content, perm) -} - -func writeFileWithPerm(path string, content []byte, perm uint32) error { - return os.WriteFile(path, content, os.FileMode(perm)) -} - func gzipCompress(t *testing.T, content string) []byte { t.Helper() var buf bytes.Buffer diff --git a/internal/e2e/ratelimit_e2e_test.go b/internal/e2e/ratelimit_e2e_test.go index f2ce0e6..42405f4 100644 --- a/internal/e2e/ratelimit_e2e_test.go +++ b/internal/e2e/ratelimit_e2e_test.go @@ -54,7 +54,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -123,7 +123,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -177,7 +177,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -241,7 +241,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) diff --git a/internal/e2e/rewrite_e2e_test.go b/internal/e2e/rewrite_e2e_test.go index 7cbf4db..dd3b367 100644 --- a/internal/e2e/rewrite_e2e_test.go +++ b/internal/e2e/rewrite_e2e_test.go @@ -47,7 +47,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -92,7 +92,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -138,7 +138,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -180,7 +180,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -232,7 +232,7 @@ servers: `, backendAddr) // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx) @@ -274,7 +274,7 @@ servers: ` // 启动 lolly - lolly, err := testutil.StartLollyContainer(ctx, "", testutil.WithConfigYAML(config)) + lolly, err := testutil.StartLolly(ctx, testutil.WithConfigYAML(config)) require.NoError(t, err, "Failed to start lolly container") defer lolly.Terminate(ctx)