Skip to content

Commit be0a3ea

Browse files
committed
Added tests for cloning request for Form, QueryString, and Cookies
1 parent b966fec commit be0a3ea

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

middleware/timeout_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"net/http"
1111
"net/http/httptest"
12+
"net/url"
1213
"reflect"
14+
"strings"
1315
"testing"
1416
"time"
1517
)
1618

1719
func TestTimeoutSkipper(t *testing.T) {
20+
t.Parallel()
1821
m := TimeoutWithConfig(TimeoutConfig{
1922
Skipper: func(context echo.Context) bool {
2023
return true
@@ -36,6 +39,7 @@ func TestTimeoutSkipper(t *testing.T) {
3639
}
3740

3841
func TestTimeoutWithTimeout0(t *testing.T) {
42+
t.Parallel()
3943
m := TimeoutWithConfig(TimeoutConfig{
4044
Timeout: 0,
4145
})
@@ -55,6 +59,7 @@ func TestTimeoutWithTimeout0(t *testing.T) {
5559
}
5660

5761
func TestTimeoutIsCancelable(t *testing.T) {
62+
t.Parallel()
5863
m := TimeoutWithConfig(TimeoutConfig{
5964
Timeout: time.Minute,
6065
})
@@ -74,6 +79,7 @@ func TestTimeoutIsCancelable(t *testing.T) {
7479
}
7580

7681
func TestTimeoutErrorOutInHandler(t *testing.T) {
82+
t.Parallel()
7783
m := Timeout()
7884

7985
req := httptest.NewRequest(http.MethodGet, "/", nil)
@@ -90,9 +96,11 @@ func TestTimeoutErrorOutInHandler(t *testing.T) {
9096
}
9197

9298
func TestTimeoutTimesOutAfterPredefinedTimeoutWithErrorHandler(t *testing.T) {
99+
t.Parallel()
93100
m := TimeoutWithConfig(TimeoutConfig{
94101
Timeout: time.Second,
95102
ErrorHandler: func(err error, e echo.Context) error {
103+
assert.EqualError(t, err, context.DeadlineExceeded.Error())
96104
return errors.New("err")
97105
},
98106
})
@@ -112,6 +120,7 @@ func TestTimeoutTimesOutAfterPredefinedTimeoutWithErrorHandler(t *testing.T) {
112120
}
113121

114122
func TestTimeoutTimesOutAfterPredefinedTimeout(t *testing.T) {
123+
t.Parallel()
115124
m := TimeoutWithConfig(TimeoutConfig{
116125
Timeout: time.Second,
117126
})
@@ -129,3 +138,44 @@ func TestTimeoutTimesOutAfterPredefinedTimeout(t *testing.T) {
129138

130139
assert.EqualError(t, err, context.DeadlineExceeded.Error())
131140
}
141+
142+
func TestTimeoutTestRequestClone(t *testing.T) {
143+
t.Parallel()
144+
req := httptest.NewRequest(http.MethodPost, "/uri?query=value", strings.NewReader(url.Values{"form": {"value"}}.Encode()))
145+
req.AddCookie(&http.Cookie{Name: "cookie", Value: "value"})
146+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
147+
rec := httptest.NewRecorder()
148+
149+
m := TimeoutWithConfig(TimeoutConfig{
150+
Timeout: time.Second,
151+
ErrorHandler: func(err error, c echo.Context) error {
152+
assert.EqualError(t, err, context.DeadlineExceeded.Error())
153+
154+
// Cookie test
155+
cookie, err := c.Request().Cookie("cookie")
156+
if assert.NoError(t, err) {
157+
assert.EqualValues(t, "cookie", cookie.Name)
158+
assert.EqualValues(t, "value", cookie.Value)
159+
}
160+
161+
// Form values
162+
if assert.NoError(t, c.Request().ParseForm()) {
163+
assert.EqualValues(t, "value", c.Request().FormValue("form"))
164+
}
165+
166+
// Query string
167+
assert.EqualValues(t, "value", c.Request().URL.Query()["query"][0])
168+
return errors.New("err")
169+
},
170+
})
171+
172+
e := echo.New()
173+
c := e.NewContext(req, rec)
174+
175+
err := m(func(c echo.Context) error {
176+
time.Sleep(time.Minute)
177+
return nil
178+
})(c)
179+
180+
assert.Error(t, err)
181+
}

0 commit comments

Comments
 (0)