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:
Sai Asish Y 2026-08-24 19:17:29 -07:00
parent dcaa4296d1
commit 9b5c2e4046
2 changed files with 13 additions and 0 deletions

View File

@ -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)

View File

@ -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"))
}