mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-20 17:23:37 +08:00
fix(context): return cookie values that are not query-escaped
This commit is contained in:
parent
0f09c3a9b4
commit
86265e039b
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user