@@ -530,34 +530,71 @@ func TestEchoRoutes(t *testing.T) {
530
530
}
531
531
}
532
532
533
- func TestEchoRoutesHandleHostsProperly (t * testing.T ) {
533
+ func TestEchoRoutesHandleAdditionalHosts (t * testing.T ) {
534
534
e := New ()
535
- h := e .Host ("route .com" )
535
+ domain2Router := e .Host ("domain2.router .com" )
536
536
routes := []* Route {
537
537
{http .MethodGet , "/users/:user/events" , "" },
538
538
{http .MethodGet , "/users/:user/events/public" , "" },
539
539
{http .MethodPost , "/repos/:owner/:repo/git/refs" , "" },
540
540
{http .MethodPost , "/repos/:owner/:repo/git/tags" , "" },
541
541
}
542
542
for _ , r := range routes {
543
- h .Add (r .Method , r .Path , func (c Context ) error {
543
+ domain2Router .Add (r .Method , r .Path , func (c Context ) error {
544
544
return c .String (http .StatusOK , "OK" )
545
545
})
546
546
}
547
+ e .Add (http .MethodGet , "/api" , func (c Context ) error {
548
+ return c .String (http .StatusOK , "OK" )
549
+ })
547
550
548
- if assert .Equal (t , len (routes ), len (e .Routes ())) {
549
- for _ , r := range e .Routes () {
550
- found := false
551
- for _ , rr := range routes {
552
- if r .Method == rr .Method && r .Path == rr .Path {
553
- found = true
554
- break
555
- }
551
+ domain2Routes := e .Routers ()["domain2.router.com" ].Routes ()
552
+
553
+ assert .Len (t , domain2Routes , len (routes ))
554
+ for _ , r := range domain2Routes {
555
+ found := false
556
+ for _ , rr := range routes {
557
+ if r .Method == rr .Method && r .Path == rr .Path {
558
+ found = true
559
+ break
556
560
}
557
- if ! found {
558
- t .Errorf ("Route %s %s not found" , r .Method , r .Path )
561
+ }
562
+ if ! found {
563
+ t .Errorf ("Route %s %s not found" , r .Method , r .Path )
564
+ }
565
+ }
566
+ }
567
+
568
+ func TestEchoRoutesHandleDefaultHost (t * testing.T ) {
569
+ e := New ()
570
+ routes := []* Route {
571
+ {http .MethodGet , "/users/:user/events" , "" },
572
+ {http .MethodGet , "/users/:user/events/public" , "" },
573
+ {http .MethodPost , "/repos/:owner/:repo/git/refs" , "" },
574
+ {http .MethodPost , "/repos/:owner/:repo/git/tags" , "" },
575
+ }
576
+ for _ , r := range routes {
577
+ e .Add (r .Method , r .Path , func (c Context ) error {
578
+ return c .String (http .StatusOK , "OK" )
579
+ })
580
+ }
581
+ e .Host ("subdomain.mysite.site" ).Add (http .MethodGet , "/api" , func (c Context ) error {
582
+ return c .String (http .StatusOK , "OK" )
583
+ })
584
+
585
+ defaultRouterRoutes := e .Routes ()
586
+ assert .Len (t , defaultRouterRoutes , len (routes ))
587
+ for _ , r := range defaultRouterRoutes {
588
+ found := false
589
+ for _ , rr := range routes {
590
+ if r .Method == rr .Method && r .Path == rr .Path {
591
+ found = true
592
+ break
559
593
}
560
594
}
595
+ if ! found {
596
+ t .Errorf ("Route %s %s not found" , r .Method , r .Path )
597
+ }
561
598
}
562
599
}
563
600
@@ -1468,14 +1505,27 @@ func TestEchoReverseHandleHostProperly(t *testing.T) {
1468
1505
dummyHandler := func (Context ) error { return nil }
1469
1506
1470
1507
e := New ()
1508
+
1509
+ // routes added to the default router are different form different hosts
1510
+ e .GET ("/static" , dummyHandler ).Name = "default-host /static"
1511
+ e .GET ("/static/*" , dummyHandler ).Name = "xxx"
1512
+
1513
+ // different host
1471
1514
h := e .Host ("the_host" )
1472
- h .GET ("/static" , dummyHandler ).Name = "/static"
1473
- h .GET ("/static/*" , dummyHandler ).Name = "/static/*"
1515
+ h .GET ("/static" , dummyHandler ).Name = "host2 /static"
1516
+ h .GET ("/static/v2/*" , dummyHandler ).Name = "xxx"
1517
+
1518
+ assert .Equal (t , "/static" , e .Reverse ("default-host /static" ))
1519
+ // when actual route does not have params and we provide some to Reverse we should get that route url back
1520
+ assert .Equal (t , "/static" , e .Reverse ("default-host /static" , "missing param" ))
1521
+
1522
+ host2Router := e .Routers ()["the_host" ]
1523
+ assert .Equal (t , "/static" , host2Router .Reverse ("host2 /static" ))
1524
+ assert .Equal (t , "/static" , host2Router .Reverse ("host2 /static" , "missing param" ))
1525
+
1526
+ assert .Equal (t , "/static/v2/*" , host2Router .Reverse ("xxx" ))
1527
+ assert .Equal (t , "/static/v2/foo.txt" , host2Router .Reverse ("xxx" , "foo.txt" ))
1474
1528
1475
- assert .Equal (t , "/static" , e .Reverse ("/static" ))
1476
- assert .Equal (t , "/static" , e .Reverse ("/static" , "missing param" ))
1477
- assert .Equal (t , "/static/*" , e .Reverse ("/static/*" ))
1478
- assert .Equal (t , "/static/foo.txt" , e .Reverse ("/static/*" , "foo.txt" ))
1479
1529
}
1480
1530
1481
1531
func TestEcho_ListenerAddr (t * testing.T ) {
0 commit comments