fix: handle nil request in post form getters

This commit is contained in:
Shantanav 2026-07-29 11:22:27 +05:30
parent 34dac209ff
commit d39aabae8a
2 changed files with 18 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

@ -903,6 +903,21 @@ func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
})
}
func TestContextPostFormOnEmptyRequest(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil
assert.NotPanics(t, func() {
value, ok := c.GetPostForm("NoKey")
assert.False(t, ok)
assert.Empty(t, value)
})
assert.NotPanics(t, func() {
assert.Equal(t, "nada", c.DefaultPostForm("NoKey", "nada"))
})
assert.NotPanics(t, func() {
assert.Empty(t, c.PostForm("NoKey"))
})
}
func TestContextQueryAndPostForm(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
body := strings.NewReader("foo=bar&page=11&both=&foo=second")