@@ -788,38 +788,35 @@ public function testMatchWithHosts() : void
788788 new Route ('qux ' , '/ping ' , ['GET ' ], $ requestHandler ),
789789 ];
790790
791- $ routes [0 ]->setHost ('foo.host ' );
792- $ routes [1 ]->setHost ('bar.host ' );
793- $ routes [2 ]->setHost ('baz.host ' );
791+ $ routes [0 ]->setHost ('foo ' );
792+ $ routes [1 ]->setHost ('bar ' );
793+ $ routes [2 ]->setHost ('baz ' );
794794
795795 $ router = new Router ();
796- $ router ->addHost ('baz.host ' , 'example.com ' );
796+ $ router ->addHost ('foo ' , 'foo.net ' );
797+ $ router ->addHost ('bar ' , 'bar.net ' );
798+ $ router ->addHost ('baz ' , 'baz.net ' );
797799 $ router ->addRoute (...$ routes );
798800
801+ // hosted route
799802 $ foundRoute = $ router ->match ((new ServerRequestFactory )
800- ->createServerRequest ('GET ' , 'http://foo.host /ping ' ));
803+ ->createServerRequest ('GET ' , 'http://foo.net /ping ' ));
801804 $ this ->assertSame ($ routes [0 ]->getName (), $ foundRoute ->getName ());
802805
806+ // hosted route
803807 $ foundRoute = $ router ->match ((new ServerRequestFactory )
804- ->createServerRequest ('GET ' , 'http://bar.host /ping ' ));
808+ ->createServerRequest ('GET ' , 'http://bar.net /ping ' ));
805809 $ this ->assertSame ($ routes [1 ]->getName (), $ foundRoute ->getName ());
806810
811+ // hosted route
807812 $ foundRoute = $ router ->match ((new ServerRequestFactory )
808- ->createServerRequest ('GET ' , 'http://baz.host/ping ' ));
809- $ this ->assertSame ($ routes [2 ]->getName (), $ foundRoute ->getName ());
810-
811- $ foundRoute = $ router ->match ((new ServerRequestFactory )
812- ->createServerRequest ('GET ' , 'http://example.com/ping ' ));
813+ ->createServerRequest ('GET ' , 'http://baz.net/ping ' ));
813814 $ this ->assertSame ($ routes [2 ]->getName (), $ foundRoute ->getName ());
814815
816+ // non-hosted route
815817 $ foundRoute = $ router ->match ((new ServerRequestFactory )
816818 ->createServerRequest ('GET ' , 'http://localhost/ping ' ));
817819 $ this ->assertSame ($ routes [3 ]->getName (), $ foundRoute ->getName ());
818-
819- $ routes [3 ]->setHost ('qux.host ' );
820- $ this ->expectException (RouteNotFoundException::class);
821- $ router ->match ((new ServerRequestFactory )
822- ->createServerRequest ('GET ' , 'http://localhost/ping ' ));
823820 }
824821
825822 /**
@@ -891,4 +888,81 @@ public function testRouteEventOverrideRequest() : void
891888 $ router ->setEventDispatcher ($ eventDispatcher );
892889 $ router ->handle ($ request );
893890 }
891+
892+ /**
893+ * @return void
894+ */
895+ public function testResolveHost () : void
896+ {
897+ $ router = new Router ();
898+ $ router ->addHost ('foo ' , 'www1.foo.com ' , 'www2.foo.com ' );
899+ $ router ->addHost ('bar ' , 'www1.bar.com ' , 'www2.bar.com ' );
900+
901+ $ this ->assertSame ('foo ' , $ router ->resolveHostname ('www1.foo.com ' ));
902+ $ this ->assertSame ('foo ' , $ router ->resolveHostname ('www2.foo.com ' ));
903+ $ this ->assertSame ('bar ' , $ router ->resolveHostname ('www1.bar.com ' ));
904+ $ this ->assertSame ('bar ' , $ router ->resolveHostname ('www2.bar.com ' ));
905+ $ this ->assertNull ($ router ->resolveHostname ('example.com ' ));
906+ }
907+
908+ /**
909+ * @return void
910+ */
911+ public function testGetRoutesByHostname () : void
912+ {
913+ $ router = new Router ();
914+ $ router ->addHost ('foo ' , 'www1.foo.com ' , 'www2.foo.com ' );
915+ $ router ->addHost ('bar ' , 'www1.bar.com ' , 'www2.bar.com ' );
916+
917+ $ routes = [
918+ new Fixtures \Route (),
919+ new Fixtures \Route (),
920+ new Fixtures \Route (),
921+ new Fixtures \Route (),
922+ new Fixtures \Route (),
923+ new Fixtures \Route (),
924+ ];
925+
926+ $ routes [0 ]->setHost ('foo ' );
927+ $ routes [2 ]->setHost ('bar ' );
928+ $ routes [4 ]->setHost ('bar ' );
929+
930+ $ router ->addRoute (...$ routes );
931+
932+ $ this ->assertSame ([
933+ $ routes [0 ],
934+ $ routes [1 ],
935+ $ routes [3 ],
936+ $ routes [5 ],
937+ ], $ router ->getRoutesByHostname ('www1.foo.com ' ));
938+
939+ $ this ->assertSame ([
940+ $ routes [0 ],
941+ $ routes [1 ],
942+ $ routes [3 ],
943+ $ routes [5 ],
944+ ], $ router ->getRoutesByHostname ('www2.foo.com ' ));
945+
946+ $ this ->assertSame ([
947+ $ routes [1 ],
948+ $ routes [2 ],
949+ $ routes [3 ],
950+ $ routes [4 ],
951+ $ routes [5 ],
952+ ], $ router ->getRoutesByHostname ('www1.bar.com ' ));
953+
954+ $ this ->assertSame ([
955+ $ routes [1 ],
956+ $ routes [2 ],
957+ $ routes [3 ],
958+ $ routes [4 ],
959+ $ routes [5 ],
960+ ], $ router ->getRoutesByHostname ('www2.bar.com ' ));
961+
962+ $ this ->assertSame ([
963+ $ routes [1 ],
964+ $ routes [3 ],
965+ $ routes [5 ],
966+ ], $ router ->getRoutesByHostname ('localhost ' ));
967+ }
894968}
0 commit comments