mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
fix: resolve gci formatting issues in test files
This commit is contained in:
parent
99964e1145
commit
2a89d2c43e
@ -12,13 +12,12 @@ import (
|
|||||||
func TestContextFileSimple(t *testing.T) {
|
func TestContextFileSimple(t *testing.T) {
|
||||||
// Test serving an existing file
|
// Test serving an existing file
|
||||||
testFile := "../test_file.txt"
|
testFile := "../test_file.txt"
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
|
||||||
c.File(testFile)
|
c.File(testFile)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Contains(t, w.Body.String(), "This is a test file")
|
assert.Contains(t, w.Body.String(), "This is a test file")
|
||||||
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
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()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
|
||||||
c.File("non_existent_file.txt")
|
c.File("non_existent_file.txt")
|
||||||
|
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||||
}
|
}
|
||||||
|
@ -81,67 +81,67 @@ func TestContextFile(t *testing.T) {
|
|||||||
t.Run("serve existing file", func(t *testing.T) {
|
t.Run("serve existing file", func(t *testing.T) {
|
||||||
// Create a temporary test file
|
// Create a temporary test file
|
||||||
testFile := "../test_file.txt"
|
testFile := "../test_file.txt"
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
|
||||||
c.File(testFile)
|
c.File(testFile)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Contains(t, w.Body.String(), "This is a test file")
|
assert.Contains(t, w.Body.String(), "This is a test file")
|
||||||
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test serving a non-existent file
|
// Test serving a non-existent file
|
||||||
t.Run("serve non-existent file", func(t *testing.T) {
|
t.Run("serve non-existent file", func(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
|
||||||
c.File("non_existent_file.txt")
|
c.File("non_existent_file.txt")
|
||||||
|
|
||||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test serving a directory (should return 200 with directory listing or 403 Forbidden)
|
// Test serving a directory (should return 200 with directory listing or 403 Forbidden)
|
||||||
t.Run("serve directory", func(t *testing.T) {
|
t.Run("serve directory", func(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
|
||||||
c.File(".")
|
c.File(".")
|
||||||
|
|
||||||
// Directory serving can return either 200 (with listing) or 403 (forbidden)
|
// Directory serving can return either 200 (with listing) or 403 (forbidden)
|
||||||
assert.True(t, w.Code == http.StatusOK || w.Code == http.StatusForbidden)
|
assert.True(t, w.Code == http.StatusOK || w.Code == http.StatusForbidden)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test with HEAD request
|
// Test with HEAD request
|
||||||
t.Run("HEAD request", func(t *testing.T) {
|
t.Run("HEAD request", func(t *testing.T) {
|
||||||
testFile := "../test_file.txt"
|
testFile := "../test_file.txt"
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodHead, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodHead, "/test", nil)
|
||||||
|
|
||||||
c.File(testFile)
|
c.File(testFile)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
assert.Empty(t, w.Body.String()) // HEAD request should not return body
|
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"))
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Test with Range request
|
// Test with Range request
|
||||||
t.Run("Range request", func(t *testing.T) {
|
t.Run("Range request", func(t *testing.T) {
|
||||||
testFile := "../test_file.txt"
|
testFile := "../test_file.txt"
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
c.Request.Header.Set("Range", "bytes=0-10")
|
c.Request.Header.Set("Range", "bytes=0-10")
|
||||||
|
|
||||||
c.File(testFile)
|
c.File(testFile)
|
||||||
|
|
||||||
assert.Equal(t, http.StatusPartialContent, w.Code)
|
assert.Equal(t, http.StatusPartialContent, w.Code)
|
||||||
assert.Equal(t, "bytes", w.Header().Get("Accept-Ranges"))
|
assert.Equal(t, "bytes", w.Header().Get("Accept-Ranges"))
|
||||||
assert.Contains(t, w.Header().Get("Content-Range"), "bytes 0-10")
|
assert.Contains(t, w.Header().Get("Content-Range"), "bytes 0-10")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user