@@ -9,12 +9,15 @@ import (
9
9
"github.com/stretchr/testify/assert"
10
10
"net/http"
11
11
"net/http/httptest"
12
+ "net/url"
12
13
"reflect"
14
+ "strings"
13
15
"testing"
14
16
"time"
15
17
)
16
18
17
19
func TestTimeoutSkipper (t * testing.T ) {
20
+ t .Parallel ()
18
21
m := TimeoutWithConfig (TimeoutConfig {
19
22
Skipper : func (context echo.Context ) bool {
20
23
return true
@@ -36,6 +39,7 @@ func TestTimeoutSkipper(t *testing.T) {
36
39
}
37
40
38
41
func TestTimeoutWithTimeout0 (t * testing.T ) {
42
+ t .Parallel ()
39
43
m := TimeoutWithConfig (TimeoutConfig {
40
44
Timeout : 0 ,
41
45
})
@@ -55,6 +59,7 @@ func TestTimeoutWithTimeout0(t *testing.T) {
55
59
}
56
60
57
61
func TestTimeoutIsCancelable (t * testing.T ) {
62
+ t .Parallel ()
58
63
m := TimeoutWithConfig (TimeoutConfig {
59
64
Timeout : time .Minute ,
60
65
})
@@ -74,6 +79,7 @@ func TestTimeoutIsCancelable(t *testing.T) {
74
79
}
75
80
76
81
func TestTimeoutErrorOutInHandler (t * testing.T ) {
82
+ t .Parallel ()
77
83
m := Timeout ()
78
84
79
85
req := httptest .NewRequest (http .MethodGet , "/" , nil )
@@ -90,9 +96,11 @@ func TestTimeoutErrorOutInHandler(t *testing.T) {
90
96
}
91
97
92
98
func TestTimeoutTimesOutAfterPredefinedTimeoutWithErrorHandler (t * testing.T ) {
99
+ t .Parallel ()
93
100
m := TimeoutWithConfig (TimeoutConfig {
94
101
Timeout : time .Second ,
95
102
ErrorHandler : func (err error , e echo.Context ) error {
103
+ assert .EqualError (t , err , context .DeadlineExceeded .Error ())
96
104
return errors .New ("err" )
97
105
},
98
106
})
@@ -112,6 +120,7 @@ func TestTimeoutTimesOutAfterPredefinedTimeoutWithErrorHandler(t *testing.T) {
112
120
}
113
121
114
122
func TestTimeoutTimesOutAfterPredefinedTimeout (t * testing.T ) {
123
+ t .Parallel ()
115
124
m := TimeoutWithConfig (TimeoutConfig {
116
125
Timeout : time .Second ,
117
126
})
@@ -129,3 +138,44 @@ func TestTimeoutTimesOutAfterPredefinedTimeout(t *testing.T) {
129
138
130
139
assert .EqualError (t , err , context .DeadlineExceeded .Error ())
131
140
}
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