@@ -272,15 +272,17 @@ func TestSegment_Match(t *testing.T) {
272
272
// Named:any
273
273
seg , err := i .NewSegment ("{id:any}/author" )
274
274
a .NotError (err ).NotNil (seg )
275
- p := types .NewContext ("1/author" )
275
+ p := types .NewContext ()
276
+ p .Path = "1/author"
276
277
a .True (seg .Match (p )).
277
278
Empty (p .Path ).
278
279
Equal (1 , p .Count ()).Equal (p .MustString ("id" , "not-exists" ), "1" )
279
280
280
281
// Named 完全匹配
281
282
seg , err = i .NewSegment ("{id}/author" )
282
283
a .NotError (err ).NotNil (seg )
283
- p = types .NewContext ("1/author" )
284
+ p = types .NewContext ()
285
+ p .Path = "1/author"
284
286
a .True (seg .Match (p )).
285
287
Empty (p .Path ).
286
288
Equal (1 , p .Count ()).
@@ -289,7 +291,8 @@ func TestSegment_Match(t *testing.T) {
289
291
// Named 部分匹配
290
292
seg , err = i .NewSegment ("{id}/author" )
291
293
a .NotError (err ).NotNil (seg )
292
- p = types .NewContext ("1/author/email" )
294
+ p = types .NewContext ()
295
+ p .Path = "1/author/email"
293
296
a .True (seg .Match (p ))
294
297
a .Equal (p .Path , "/email" ).
295
298
Equal (1 , p .Count ()).
@@ -298,14 +301,16 @@ func TestSegment_Match(t *testing.T) {
298
301
// Named 不匹配
299
302
seg , err = i .NewSegment ("{id}/author" )
300
303
a .NotError (err ).NotNil (seg )
301
- p = types .NewContext ("1/aut" )
304
+ p = types .NewContext ()
305
+ p .Path = "1/aut"
302
306
a .False (seg .Match (p ))
303
307
a .Equal (p .Path , "1/aut" ).Zero (p .Count ())
304
308
305
309
// Named 1/2 匹配 {id}
306
310
seg , err = i .NewSegment ("{id}/author" )
307
311
a .NotError (err ).NotNil (seg )
308
- p = types .NewContext ("1/2/author" )
312
+ p = types .NewContext ()
313
+ p .Path = "1/2/author"
309
314
a .True (seg .Match (p )).
310
315
Equal (p .Path , "" ).
311
316
Equal (1 , p .Count ()).
@@ -314,29 +319,33 @@ func TestSegment_Match(t *testing.T) {
314
319
// Interceptor 1/2 匹配 {id}
315
320
seg , err = i .NewSegment ("{id:any}/author" )
316
321
a .NotError (err ).NotNil (seg )
317
- p = types .NewContext ("1/2/author" )
322
+ p = types .NewContext ()
323
+ p .Path = "1/2/author"
318
324
a .True (seg .Match (p )).
319
325
Equal (p .Path , "" ).
320
326
Equal (1 , p .Count ()).
321
327
Equal (p .MustString ("id" , "not-exists" ), "1/2" )
322
328
323
329
seg , err = i .NewSegment ("{any}/123" )
324
330
a .NotError (err ).NotNil (seg )
325
- p = types .NewContext ("123" )
331
+ p = types .NewContext ()
332
+ p .Path = "123"
326
333
a .False (seg .Match (p )).
327
334
Equal (p .Path , "123" ).Zero (p .Count ())
328
335
329
336
seg , err = i .NewSegment ("{any:any}/123" )
330
337
a .NotError (err ).NotNil (seg )
331
- p = types .NewContext ("123" )
338
+ p = types .NewContext ()
339
+ p .Path = "123"
332
340
a .False (seg .Match (p )).
333
341
Equal (p .Path , "123" ).Zero (p .Count ())
334
342
335
343
// 命名参数,any 匹配到了空.
336
344
seg , err = i .NewSegment ("{any}123" )
337
345
a .NotError (err ).NotNil (seg )
338
346
a .Equal (seg .Type , Named )
339
- p = types .NewContext ("123123" )
347
+ p = types .NewContext ()
348
+ p .Path = "123123"
340
349
a .True (seg .Match (p )).
341
350
Equal (p .Path , "123" ).
342
351
Equal (1 , p .Count ()).
@@ -346,23 +355,26 @@ func TestSegment_Match(t *testing.T) {
346
355
seg , err = i .NewSegment ("{any:any}123" )
347
356
a .NotError (err ).NotNil (seg )
348
357
a .Equal (seg .Type , Interceptor )
349
- p = types .NewContext ("123123" )
358
+ p = types .NewContext ()
359
+ p .Path = "123123"
350
360
a .True (seg .Match (p ))
351
361
a .Empty (p .Path ).
352
362
Equal (1 , p .Count ()).
353
363
Equal (p .MustString ("any" , "not-exists" ), "123" )
354
364
355
365
seg , err = i .NewSegment ("{any:any}123" )
356
366
a .NotError (err ).NotNil (seg )
357
- p = types .NewContext ("12345123" )
367
+ p = types .NewContext ()
368
+ p .Path = "12345123"
358
369
a .True (seg .Match (p )).
359
370
Empty (p .Path ).
360
371
Equal (1 , p .Count ()).
361
372
Equal (p .MustString ("any" , "not-exists" ), "12345" )
362
373
363
374
seg , err = i .NewSegment ("{any:digit}123" )
364
375
a .NotError (err ).NotNil (seg )
365
- p = types .NewContext ("12345123" )
376
+ p = types .NewContext ()
377
+ p .Path = "12345123"
366
378
a .True (seg .Match (p )).
367
379
Empty (p .Path ).
368
380
Equal (1 , p .Count ()).
@@ -371,7 +383,8 @@ func TestSegment_Match(t *testing.T) {
371
383
// Named Endpoint 匹配
372
384
seg , err = i .NewSegment ("{path}" )
373
385
a .NotError (err ).NotNil (seg )
374
- p = types .NewContext ("/posts/author" )
386
+ p = types .NewContext ()
387
+ p .Path = "/posts/author"
375
388
a .True (seg .Match (p )).
376
389
Empty (p .Path ).
377
390
Equal (1 , p .Count ()).
@@ -380,7 +393,8 @@ func TestSegment_Match(t *testing.T) {
380
393
// Named:digit Endpoint 匹配
381
394
seg , err = i .NewSegment ("{id:digit}" )
382
395
a .NotError (err ).NotNil (seg )
383
- p = types .NewContext ("123" )
396
+ p = types .NewContext ()
397
+ p .Path = "123"
384
398
a .True (seg .Match (p ))
385
399
a .Empty (p .Path ).
386
400
Equal (1 , p .Count ()).
@@ -389,7 +403,8 @@ func TestSegment_Match(t *testing.T) {
389
403
// Named:digit Endpoint 不匹配,不会删除传入的参数
390
404
seg , err = i .NewSegment ("{id:digit}" )
391
405
a .NotError (err ).NotNil (seg )
392
- p = types .NewContext ("one" )
406
+ p = types .NewContext ()
407
+ p .Path = "one"
393
408
p .Set ("p1" , "v1" )
394
409
a .False (seg .Match (p )).
395
410
Equal (p .Path , "one" ).
@@ -401,12 +416,14 @@ func TestSegment_Match(t *testing.T) {
401
416
a .NotError (err ).NotNil (seg )
402
417
403
418
// Named:digit 不匹配
404
- p = types .NewContext ("1/aut" )
419
+ p = types .NewContext ()
420
+ p .Path = "1/aut"
405
421
a .False (seg .Match (p )).
406
422
Equal (p .Path , "1/aut" ).Zero (p .Count ())
407
423
408
424
// Named:digit 类型不匹配
409
- p = types .NewContext ("xx/author" )
425
+ p = types .NewContext ()
426
+ p .Path = "xx/author"
410
427
a .False (seg .Match (p )).
411
428
Equal (p .Path , "xx/author" ).
412
429
Zero (p .Count ())
@@ -416,15 +433,17 @@ func TestSegment_Match(t *testing.T) {
416
433
a .NotError (err ).NotNil (seg )
417
434
418
435
// String 匹配
419
- p = types .NewContext ("/posts/author" )
436
+ p = types .NewContext ()
437
+ p .Path = "/posts/author"
420
438
p .Set ("p1" , "v1" )
421
439
a .True (seg .Match (p )).
422
440
Empty (p .Path ).
423
441
Equal (1 , p .Count ()).
424
442
Equal (p .MustString ("p1" , "not-exists" ), "v1" )
425
443
426
444
// String 不匹配
427
- p = types .NewContext ("/posts/author/email" )
445
+ p = types .NewContext ()
446
+ p .Path = "/posts/author/email"
428
447
a .True (seg .Match (p ))
429
448
a .Equal (p .Path , "/email" ).Zero (p .Count ())
430
449
@@ -433,20 +452,23 @@ func TestSegment_Match(t *testing.T) {
433
452
a .NotError (err ).NotNil (seg )
434
453
435
454
// Regexp 完全匹配
436
- p = types .NewContext ("1/author" )
455
+ p = types .NewContext ()
456
+ p .Path = "1/author"
437
457
a .True (seg .Match (p )).
438
458
Empty (p .Path ).
439
459
Equal (1 , p .Count ()).
440
460
Equal (p .MustString ("id" , "not-exists" ), "1" )
441
461
442
462
// Regexp 不匹配
443
- p = types .NewContext ("xxx/author" )
463
+ p = types .NewContext ()
464
+ p .Path = "xxx/author"
444
465
a .False (seg .Match (p )).
445
466
Equal (p .Path , "xxx/author" ).
446
467
Zero (p .Count ())
447
468
448
469
// Regexp 部分匹配
449
- p = types .NewContext ("1/author/email" )
470
+ p = types .NewContext ()
471
+ p .Path = "1/author/email"
450
472
a .True (seg .Match (p )).
451
473
Equal (p .Path , "/email" ).
452
474
Equal (1 , p .Count ()).
0 commit comments