mirror of
https://github.com/labstack/echo.git
synced 2025-07-17 01:43:02 +02:00
Add tests for fasthttp engine (#550)
This commit is contained in:
committed by
Vishal Rana
parent
a0bc02815f
commit
8fbe719636
@ -60,7 +60,7 @@ func (r *Request) Header() engine.Header {
|
|||||||
|
|
||||||
// Referer implements `engine.Request#Referer` function.
|
// Referer implements `engine.Request#Referer` function.
|
||||||
func (r *Request) Referer() string {
|
func (r *Request) Referer() string {
|
||||||
return r.Referer()
|
return string(r.Request.Header.Referer())
|
||||||
}
|
}
|
||||||
|
|
||||||
// ContentLength implements `engine.Request#ContentLength` function.
|
// ContentLength implements `engine.Request#ContentLength` function.
|
||||||
@ -85,7 +85,7 @@ func (r *Request) Method() string {
|
|||||||
|
|
||||||
// SetMethod implements `engine.Request#SetMethod` function.
|
// SetMethod implements `engine.Request#SetMethod` function.
|
||||||
func (r *Request) SetMethod(method string) {
|
func (r *Request) SetMethod(method string) {
|
||||||
r.Request.Header.SetMethod(method)
|
r.Request.Header.SetMethodBytes([]byte(method))
|
||||||
}
|
}
|
||||||
|
|
||||||
// URI implements `engine.Request#URI` function.
|
// URI implements `engine.Request#URI` function.
|
||||||
@ -116,10 +116,21 @@ func (r *Request) FormValue(name string) string {
|
|||||||
// FormParams implements `engine.Request#FormParams` function.
|
// FormParams implements `engine.Request#FormParams` function.
|
||||||
func (r *Request) FormParams() (params map[string][]string) {
|
func (r *Request) FormParams() (params map[string][]string) {
|
||||||
params = make(map[string][]string)
|
params = make(map[string][]string)
|
||||||
r.PostArgs().VisitAll(func(k, v []byte) {
|
mf, err := r.RequestCtx.MultipartForm()
|
||||||
// TODO: Filling with only first value
|
|
||||||
params[string(k)] = []string{string(v)}
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,24 +147,23 @@ func (r *Request) MultipartForm() (*multipart.Form, error) {
|
|||||||
// Cookie implements `engine.Request#Cookie` function.
|
// Cookie implements `engine.Request#Cookie` function.
|
||||||
func (r *Request) Cookie(name string) (engine.Cookie, error) {
|
func (r *Request) Cookie(name string) (engine.Cookie, error) {
|
||||||
c := new(fasthttp.Cookie)
|
c := new(fasthttp.Cookie)
|
||||||
c.SetKey(name)
|
|
||||||
b := r.Request.Header.Cookie(name)
|
b := r.Request.Header.Cookie(name)
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return nil, echo.ErrCookieNotFound
|
return nil, echo.ErrCookieNotFound
|
||||||
}
|
}
|
||||||
c.ParseBytes(b)
|
c.ParseBytes(b)
|
||||||
|
c.SetKey(name)
|
||||||
return &Cookie{c}, nil
|
return &Cookie{c}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cookies implements `engine.Request#Cookies` function.
|
// Cookies implements `engine.Request#Cookies` function.
|
||||||
func (r *Request) Cookies() []engine.Cookie {
|
func (r *Request) Cookies() []engine.Cookie {
|
||||||
var cookies []engine.Cookie
|
cookies := make([]engine.Cookie, 0)
|
||||||
i := 0
|
|
||||||
r.Request.Header.VisitAllCookie(func(name, value []byte) {
|
r.Request.Header.VisitAllCookie(func(name, value []byte) {
|
||||||
c := new(fasthttp.Cookie)
|
c := new(fasthttp.Cookie)
|
||||||
c.SetKey(string(name))
|
c.SetKey(string(name))
|
||||||
c.ParseBytes(value)
|
c.ParseBytes(value)
|
||||||
cookies[i] = &Cookie{c}
|
cookies = append(cookies, &Cookie{c})
|
||||||
})
|
})
|
||||||
return cookies
|
return cookies
|
||||||
}
|
}
|
||||||
|
32
engine/fasthttp/request_test.go
Normal file
32
engine/fasthttp/request_test.go
Normal 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")))
|
||||||
|
}
|
@ -9,13 +9,13 @@ import (
|
|||||||
|
|
||||||
func TestCookie(t *testing.T) {
|
func TestCookie(t *testing.T) {
|
||||||
cookie := &Cookie{&http.Cookie{
|
cookie := &Cookie{&http.Cookie{
|
||||||
Name: "session",
|
Name: "session",
|
||||||
Value: "securetoken",
|
Value: "securetoken",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Domain: "github.com",
|
Domain: "github.com",
|
||||||
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
|
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||||
Secure: true,
|
Secure: true,
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
}}
|
}}
|
||||||
test.CookieTest(t, cookie)
|
test.CookieTest(t, cookie)
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,7 @@ func RequestTest(t *testing.T, request engine.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cookie, err := request.Cookie("session"); assert.NoError(t, err) {
|
if cookie, err := request.Cookie("session"); assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, "session", cookie.Name())
|
||||||
assert.Equal(t, "securetoken", cookie.Value())
|
assert.Equal(t, "securetoken", cookie.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user