From 2a89d2c43e4b11d53570cce819cc18ebc7d8c2ac Mon Sep 17 00:00:00 2001 From: caplost Date: Mon, 21 Jul 2025 15:38:59 +0800 Subject: [PATCH] fix: resolve gci formatting issues in test files --- context_file_test.go | 11 +++++------ context_test.go | 34 +++++++++++++++++----------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/context_file_test.go b/context_file_test.go index 302870e5..5af010b0 100644 --- a/context_file_test.go +++ b/context_file_test.go @@ -12,13 +12,12 @@ import ( func TestContextFileSimple(t *testing.T) { // Test serving an existing file testFile := "../test_file.txt" - w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) - + c.File(testFile) - + assert.Equal(t, http.StatusOK, w.Code) assert.Contains(t, w.Body.String(), "This is a test file") assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type")) @@ -29,8 +28,8 @@ func TestContextFileNotFound(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) - + c.File("non_existent_file.txt") - + assert.Equal(t, http.StatusNotFound, w.Code) -} \ No newline at end of file +} diff --git a/context_test.go b/context_test.go index 353c0755..cd782e9d 100644 --- a/context_test.go +++ b/context_test.go @@ -81,67 +81,67 @@ func TestContextFile(t *testing.T) { t.Run("serve existing file", func(t *testing.T) { // Create a temporary test file testFile := "../test_file.txt" - + w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) - + c.File(testFile) - + assert.Equal(t, http.StatusOK, w.Code) assert.Contains(t, w.Body.String(), "This is a test file") assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type")) }) - + // Test serving a non-existent file t.Run("serve non-existent file", func(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) - + c.File("non_existent_file.txt") - + assert.Equal(t, http.StatusNotFound, w.Code) }) - + // Test serving a directory (should return 200 with directory listing or 403 Forbidden) t.Run("serve directory", func(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) - + c.File(".") - + // Directory serving can return either 200 (with listing) or 403 (forbidden) assert.True(t, w.Code == http.StatusOK || w.Code == http.StatusForbidden) }) - + // Test with HEAD request t.Run("HEAD request", func(t *testing.T) { testFile := "../test_file.txt" - + w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodHead, "/test", nil) - + c.File(testFile) - + assert.Equal(t, http.StatusOK, w.Code) assert.Empty(t, w.Body.String()) // HEAD request should not return body assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type")) }) - + // Test with Range request t.Run("Range request", func(t *testing.T) { testFile := "../test_file.txt" - + w := httptest.NewRecorder() c, _ := CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/test", nil) c.Request.Header.Set("Range", "bytes=0-10") - + c.File(testFile) - + assert.Equal(t, http.StatusPartialContent, w.Code) assert.Equal(t, "bytes", w.Header().Get("Accept-Ranges")) assert.Contains(t, w.Header().Get("Content-Range"), "bytes 0-10")