@@ -472,6 +472,37 @@ func TestEchoRoutes(t *testing.T) {
472472 }
473473}
474474
475+ func TestEchoRoutesHandleHostsProperly (t * testing.T ) {
476+ e := New ()
477+ h := e .Host ("route.com" )
478+ routes := []* Route {
479+ {http .MethodGet , "/users/:user/events" , "" },
480+ {http .MethodGet , "/users/:user/events/public" , "" },
481+ {http .MethodPost , "/repos/:owner/:repo/git/refs" , "" },
482+ {http .MethodPost , "/repos/:owner/:repo/git/tags" , "" },
483+ }
484+ for _ , r := range routes {
485+ h .Add (r .Method , r .Path , func (c Context ) error {
486+ return c .String (http .StatusOK , "OK" )
487+ })
488+ }
489+
490+ if assert .Equal (t , len (routes ), len (e .Routes ())) {
491+ for _ , r := range e .Routes () {
492+ found := false
493+ for _ , rr := range routes {
494+ if r .Method == rr .Method && r .Path == rr .Path {
495+ found = true
496+ break
497+ }
498+ }
499+ if ! found {
500+ t .Errorf ("Route %s %s not found" , r .Method , r .Path )
501+ }
502+ }
503+ }
504+ }
505+
475506func TestEchoServeHTTPPathEncoding (t * testing.T ) {
476507 e := New ()
477508 e .GET ("/with/slash" , func (c Context ) error {
@@ -514,6 +545,109 @@ func TestEchoServeHTTPPathEncoding(t *testing.T) {
514545 }
515546}
516547
548+ func TestEchoHost (t * testing.T ) {
549+ assert := assert .New (t )
550+
551+ okHandler := func (c Context ) error { return c .String (http .StatusOK , http .StatusText (http .StatusOK )) }
552+ teapotHandler := func (c Context ) error { return c .String (http .StatusTeapot , http .StatusText (http .StatusTeapot )) }
553+ acceptHandler := func (c Context ) error { return c .String (http .StatusAccepted , http .StatusText (http .StatusAccepted )) }
554+ teapotMiddleware := MiddlewareFunc (func (next HandlerFunc ) HandlerFunc { return teapotHandler })
555+
556+ e := New ()
557+ e .GET ("/" , acceptHandler )
558+ e .GET ("/foo" , acceptHandler )
559+
560+ ok := e .Host ("ok.com" )
561+ ok .GET ("/" , okHandler )
562+ ok .GET ("/foo" , okHandler )
563+
564+ teapot := e .Host ("teapot.com" )
565+ teapot .GET ("/" , teapotHandler )
566+ teapot .GET ("/foo" , teapotHandler )
567+
568+ middle := e .Host ("middleware.com" , teapotMiddleware )
569+ middle .GET ("/" , okHandler )
570+ middle .GET ("/foo" , okHandler )
571+
572+ var testCases = []struct {
573+ name string
574+ whenHost string
575+ whenPath string
576+ expectBody string
577+ expectStatus int
578+ }{
579+ {
580+ name : "No Host Root" ,
581+ whenHost : "" ,
582+ whenPath : "/" ,
583+ expectBody : http .StatusText (http .StatusAccepted ),
584+ expectStatus : http .StatusAccepted ,
585+ },
586+ {
587+ name : "No Host Foo" ,
588+ whenHost : "" ,
589+ whenPath : "/foo" ,
590+ expectBody : http .StatusText (http .StatusAccepted ),
591+ expectStatus : http .StatusAccepted ,
592+ },
593+ {
594+ name : "OK Host Root" ,
595+ whenHost : "ok.com" ,
596+ whenPath : "/" ,
597+ expectBody : http .StatusText (http .StatusOK ),
598+ expectStatus : http .StatusOK ,
599+ },
600+ {
601+ name : "OK Host Foo" ,
602+ whenHost : "ok.com" ,
603+ whenPath : "/foo" ,
604+ expectBody : http .StatusText (http .StatusOK ),
605+ expectStatus : http .StatusOK ,
606+ },
607+ {
608+ name : "Teapot Host Root" ,
609+ whenHost : "teapot.com" ,
610+ whenPath : "/" ,
611+ expectBody : http .StatusText (http .StatusTeapot ),
612+ expectStatus : http .StatusTeapot ,
613+ },
614+ {
615+ name : "Teapot Host Foo" ,
616+ whenHost : "teapot.com" ,
617+ whenPath : "/foo" ,
618+ expectBody : http .StatusText (http .StatusTeapot ),
619+ expectStatus : http .StatusTeapot ,
620+ },
621+ {
622+ name : "Middleware Host" ,
623+ whenHost : "middleware.com" ,
624+ whenPath : "/" ,
625+ expectBody : http .StatusText (http .StatusTeapot ),
626+ expectStatus : http .StatusTeapot ,
627+ },
628+ {
629+ name : "Middleware Host Foo" ,
630+ whenHost : "middleware.com" ,
631+ whenPath : "/foo" ,
632+ expectBody : http .StatusText (http .StatusTeapot ),
633+ expectStatus : http .StatusTeapot ,
634+ },
635+ }
636+
637+ for _ , tc := range testCases {
638+ t .Run (tc .name , func (t * testing.T ) {
639+ req := httptest .NewRequest (http .MethodGet , tc .whenPath , nil )
640+ req .Host = tc .whenHost
641+ rec := httptest .NewRecorder ()
642+
643+ e .ServeHTTP (rec , req )
644+
645+ assert .Equal (tc .expectStatus , rec .Code )
646+ assert .Equal (tc .expectBody , rec .Body .String ())
647+ })
648+ }
649+ }
650+
517651func TestEchoGroup (t * testing.T ) {
518652 e := New ()
519653 buf := new (bytes.Buffer )
@@ -1166,6 +1300,22 @@ func TestEchoReverse(t *testing.T) {
11661300 assert .Equal ("/params/one/bar/two/three" , e .Reverse ("/params/:foo/bar/:qux/*" , "one" , "two" , "three" ))
11671301}
11681302
1303+ func TestEchoReverseHandleHostProperly (t * testing.T ) {
1304+ assert := assert .New (t )
1305+
1306+ dummyHandler := func (Context ) error { return nil }
1307+
1308+ e := New ()
1309+ h := e .Host ("the_host" )
1310+ h .GET ("/static" , dummyHandler ).Name = "/static"
1311+ h .GET ("/static/*" , dummyHandler ).Name = "/static/*"
1312+
1313+ assert .Equal ("/static" , e .Reverse ("/static" ))
1314+ assert .Equal ("/static" , e .Reverse ("/static" , "missing param" ))
1315+ assert .Equal ("/static/*" , e .Reverse ("/static/*" ))
1316+ assert .Equal ("/static/foo.txt" , e .Reverse ("/static/*" , "foo.txt" ))
1317+ }
1318+
11691319func TestEcho_ListenerAddr (t * testing.T ) {
11701320 e := New ()
11711321
0 commit comments