mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
initialised a fix that prevent panic in orm
This commit is contained in:
parent
5c6a15f8f9
commit
dcc508aa44
10
context.go
10
context.go
@ -649,12 +649,14 @@ func (c *Context) initFormCache() {
|
||||
if c.formCache == nil {
|
||||
c.formCache = make(url.Values)
|
||||
req := c.Request
|
||||
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
|
||||
if !errors.Is(err, http.ErrNotMultipart) {
|
||||
debugPrint("error on parse multipart form array: %v", err)
|
||||
if req != nil {
|
||||
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
|
||||
if !errors.Is(err, http.ErrNotMultipart) {
|
||||
debugPrint("error on parse multipart form array: %v", err)
|
||||
}
|
||||
}
|
||||
c.formCache = req.PostForm
|
||||
}
|
||||
c.formCache = req.PostForm
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -4031,3 +4031,17 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextFormWithoutRequest(t *testing.T) {
|
||||
// Regression test for https://github.com/gin-gonic/gin/issues/4772
|
||||
// Form getters should not panic when c.Request is nil, and should
|
||||
// behave like the query getters by returning empty/default values.
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
assert.Equal(t, "", c.PostForm("key"))
|
||||
assert.Equal(t, "fallback", c.DefaultPostForm("key", "fallback"))
|
||||
|
||||
value, ok := c.GetPostForm("key")
|
||||
assert.Equal(t, "", value)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user