mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 14:49:27 +08:00
fix: prevent panic in PostForm getters when Request is nil
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
This commit is contained in:
parent
dcaa4296d1
commit
9b5c2e4046
@ -649,6 +649,9 @@ func (c *Context) initFormCache() {
|
||||
if c.formCache == nil {
|
||||
c.formCache = make(url.Values)
|
||||
req := c.Request
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
|
||||
if !errors.Is(err, http.ErrNotMultipart) {
|
||||
debugPrint("error on parse multipart form array: %v", err)
|
||||
|
||||
@ -3955,3 +3955,13 @@ func BenchmarkGetMapFromFormData(b *testing.B) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextPostFormNoRequest(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
value, ok := c.GetPostForm("key")
|
||||
assert.False(t, ok)
|
||||
assert.Empty(t, value)
|
||||
assert.Empty(t, c.PostForm("key"))
|
||||
assert.Equal(t, "fallback", c.DefaultPostForm("key", "fallback"))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user