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
This commit is contained in:
caplost 2025-07-21 13:47:39 +08:00
parent 5826722a87
commit 99964e1145
2 changed files with 109 additions and 0 deletions

36
context_file_test.go Normal file
View File

@ -0,0 +1,36 @@
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 := "../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 := "../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")
})
}
func TestContextFormFile(t *testing.T) { func TestContextFormFile(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
mw := multipart.NewWriter(buf) mw := multipart.NewWriter(buf)