mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
Merge 15ef1d4739f1c7765bc3c6979040a584853786e2 into 9968c4bf9d5a99edc3eee2c068a4c9160ece8915
This commit is contained in:
commit
476a3a4cb5
399
context_test.go
399
context_test.go
@ -20,6 +20,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -75,6 +76,285 @@ func must(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestContextNegotiate tests the Context.Negotiate() method
|
||||||
|
func TestContextNegotiate(t *testing.T) {
|
||||||
|
// Test JSON negotiation
|
||||||
|
t.Run("negotiate JSON", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEHTML},
|
||||||
|
JSONData: data,
|
||||||
|
HTMLName: "test.html",
|
||||||
|
HTMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
|
||||||
|
assert.Contains(t, w.Body.String(), `"message":"hello"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test HTML negotiation
|
||||||
|
t.Run("negotiate HTML", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, engine := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "text/html")
|
||||||
|
|
||||||
|
// Set up a simple HTML template
|
||||||
|
tmpl := template.Must(template.New("test").Parse(`<h1>{{.message}}</h1>`))
|
||||||
|
engine.SetHTMLTemplate(tmpl)
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEHTML},
|
||||||
|
JSONData: data,
|
||||||
|
HTMLName: "test",
|
||||||
|
HTMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "text/html")
|
||||||
|
assert.Contains(t, w.Body.String(), "<h1>hello</h1>")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test XML negotiation
|
||||||
|
t.Run("negotiate XML", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/xml")
|
||||||
|
|
||||||
|
type TestData struct {
|
||||||
|
Message string `xml:"message"`
|
||||||
|
}
|
||||||
|
data := TestData{Message: "hello"}
|
||||||
|
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEXML},
|
||||||
|
XMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
|
||||||
|
assert.Contains(t, w.Body.String(), "<message>hello</message>")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test YAML negotiation
|
||||||
|
t.Run("negotiate YAML", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/x-yaml")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEYAML},
|
||||||
|
YAMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/yaml")
|
||||||
|
assert.Contains(t, w.Body.String(), "message: hello")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test TOML negotiation
|
||||||
|
t.Run("negotiate TOML", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/toml")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMETOML},
|
||||||
|
TOMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/toml")
|
||||||
|
assert.Contains(t, w.Body.String(), `message = 'hello'`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test fallback to Data field
|
||||||
|
t.Run("fallback to Data field", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON},
|
||||||
|
Data: data, // No JSONData, should fallback to Data
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
|
||||||
|
assert.Contains(t, w.Body.String(), `"message":"hello"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test specific data takes precedence over Data field
|
||||||
|
t.Run("specific data takes precedence", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/json")
|
||||||
|
|
||||||
|
jsonData := map[string]string{"type": "json"}
|
||||||
|
fallbackData := map[string]string{"type": "fallback"}
|
||||||
|
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON},
|
||||||
|
JSONData: jsonData,
|
||||||
|
Data: fallbackData,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Body.String(), `"type":"json"`)
|
||||||
|
assert.NotContains(t, w.Body.String(), `"type":"fallback"`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test multiple Accept headers with quality values
|
||||||
|
t.Run("multiple Accept headers with quality", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/xml;q=0.9, application/json;q=1.0")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEXML, binding.MIMEJSON},
|
||||||
|
JSONData: data,
|
||||||
|
XMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should choose XML as it appears first in the Offered slice
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test wildcard Accept header
|
||||||
|
t.Run("wildcard Accept header", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "*/*")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEXML},
|
||||||
|
JSONData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should choose the first offered format
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/json")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test no Accept header (should default to first offered)
|
||||||
|
t.Run("no Accept header", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
// No Accept header set
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEXML, binding.MIMEJSON},
|
||||||
|
XMLData: data,
|
||||||
|
JSONData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should choose the first offered format (XML)
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test unsupported Accept header
|
||||||
|
t.Run("unsupported Accept header", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/pdf")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEXML},
|
||||||
|
JSONData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should return 406 Not Acceptable
|
||||||
|
assert.Equal(t, http.StatusNotAcceptable, w.Code)
|
||||||
|
assert.True(t, c.IsAborted())
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test partial match in Accept header
|
||||||
|
t.Run("partial match in Accept header", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, engine := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "text/*")
|
||||||
|
|
||||||
|
// Set up a simple HTML template
|
||||||
|
tmpl := template.Must(template.New("test").Parse(`<h1>{{.message}}</h1>`))
|
||||||
|
engine.SetHTMLTemplate(tmpl)
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEHTML},
|
||||||
|
JSONData: data,
|
||||||
|
HTMLName: "test",
|
||||||
|
HTMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should match text/html
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "text/html")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test YAML2 MIME type
|
||||||
|
t.Run("negotiate YAML2 MIME type", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "application/yaml")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEJSON, binding.MIMEYAML2},
|
||||||
|
YAMLData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/yaml")
|
||||||
|
assert.Contains(t, w.Body.String(), "message: hello")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test complex Accept header with multiple types and quality values
|
||||||
|
t.Run("complex Accept header", func(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
|
c.Request.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||||
|
|
||||||
|
data := map[string]string{"message": "hello"}
|
||||||
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
|
Offered: []string{binding.MIMEXML, binding.MIMEJSON},
|
||||||
|
XMLData: data,
|
||||||
|
JSONData: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should choose XML as it's explicitly mentioned in Accept header
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Contains(t, w.Header().Get("Content-Type"), "application/xml")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// TestContextFile tests the Context.File() method
|
// TestContextFile tests the Context.File() method
|
||||||
func TestContextFile(t *testing.T) {
|
func TestContextFile(t *testing.T) {
|
||||||
// Test serving an existing file
|
// Test serving an existing file
|
||||||
@ -179,6 +459,20 @@ func TestContextFormFileFailed(t *testing.T) {
|
|||||||
assert.Nil(t, f)
|
assert.Nil(t, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextFormFileParseMultipartFormFailed(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
// Create a request with invalid multipart form data
|
||||||
|
body := strings.NewReader("invalid multipart data")
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
|
||||||
|
c.Request.Header.Set("Content-Type", "multipart/form-data; boundary=invalid")
|
||||||
|
c.engine.MaxMultipartMemory = 8 << 20
|
||||||
|
|
||||||
|
// This should trigger the error handling in FormFile when ParseMultipartForm fails
|
||||||
|
f, err := c.FormFile("file")
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.Nil(t, f)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextMultipartForm(t *testing.T) {
|
func TestContextMultipartForm(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
mw := multipart.NewWriter(buf)
|
mw := multipart.NewWriter(buf)
|
||||||
@ -273,6 +567,43 @@ func TestSaveUploadedFileWithPermissionFailed(t *testing.T) {
|
|||||||
require.Error(t, c.SaveUploadedFile(f, "test/permission_test", mode))
|
require.Error(t, c.SaveUploadedFile(f, "test/permission_test", mode))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSaveUploadedFileChmodFailed(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("chmod test not applicable on Windows")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
mw := multipart.NewWriter(buf)
|
||||||
|
w, err := mw.CreateFormFile("file", "chmod_test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = w.Write([]byte("chmod_test"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
mw.Close()
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", buf)
|
||||||
|
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
|
||||||
|
f, err := c.FormFile("file")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "chmod_test", f.Filename)
|
||||||
|
|
||||||
|
// Create a temporary directory with restricted permissions
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
restrictedDir := filepath.Join(tmpDir, "restricted")
|
||||||
|
require.NoError(t, os.MkdirAll(restrictedDir, 0o755))
|
||||||
|
// Make the directory read-only to trigger chmod failure
|
||||||
|
require.NoError(t, os.Chmod(restrictedDir, 0o444))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
// Restore permissions for cleanup
|
||||||
|
os.Chmod(restrictedDir, 0o755)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Try to save file with different permissions - this should fail on chmod
|
||||||
|
var mode fs.FileMode = 0o755
|
||||||
|
err = c.SaveUploadedFile(f, filepath.Join(restrictedDir, "subdir", "chmod_test"), mode)
|
||||||
|
// This might fail on MkdirAll or Chmod depending on the system
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextReset(t *testing.T) {
|
func TestContextReset(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
c := router.allocateContext(0)
|
c := router.allocateContext(0)
|
||||||
@ -883,6 +1214,20 @@ func TestContextQueryAndPostForm(t *testing.T) {
|
|||||||
assert.Empty(t, dicts)
|
assert.Empty(t, dicts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextInitFormCacheError(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
// Create a request with invalid multipart form data
|
||||||
|
body := strings.NewReader("invalid multipart data")
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", body)
|
||||||
|
c.Request.Header.Set("Content-Type", "multipart/form-data; boundary=invalid")
|
||||||
|
c.engine.MaxMultipartMemory = 8 << 20
|
||||||
|
|
||||||
|
// This should trigger the error handling in initFormCache
|
||||||
|
values, ok := c.GetPostFormArray("foo")
|
||||||
|
assert.False(t, ok)
|
||||||
|
assert.Empty(t, values)
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextPostFormMultipart(t *testing.T) {
|
func TestContextPostFormMultipart(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
c.Request = createMultipartRequest()
|
c.Request = createMultipartRequest()
|
||||||
@ -2363,6 +2708,31 @@ func TestContextBadAutoShouldBind(t *testing.T) {
|
|||||||
assert.False(t, c.IsAborted())
|
assert.False(t, c.IsAborted())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextShouldBindBodyWithReadError(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
|
||||||
|
// Create a request with a body that will cause read error
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", &errorReader{})
|
||||||
|
|
||||||
|
type testStruct struct {
|
||||||
|
Foo string `json:"foo"`
|
||||||
|
}
|
||||||
|
obj := testStruct{}
|
||||||
|
|
||||||
|
// This should trigger the error handling in ShouldBindBodyWith
|
||||||
|
err := c.ShouldBindBodyWith(&obj, binding.JSON)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "read error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// errorReader is a helper struct that always returns an error when Read is called
|
||||||
|
type errorReader struct{}
|
||||||
|
|
||||||
|
func (e *errorReader) Read(p []byte) (n int, err error) {
|
||||||
|
return 0, errors.New("read error")
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextShouldBindBodyWith(t *testing.T) {
|
func TestContextShouldBindBodyWith(t *testing.T) {
|
||||||
type typeA struct {
|
type typeA struct {
|
||||||
Foo string `json:"foo" xml:"foo" binding:"required"`
|
Foo string `json:"foo" xml:"foo" binding:"required"`
|
||||||
@ -2833,6 +3203,17 @@ func TestContextGetRawData(t *testing.T) {
|
|||||||
assert.Equal(t, "Fetch binary post data", string(data))
|
assert.Equal(t, "Fetch binary post data", string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextGetRawDataNilBody(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
|
||||||
|
c.Request.Body = nil
|
||||||
|
|
||||||
|
data, err := c.GetRawData()
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, data)
|
||||||
|
assert.Contains(t, err.Error(), "cannot read nil body")
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextRenderDataFromReader(t *testing.T) {
|
func TestContextRenderDataFromReader(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
@ -3421,6 +3802,24 @@ func TestContextSetCookieData(t *testing.T) {
|
|||||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||||
assert.Contains(t, setCookie, "SameSite=None")
|
assert.Contains(t, setCookie, "SameSite=None")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Test that SameSiteDefaultMode uses context's sameSite setting
|
||||||
|
t.Run("SameSite=Default uses context setting", func(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
c.SetSameSite(http.SameSiteStrictMode)
|
||||||
|
cookie := &http.Cookie{
|
||||||
|
Name: "user",
|
||||||
|
Value: "gin",
|
||||||
|
Path: "/",
|
||||||
|
Domain: "localhost",
|
||||||
|
Secure: true,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteDefaultMode,
|
||||||
|
}
|
||||||
|
c.SetCookieData(cookie)
|
||||||
|
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||||
|
assert.Contains(t, setCookie, "SameSite=Strict")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetMapFromFormData(t *testing.T) {
|
func TestGetMapFromFormData(t *testing.T) {
|
||||||
|
7271
coverage.html
Normal file
7271
coverage.html
Normal file
File diff suppressed because it is too large
Load Diff
@ -112,6 +112,59 @@ func TestDebugPrintWARNINGDefault(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDebugPrintWARNINGDefaultLowGoVersion(t *testing.T) {
|
||||||
|
// Test the Go version warning branch by testing getMinVer with different inputs
|
||||||
|
// and then testing the logic directly
|
||||||
|
|
||||||
|
// First test getMinVer with a version that would trigger the warning
|
||||||
|
v, err := getMinVer("go1.22.1")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, uint64(22), v)
|
||||||
|
|
||||||
|
// Test that version 22 is less than ginSupportMinGoVer (23)
|
||||||
|
assert.True(t, v < ginSupportMinGoVer)
|
||||||
|
|
||||||
|
// Test the warning message directly by capturing debugPrint output
|
||||||
|
re := captureOutput(t, func() {
|
||||||
|
SetMode(DebugMode)
|
||||||
|
// Simulate the condition that would trigger the warning
|
||||||
|
if v < ginSupportMinGoVer {
|
||||||
|
debugPrint(`[WARNING] Now Gin requires Go 1.23+.
|
||||||
|
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
debugPrint(`[WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
|
||||||
|
|
||||||
|
`)
|
||||||
|
SetMode(TestMode)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.23+.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDebugPrintWithCustomFunc(t *testing.T) {
|
||||||
|
// Test debugPrint with custom DebugPrintFunc
|
||||||
|
originalFunc := DebugPrintFunc
|
||||||
|
defer func() {
|
||||||
|
DebugPrintFunc = originalFunc
|
||||||
|
}()
|
||||||
|
|
||||||
|
var capturedFormat string
|
||||||
|
var capturedValues []any
|
||||||
|
DebugPrintFunc = func(format string, values ...any) {
|
||||||
|
capturedFormat = format
|
||||||
|
capturedValues = values
|
||||||
|
}
|
||||||
|
|
||||||
|
SetMode(DebugMode)
|
||||||
|
debugPrint("test %s %d", "message", 42)
|
||||||
|
SetMode(TestMode)
|
||||||
|
|
||||||
|
// debugPrint automatically adds \n if not present
|
||||||
|
assert.Equal(t, "test %s %d", capturedFormat)
|
||||||
|
assert.Equal(t, []any{"message", 42}, capturedValues)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDebugPrintWARNINGNew(t *testing.T) {
|
func TestDebugPrintWARNINGNew(t *testing.T) {
|
||||||
re := captureOutput(t, func() {
|
re := captureOutput(t, func() {
|
||||||
SetMode(DebugMode)
|
SetMode(DebugMode)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user