mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
Merge 10dd64820b37e92d8d4f23845a4c662e3bab0caf into 43fe48e8a0f44af783116cdb010725e6bb50255f
This commit is contained in:
commit
96d3282fdd
@ -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
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user