Skip to content

Commit 8fbe719

Browse files
Alexander Menzhinskyvishr
authored andcommitted
Add tests for fasthttp engine (#550)
1 parent a0bc028 commit 8fbe719

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

engine/fasthttp/request.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *Request) Header() engine.Header {
6060

6161
// Referer implements `engine.Request#Referer` function.
6262
func (r *Request) Referer() string {
63-
return r.Referer()
63+
return string(r.Request.Header.Referer())
6464
}
6565

6666
// ContentLength implements `engine.Request#ContentLength` function.
@@ -85,7 +85,7 @@ func (r *Request) Method() string {
8585

8686
// SetMethod implements `engine.Request#SetMethod` function.
8787
func (r *Request) SetMethod(method string) {
88-
r.Request.Header.SetMethod(method)
88+
r.Request.Header.SetMethodBytes([]byte(method))
8989
}
9090

9191
// URI implements `engine.Request#URI` function.
@@ -116,10 +116,21 @@ func (r *Request) FormValue(name string) string {
116116
// FormParams implements `engine.Request#FormParams` function.
117117
func (r *Request) FormParams() (params map[string][]string) {
118118
params = make(map[string][]string)
119-
r.PostArgs().VisitAll(func(k, v []byte) {
120-
// TODO: Filling with only first value
121-
params[string(k)] = []string{string(v)}
122-
})
119+
mf, err := r.RequestCtx.MultipartForm()
120+
121+
if err == fasthttp.ErrNoMultipartForm {
122+
r.PostArgs().VisitAll(func(k, v []byte) {
123+
// TODO: Filling with only first value
124+
params[string(k)] = []string{string(v)}
125+
})
126+
} else if err == nil {
127+
for k, v := range mf.Value {
128+
if len(v) > 0 {
129+
params[k] = v
130+
}
131+
}
132+
}
133+
123134
return
124135
}
125136

@@ -136,24 +147,23 @@ func (r *Request) MultipartForm() (*multipart.Form, error) {
136147
// Cookie implements `engine.Request#Cookie` function.
137148
func (r *Request) Cookie(name string) (engine.Cookie, error) {
138149
c := new(fasthttp.Cookie)
139-
c.SetKey(name)
140150
b := r.Request.Header.Cookie(name)
141151
if b == nil {
142152
return nil, echo.ErrCookieNotFound
143153
}
144154
c.ParseBytes(b)
155+
c.SetKey(name)
145156
return &Cookie{c}, nil
146157
}
147158

148159
// Cookies implements `engine.Request#Cookies` function.
149160
func (r *Request) Cookies() []engine.Cookie {
150-
var cookies []engine.Cookie
151-
i := 0
161+
cookies := make([]engine.Cookie, 0)
152162
r.Request.Header.VisitAllCookie(func(name, value []byte) {
153163
c := new(fasthttp.Cookie)
154164
c.SetKey(string(name))
155165
c.ParseBytes(value)
156-
cookies[i] = &Cookie{c}
166+
cookies = append(cookies, &Cookie{c})
157167
})
158168
return cookies
159169
}

engine/fasthttp/request_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fasthttp
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"github.com/labstack/echo/engine/test"
7+
"github.com/labstack/gommon/log"
8+
fast "github.com/valyala/fasthttp"
9+
"net"
10+
"net/url"
11+
"testing"
12+
)
13+
14+
type fakeAddr struct {
15+
addr string
16+
net.Addr
17+
}
18+
19+
func (a fakeAddr) String() string {
20+
return a.addr
21+
}
22+
23+
func TestRequest(t *testing.T) {
24+
var ctx fast.RequestCtx
25+
26+
url, _ := url.Parse("https://github.com/labstack/echo")
27+
ctx.Init(&fast.Request{}, fakeAddr{addr: "127.0.0.1"}, nil)
28+
ctx.Request.Read(bufio.NewReader(bytes.NewBufferString(test.MultipartRequest)))
29+
ctx.Request.SetRequestURI(url.String())
30+
31+
test.RequestTest(t, NewRequest(&ctx, log.New("echo")))
32+
}

engine/standard/cookie_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
func TestCookie(t *testing.T) {
1111
cookie := &Cookie{&http.Cookie{
12-
Name: "session",
13-
Value: "securetoken",
14-
Path: "/",
15-
Domain: "github.com",
16-
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
17-
Secure: true,
18-
HttpOnly: true,
12+
Name: "session",
13+
Value: "securetoken",
14+
Path: "/",
15+
Domain: "github.com",
16+
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
17+
Secure: true,
18+
HttpOnly: true,
1919
}}
2020
test.CookieTest(t, cookie)
2121
}

engine/test/test_request.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func RequestTest(t *testing.T, request engine.Request) {
7878
}
7979

8080
if cookie, err := request.Cookie("session"); assert.NoError(t, err) {
81+
assert.Equal(t, "session", cookie.Name())
8182
assert.Equal(t, "securetoken", cookie.Value())
8283
}
8384

0 commit comments

Comments
 (0)