fix(context): return cookie values that are not query-escaped

This commit is contained in:
Dv2Corn 2026-11-12 13:52:45 +00:00
parent 0f09c3a9b4
commit 86265e039b
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))