Merge 86265e039bd1300eb285fa68ea725e921d217699 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Aleksey Fedorov 2026-11-13 00:05:56 +10:00 committed by GitHub
commit a45c26388b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 1 deletions

View File

@ -1193,7 +1193,11 @@ func (c *Context) Cookie(name string) (string, error) {
if err != nil {
return "", err
}
val, _ := url.QueryUnescape(cookie.Value)
val := cookie.Value
// A value that cannot be unescaped was not written by SetCookie, return it as received.
if unescaped, err := url.QueryUnescape(val); err == nil {
val = unescaped
}
return val, nil
}

View File

@ -1142,6 +1142,27 @@ func TestContextGetCookie(t *testing.T) {
require.Error(t, err)
}
func TestContextGetCookieNotEscaped(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodGet, "/get", nil)
// Cookies set by other software are not necessarily query-escaped.
c.Request.Header.Set("Cookie", "discount=50%; progress=100%done; user=John+Doe%21")
for name, want := range map[string]string{
"discount": "50%",
"progress": "100%done",
"user": "John Doe!",
} {
std, err := c.Request.Cookie(name)
require.NoError(t, err)
assert.NotEmpty(t, std.Value)
cookie, err := c.Cookie(name)
require.NoError(t, err)
assert.Equal(t, want, cookie)
}
}
func TestContextBodyAllowedForStatus(t *testing.T) {
assert.False(t, bodyAllowedForStatus(http.StatusContinue))
assert.False(t, bodyAllowedForStatus(http.StatusProcessing))