diff --git a/context.go b/context.go index 582d00f2..fc9ce20e 100644 --- a/context.go +++ b/context.go @@ -648,6 +648,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) diff --git a/context_test.go b/context_test.go index b82e04d5..0d8187ca 100644 --- a/context_test.go +++ b/context_test.go @@ -4055,3 +4055,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")) +}