@@ -189,16 +189,14 @@ func TestRouterOf_Middleware(t *testing.T) {
189
189
def := std .NewRouter ("" )
190
190
a .NotNil (def )
191
191
def .Use (tree .BuildTestMiddleware (a , "m1" ), tree .BuildTestMiddleware (a , "m2" ), tree .BuildTestMiddleware (a , "m3" ), tree .BuildTestMiddleware (a , "m4" ))
192
- def .Get ("/get" , rest .BuildHandler (a , 201 , "" , nil ))
192
+ def .Get ("/get" , rest .BuildHandler (a , 201 , "" , nil ), tree . BuildTestMiddleware ( a , "m0" ) )
193
193
def .Post ("/get" , rest .BuildHandler (a , 201 , "" , nil ))
194
194
195
- rest .Get (a , "/get" ).Do (def ).Status (201 ).StringBody ("m1m2m3m4" )
196
- rest .Post (a , "/get" , nil ).Do (def ).Status (201 ).StringBody ("m1m2m3m4" )
197
- rest .Get (a , "/get" ).Do (def ).Status (201 ).StringBody ("m1m2m3m4" )
195
+ rest .Get (a , "/get" ).Do (def ).Status (201 ).StringBody ("m0m1m2m3m4" )
198
196
rest .Post (a , "/get" , nil ).Do (def ).Status (201 ).StringBody ("m1m2m3m4" )
199
197
200
198
def .Use (tree .BuildTestMiddleware (a , "m5" ), tree .BuildTestMiddleware (a , "m6" ))
201
- rest .Get (a , "/get" ).Do (def ).Status (201 ).StringBody ("m1m2m3m4m5m6 " )
199
+ rest .Get (a , "/get" ).Do (def ).Status (201 ).StringBody ("m0m1m2m3m4m5m6 " )
202
200
}
203
201
204
202
func TestResourceOf (t * testing.T ) {
@@ -247,17 +245,18 @@ func TestRouterOf_Resource(t *testing.T) {
247
245
a := assert .New (t , false )
248
246
def := std .NewRouter ("" )
249
247
a .NotNil (def )
248
+ def .Use (tree .BuildTestMiddleware (a , "d1" ))
250
249
251
- r1 := def .Resource ("/abc/1" )
250
+ r1 := def .Resource ("/abc/1" , tree . BuildTestMiddleware ( a , "r1" ) )
252
251
a .NotNil (r1 )
253
252
a .Equal (r1 .Router (), def )
254
253
255
- r2 := def .Resource ("/abc/1" )
254
+ r2 := def .Resource ("/abc/1" , tree . BuildTestMiddleware ( a , "r1" ) )
256
255
a .NotNil (r2 )
257
256
a .False (r1 == r2 ) // 不是同一个 *Resource
258
257
259
- r2 .Delete (rest .BuildHandler (a , 201 , "" , nil ))
260
- rest .Delete (a , "/abc/1" ).Do (def ).Status (201 )
258
+ r2 .Delete (rest .BuildHandler (a , 201 , "-201- " , nil ), tree . BuildTestMiddleware ( a , "d1" ))
259
+ rest .Delete (a , "/abc/1" ).Do (def ).Status (201 ). StringBody ( "-201-d1r1d1" )
261
260
}
262
261
263
262
func TestPrefix_Resource (t * testing.T ) {
@@ -272,9 +271,9 @@ func TestPrefix_Resource(t *testing.T) {
272
271
r1 := p .Resource ("/abc/1" , tree .BuildTestMiddleware (a , "r1" ), tree .BuildTestMiddleware (a , "r2" ))
273
272
a .NotNil (r1 )
274
273
275
- r1 .Delete (rest .BuildHandler (a , 201 , "-201-" , nil ))
276
- rest .Delete (a , "/p1/abc/1" ).Do (def ).Status (201 ).StringBody ("-201-p1p2r1r2 " )
277
- rest .Delete (a , "/p1/abc/1" ).Do (def ).Status (201 ).StringBody ("-201-p1p2r1r2 " )
274
+ r1 .Delete (rest .BuildHandler (a , 201 , "-201-" , nil ), tree . BuildTestMiddleware ( a , "m1" ) )
275
+ rest .Delete (a , "/p1/abc/1" ).Do (def ).Status (201 ).StringBody ("-201-m1r1r2p1p2 " )
276
+ rest .Delete (a , "/p1/abc/1" ).Do (def ).Status (201 ).StringBody ("-201-m1r1r2p1p2 " )
278
277
}
279
278
280
279
func TestResourceOf_URL (t * testing.T ) {
@@ -374,7 +373,7 @@ func TestPrefixOf(t *testing.T) {
374
373
rest .NewRequest (a , http .MethodOptions , "/p/h/any" ).Do (r ).Status (404 )
375
374
}
376
375
377
- func TestRouterOf_Prefix (t * testing.T ) {
376
+ func TestPrefixOf_Prefix (t * testing.T ) {
378
377
t .Run ("prefix" , func (t * testing.T ) {
379
378
a := assert .New (t , false )
380
379
def := std .NewRouter ("" , mux .AllowedCORS (3600 ))
@@ -384,7 +383,7 @@ func TestRouterOf_Prefix(t *testing.T) {
384
383
a .Equal (p .Router (), def )
385
384
})
386
385
387
- t .Run ("prefix with middleware " , func (t * testing.T ) {
386
+ t .Run ("empty prefix " , func (t * testing.T ) {
388
387
a := assert .New (t , false )
389
388
def := std .NewRouter ("" , mux .AllowedCORS (3600 ))
390
389
a .NotNil (def )
@@ -397,42 +396,27 @@ func TestRouterOf_Prefix(t *testing.T) {
397
396
rest .Delete (a , "/abc" ).Do (def ).Status (201 )
398
397
})
399
398
400
- t .Run ("empty prefix with middleware" , func (t * testing.T ) {
399
+ //
400
+ t .Run ("prefix 的中间调用顺序" , func (t * testing.T ) {
401
401
a := assert .New (t , false )
402
402
def := std .NewRouter ("" , mux .AllowedCORS (3600 ))
403
403
a .NotNil (def )
404
+ def .Use (tree .BuildTestMiddleware (a , "r0" ))
404
405
405
- p := def .Prefix ("/abc" )
406
+ p := def .Prefix ("/abc" , tree . BuildTestMiddleware ( a , "p1" ) )
406
407
a .Equal (p .Router (), def )
407
408
408
- pp := p .Prefix ("" , tree .BuildTestMiddleware (a , "p1 " ), tree .BuildTestMiddleware (a , "p2 " ))
409
+ pp := p .Prefix ("" , tree .BuildTestMiddleware (a , "p2 " ), tree .BuildTestMiddleware (a , "p3 " ))
409
410
pp .Delete ("" , rest .BuildHandler (a , 201 , "-201-" , nil ))
410
411
411
- rest .Delete (a , "/abc" ).Do (def ).Status (201 ).StringBody ("-201-p1p2 " )
412
+ rest .Delete (a , "/abc" ).Do (def ).Status (201 ).StringBody ("-201-p2p3p1r0 " )
412
413
413
414
// Trace
414
415
rest .NewRequest (a , http .MethodTrace , "/abc" ).Do (def ).Status (http .StatusMethodNotAllowed )
415
416
rest .NewRequest (a , http .MethodTrace , "/abc/not-exists" ).Do (def ).Status (http .StatusNotFound )
416
417
})
417
418
}
418
419
419
- func TestPrefixOf_Prefix (t * testing.T ) {
420
- a := assert .New (t , false )
421
- def := std .NewRouter ("" , mux .AllowedCORS (3600 ), mux .Trace (true ))
422
- a .NotNil (def )
423
-
424
- p := def .Prefix ("/abc" , tree .BuildTestMiddleware (a , "p1" ), tree .BuildTestMiddleware (a , "p2" ))
425
- pp := p .Prefix ("/def" , tree .BuildTestMiddleware (a , "pp1" ), tree .BuildTestMiddleware (a , "pp2" ))
426
- a .Equal (p .Router (), def )
427
- pp .Delete ("" , rest .BuildHandler (a , 201 , "-201-" , nil ))
428
-
429
- rest .Delete (a , "/abc/def" ).Do (def ).Status (201 ).StringBody ("-201-p1p2pp1pp2" )
430
-
431
- // Trace
432
- rest .NewRequest (a , http .MethodTrace , "/abc/def" ).Do (def ).Status (http .StatusOK )
433
- rest .NewRequest (a , http .MethodTrace , "/abc/not-exists" ).Do (def ).Status (http .StatusOK )
434
- }
435
-
436
420
func TestPrefixOf_URL (t * testing.T ) {
437
421
a := assert .New (t , false )
438
422
def := std .NewRouter ("" , mux .AllowedCORS (3600 ), mux .URLDomain ("https://example.com" ))
0 commit comments