@@ -73,7 +73,17 @@ Verbose: true
7373 Path : "/" ,
7474 Handler : func (w http.ResponseWriter , r * http.Request ) {},
7575 }},
76- timeout : time .Minute ,
76+ timeout : ptrOfDuration (time .Minute ),
77+ },
78+ {
79+ jwt : jwtSetting {},
80+ signature : signatureSetting {},
81+ routes : []Route {{
82+ Method : http .MethodGet ,
83+ Path : "/" ,
84+ Handler : func (w http.ResponseWriter , r * http.Request ) {},
85+ }},
86+ timeout : ptrOfDuration (0 ),
7787 },
7888 {
7989 priority : true ,
@@ -84,7 +94,7 @@ Verbose: true
8494 Path : "/" ,
8595 Handler : func (w http.ResponseWriter , r * http.Request ) {},
8696 }},
87- timeout : time .Second ,
97+ timeout : ptrOfDuration ( time .Second ) ,
8898 },
8999 {
90100 priority : true ,
@@ -227,19 +237,82 @@ Verbose: true
227237 }))
228238
229239 timeout := time .Second * 3
230- if route .timeout > timeout {
231- timeout = route .timeout
240+ if route .timeout != nil {
241+ if * route .timeout == 0 {
242+ timeout = 0
243+ } else if * route .timeout > timeout {
244+ timeout = * route .timeout
245+ }
232246 }
233247 assert .Equal (t , timeout , ng .timeout )
234248 })
235249 }
236250 }
237251}
238252
253+ func TestNewEngine_unsignedCallback (t * testing.T ) {
254+ priKeyfile , err := fs .TempFilenameWithText (priKey )
255+ assert .Nil (t , err )
256+ defer os .Remove (priKeyfile )
257+
258+ yaml := `Name: foo
259+ Host: localhost
260+ Port: 0
261+ Middlewares:
262+ Log: false
263+ `
264+ route := featuredRoutes {
265+ priority : true ,
266+ jwt : jwtSetting {
267+ enabled : true ,
268+ },
269+ signature : signatureSetting {
270+ enabled : true ,
271+ SignatureConf : SignatureConf {
272+ Strict : true ,
273+ PrivateKeys : []PrivateKeyConf {
274+ {
275+ Fingerprint : "a" ,
276+ KeyFile : priKeyfile ,
277+ },
278+ },
279+ },
280+ },
281+ routes : []Route {{
282+ Method : http .MethodGet ,
283+ Path : "/" ,
284+ Handler : func (w http.ResponseWriter , r * http.Request ) {},
285+ }},
286+ }
287+
288+ var index int32
289+ t .Run (fmt .Sprintf ("%s-%v" , yaml , route .routes ), func (t * testing.T ) {
290+ var cnf RestConf
291+ assert .Nil (t , conf .LoadFromYamlBytes ([]byte (yaml ), & cnf ))
292+ ng := newEngine (cnf )
293+ if atomic .AddInt32 (& index , 1 )% 2 == 0 {
294+ ng .setUnsignedCallback (func (w http.ResponseWriter , r * http.Request ,
295+ next http.Handler , strict bool , code int ) {
296+ })
297+ }
298+ ng .addRoutes (route )
299+ ng .use (func (next http.HandlerFunc ) http.HandlerFunc {
300+ return func (w http.ResponseWriter , r * http.Request ) {
301+ next .ServeHTTP (w , r )
302+ }
303+ })
304+
305+ assert .NotNil (t , ng .start (mockedRouter {}, func (svr * http.Server ) {
306+ }))
307+
308+ assert .Equal (t , time .Duration (time .Second * 3 ), ng .timeout )
309+ })
310+ }
311+
239312func TestEngine_checkedTimeout (t * testing.T ) {
240313 tests := []struct {
241314 name string
242- timeout time.Duration
315+ timeout * time.Duration
243316 expect time.Duration
244317 }{
245318 {
@@ -248,17 +321,17 @@ func TestEngine_checkedTimeout(t *testing.T) {
248321 },
249322 {
250323 name : "less" ,
251- timeout : time .Millisecond * 500 ,
324+ timeout : ptrOfDuration ( time .Millisecond * 500 ) ,
252325 expect : time .Millisecond * 500 ,
253326 },
254327 {
255328 name : "equal" ,
256- timeout : time .Second ,
329+ timeout : ptrOfDuration ( time .Second ) ,
257330 expect : time .Second ,
258331 },
259332 {
260333 name : "more" ,
261- timeout : time .Millisecond * 1500 ,
334+ timeout : ptrOfDuration ( time .Millisecond * 1500 ) ,
262335 expect : time .Millisecond * 1500 ,
263336 },
264337 }
@@ -401,7 +474,7 @@ func TestEngine_withTimeout(t *testing.T) {
401474 },
402475 })
403476 svr := & http.Server {}
404- ng .withTimeout ()(svr )
477+ ng .withNetworkTimeout ()(svr )
405478
406479 assert .Equal (t , time .Duration (test .timeout )* time .Millisecond * 4 / 5 , svr .ReadTimeout )
407480 assert .Equal (t , time .Duration (0 ), svr .ReadHeaderTimeout )
@@ -451,7 +524,7 @@ func TestEngine_ReadWriteTimeout(t *testing.T) {
451524 },
452525 })
453526 svr := & http.Server {}
454- ng .withTimeout ()(svr )
527+ ng .withNetworkTimeout ()(svr )
455528
456529 assert .Equal (t , time .Duration (0 ), svr .ReadHeaderTimeout )
457530 assert .Equal (t , time .Duration (0 ), svr .IdleTimeout )
0 commit comments