@@ -530,34 +530,71 @@ func TestEchoRoutes(t *testing.T) {
530530 }
531531}
532532
533- func TestEchoRoutesHandleHostsProperly (t * testing.T ) {
533+ func TestEchoRoutesHandleAdditionalHosts (t * testing.T ) {
534534 e := New ()
535- h := e .Host ("route .com" )
535+ domain2Router := e .Host ("domain2.router .com" )
536536 routes := []* Route {
537537 {http .MethodGet , "/users/:user/events" , "" },
538538 {http .MethodGet , "/users/:user/events/public" , "" },
539539 {http .MethodPost , "/repos/:owner/:repo/git/refs" , "" },
540540 {http .MethodPost , "/repos/:owner/:repo/git/tags" , "" },
541541 }
542542 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 {
544544 return c .String (http .StatusOK , "OK" )
545545 })
546546 }
547+ e .Add (http .MethodGet , "/api" , func (c Context ) error {
548+ return c .String (http .StatusOK , "OK" )
549+ })
547550
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
556560 }
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
559593 }
560594 }
595+ if ! found {
596+ t .Errorf ("Route %s %s not found" , r .Method , r .Path )
597+ }
561598 }
562599}
563600
@@ -1468,14 +1505,27 @@ func TestEchoReverseHandleHostProperly(t *testing.T) {
14681505 dummyHandler := func (Context ) error { return nil }
14691506
14701507 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
14711514 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" ))
14741528
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" ))
14791529}
14801530
14811531func TestEcho_ListenerAddr (t * testing.T ) {
0 commit comments