mirror of
https://github.com/labstack/echo.git
synced 2025-01-01 22:09:21 +02:00
parent
fbcdf70c52
commit
fa80ead5e2
@ -155,18 +155,18 @@ func (r *Request) Cookie(name string) (engine.Cookie, error) {
|
||||
if b == nil {
|
||||
return nil, echo.ErrCookieNotFound
|
||||
}
|
||||
c.ParseBytes(b)
|
||||
c.SetKey(name)
|
||||
c.SetValueBytes(b)
|
||||
return &Cookie{c}, nil
|
||||
}
|
||||
|
||||
// Cookies implements `engine.Request#Cookies` function.
|
||||
func (r *Request) Cookies() []engine.Cookie {
|
||||
cookies := make([]engine.Cookie, 0)
|
||||
cookies := []engine.Cookie{}
|
||||
r.Request.Header.VisitAllCookie(func(name, value []byte) {
|
||||
c := new(fasthttp.Cookie)
|
||||
c.SetKey(string(name))
|
||||
c.ParseBytes(value)
|
||||
c.SetKeyBytes(name)
|
||||
c.SetValueBytes(value)
|
||||
cookies = append(cookies, &Cookie{c})
|
||||
})
|
||||
return cookies
|
||||
|
@ -22,12 +22,10 @@ func (a fakeAddr) String() string {
|
||||
}
|
||||
|
||||
func TestRequest(t *testing.T) {
|
||||
var ctx fast.RequestCtx
|
||||
|
||||
ctx := new(fast.RequestCtx)
|
||||
url, _ := url.Parse("http://github.com/labstack/echo")
|
||||
ctx.Init(&fast.Request{}, fakeAddr{addr: "127.0.0.1"}, nil)
|
||||
ctx.Request.Read(bufio.NewReader(bytes.NewBufferString(test.MultipartRequest)))
|
||||
ctx.Request.SetRequestURI(url.String())
|
||||
|
||||
test.RequestTest(t, NewRequest(&ctx, log.New("echo")))
|
||||
test.RequestTest(t, NewRequest(ctx, log.New("echo")))
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ func TestRequest(t *testing.T) {
|
||||
httpReq.RemoteAddr = "127.0.0.1"
|
||||
req := NewRequest(httpReq, log.New("echo"))
|
||||
test.RequestTest(t, req)
|
||||
|
||||
nr, _ := http.NewRequest("GET", "/", nil)
|
||||
req.reset(nr, nil, nil)
|
||||
assert.Equal(t, "", req.Host())
|
||||
|
@ -86,5 +86,12 @@ func RequestTest(t *testing.T, request engine.Request) {
|
||||
_, err := request.Cookie("foo")
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.Equal(t, 2, len(request.Cookies()))
|
||||
// Cookies
|
||||
cs := request.Cookies()
|
||||
if assert.Len(t, cs, 2) {
|
||||
assert.Equal(t, "session", cs[0].Name())
|
||||
assert.Equal(t, "securetoken", cs[0].Value())
|
||||
assert.Equal(t, "user", cs[1].Name())
|
||||
assert.Equal(t, "123", cs[1].Value())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user