Merge 371331ef11ebe4736a4061146cf8a95423f79263 into 43fe48e8a0f44af783116cdb010725e6bb50255f

This commit is contained in:
Johnny Lee 2026-10-03 18:04:42 +08:00 committed by GitHub
commit 7c07464d73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 0 deletions

View File

@ -1189,6 +1189,11 @@ func (c *Context) SetCookieData(cookie *http.Cookie) {
// If multiple cookies match the given name, only one cookie will
// be returned.
func (c *Context) Cookie(name string) (string, error) {
// A nil Request (e.g. Context from CreateTestContext) has no cookies:
// report the same error as a request without the named cookie.
if c.Request == nil {
return "", http.ErrNoCookie
}
cookie, err := c.Request.Cookie(name)
if err != nil {
return "", err

View File

@ -927,6 +927,15 @@ func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
})
}
func TestContextCookieWithNilRequest(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil
assert.NotPanics(t, func() {
value, err := c.Cookie("NoCookie")
assert.Empty(t, value)
assert.ErrorIs(t, err, http.ErrNoCookie)
})
}
func TestContextQueryAndPostForm(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
body := strings.NewReader("foo=bar&page=11&both=&foo=second")