mirror of
				https://github.com/gin-gonic/gin.git
				synced 2025-11-04 17:22:12 +08:00 
			
		
		
		
	* 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
		
			
				
	
	
		
			36 lines
		
	
	
		
			933 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			933 B
		
	
	
	
		
			Go
		
	
	
	
	
	
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)
 | 
						|
}
 |