test(context): add comprehensive unit tests for Context.File() method (#4307)

* test: add comprehensive unit tests for Context.File() method

- Add TestContextFile with multiple test scenarios in context_test.go
- Add simplified tests in context_file_test.go
- Cover file serving, 404 handling, directory access, HEAD requests, and Range requests
- All tests pass successfully

* fix: resolve gci formatting issues in test files

* fix: correct test file paths and add test file to gin directory

* move test_file.txt to testdata directory as suggested by maintainer

* fix: update test file paths to use testdata directory
This commit is contained in:
Leon cap 2025-07-22 21:38:32 +08:00 committed by GitHub
parent 9708475b3b
commit dab5944a7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 110 additions and 0 deletions

35
context_file_test.go Normal file
View File

@ -0,0 +1,35 @@
package gin
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// TestContextFileSimple tests the Context.File() method with a simple case
func TestContextFileSimple(t *testing.T) {
// Test serving an existing file
testFile := "testdata/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"))
}
// TestContextFileNotFound tests serving a non-existent file
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)
}

View File

@ -75,6 +75,79 @@ func must(err error) {
}
}
// TestContextFile tests the Context.File() method
func TestContextFile(t *testing.T) {
// Test serving an existing file
t.Run("serve existing file", func(t *testing.T) {
// Create a temporary test file
testFile := "testdata/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 := "testdata/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 := "testdata/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")
})
}
func TestContextFormFile(t *testing.T) {
buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf)

2
testdata/test_file.txt vendored Normal file
View File

@ -0,0 +1,2 @@
This is a test file for Context.File() method testing.
It contains some sample content to verify file serving functionality.