1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-01 22:09:21 +02:00

Add tests for fasthttp engine (#550)

This commit is contained in:
Alexander Menzhinsky 2016-06-07 05:23:59 +03:00 committed by Vishal Rana
parent a0bc02815f
commit 8fbe719636
4 changed files with 60 additions and 17 deletions

View File

@ -60,7 +60,7 @@ func (r *Request) Header() engine.Header {
// Referer implements `engine.Request#Referer` function.
func (r *Request) Referer() string {
return r.Referer()
return string(r.Request.Header.Referer())
}
// ContentLength implements `engine.Request#ContentLength` function.
@ -85,7 +85,7 @@ func (r *Request) Method() string {
// SetMethod implements `engine.Request#SetMethod` function.
func (r *Request) SetMethod(method string) {
r.Request.Header.SetMethod(method)
r.Request.Header.SetMethodBytes([]byte(method))
}
// URI implements `engine.Request#URI` function.
@ -116,10 +116,21 @@ func (r *Request) FormValue(name string) string {
// FormParams implements `engine.Request#FormParams` function.
func (r *Request) FormParams() (params map[string][]string) {
params = make(map[string][]string)
r.PostArgs().VisitAll(func(k, v []byte) {
// TODO: Filling with only first value
params[string(k)] = []string{string(v)}
})
mf, err := r.RequestCtx.MultipartForm()
if err == fasthttp.ErrNoMultipartForm {
r.PostArgs().VisitAll(func(k, v []byte) {
// TODO: Filling with only first value
params[string(k)] = []string{string(v)}
})
} else if err == nil {
for k, v := range mf.Value {
if len(v) > 0 {
params[k] = v
}
}
}
return
}
@ -136,24 +147,23 @@ func (r *Request) MultipartForm() (*multipart.Form, error) {
// Cookie implements `engine.Request#Cookie` function.
func (r *Request) Cookie(name string) (engine.Cookie, error) {
c := new(fasthttp.Cookie)
c.SetKey(name)
b := r.Request.Header.Cookie(name)
if b == nil {
return nil, echo.ErrCookieNotFound
}
c.ParseBytes(b)
c.SetKey(name)
return &Cookie{c}, nil
}
// Cookies implements `engine.Request#Cookies` function.
func (r *Request) Cookies() []engine.Cookie {
var cookies []engine.Cookie
i := 0
cookies := make([]engine.Cookie, 0)
r.Request.Header.VisitAllCookie(func(name, value []byte) {
c := new(fasthttp.Cookie)
c.SetKey(string(name))
c.ParseBytes(value)
cookies[i] = &Cookie{c}
cookies = append(cookies, &Cookie{c})
})
return cookies
}

View File

@ -0,0 +1,32 @@
package fasthttp
import (
"bufio"
"bytes"
"github.com/labstack/echo/engine/test"
"github.com/labstack/gommon/log"
fast "github.com/valyala/fasthttp"
"net"
"net/url"
"testing"
)
type fakeAddr struct {
addr string
net.Addr
}
func (a fakeAddr) String() string {
return a.addr
}
func TestRequest(t *testing.T) {
var ctx fast.RequestCtx
url, _ := url.Parse("https://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")))
}

View File

@ -9,13 +9,13 @@ import (
func TestCookie(t *testing.T) {
cookie := &Cookie{&http.Cookie{
Name: "session",
Value: "securetoken",
Path: "/",
Domain: "github.com",
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
Secure: true,
HttpOnly: true,
Name: "session",
Value: "securetoken",
Path: "/",
Domain: "github.com",
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
Secure: true,
HttpOnly: true,
}}
test.CookieTest(t, cookie)
}

View File

@ -78,6 +78,7 @@ func RequestTest(t *testing.T, request engine.Request) {
}
if cookie, err := request.Cookie("session"); assert.NoError(t, err) {
assert.Equal(t, "session", cookie.Name())
assert.Equal(t, "securetoken", cookie.Value())
}