1414import io .vertx .core .Promise ;
1515import io .vertx .core .buffer .Buffer ;
1616import io .vertx .core .http .*;
17+ import io .vertx .core .net .HostAndPort ;
1718import io .vertx .core .net .NetClient ;
1819import io .vertx .core .net .SocketAddress ;
1920import io .vertx .core .streams .WriteStream ;
2021import io .vertx .ext .unit .Async ;
2122import io .vertx .ext .unit .TestContext ;
23+ import org .junit .Ignore ;
2224import org .junit .Test ;
2325
2426import java .io .Closeable ;
@@ -736,22 +738,62 @@ public void testPropagateHeaders(TestContext ctx) {
736738 }));
737739 }
738740
741+ @ Test
742+ public void testIPV6Authority (TestContext ctx ) {
743+ testAuthority (ctx , HostAndPort .authority ("[7a03:908:671:b520:ba27:bbff:ffff:fed2]" , 1234 ));
744+ }
745+
746+ @ Test
747+ public void testIPV4Authority (TestContext ctx ) {
748+ testAuthority (ctx , HostAndPort .authority ("192.168.0.1" , 1234 ));
749+ }
750+
751+ @ Test
752+ public void testMissingPortAuthority (TestContext ctx ) {
753+ testAuthority (ctx , HostAndPort .authority ("localhost" , -1 ));
754+ }
755+
756+ private void testAuthority (TestContext ctx , HostAndPort requestAuthority ) {
757+ SocketAddress backend = startHttpBackend (ctx , 8081 , req -> {
758+ ctx .assertEquals ("/somepath" , req .uri ());
759+ ctx .assertEquals (requestAuthority .host (), req .authority ().host ());
760+ ctx .assertEquals (requestAuthority .port (), req .authority ().port ());
761+ ctx .assertEquals (null , req .getHeader ("x-forwarded-host" ));
762+ req .response ().end ("Hello World" );
763+ });
764+ startProxy (proxy -> {
765+ proxy .origin (backend );
766+ });
767+ HttpClient client = vertx .createHttpClient ();
768+ client .request (GET , 8080 , "localhost" , "/somepath" )
769+ .compose (req -> req
770+ .authority (requestAuthority )
771+ .send ()
772+ .compose (resp -> {
773+ ctx .assertEquals (200 , resp .statusCode ());
774+ return resp .body ();
775+ }))
776+ .onComplete (ctx .asyncAssertSuccess (body -> {
777+ ctx .assertEquals ("Hello World" , body .toString ());
778+ }));
779+ }
780+
739781 @ Test
740782 public void testAuthorityOverride1 (TestContext ctx ) {
741- testAuthorityOverride (ctx , "foo:8080" , "foo:8080" , "localhost:8080" );
783+ testAuthorityOverride (ctx , HostAndPort . authority ( "foo" , 8080 ) , "foo:8080" , "localhost:8080" );
742784 }
743785
744786 @ Test
745787 public void testAuthorityOverride2 (TestContext ctx ) {
746- testAuthorityOverride (ctx , "foo" , "foo" , "localhost:8080" );
788+ testAuthorityOverride (ctx , HostAndPort . authority ( "foo" ) , "foo" , "localhost:8080" );
747789 }
748790
749791 @ Test
750792 public void testAuthorityOverride3 (TestContext ctx ) {
751- testAuthorityOverride (ctx , "localhost:8080" , "localhost:8080" , null );
793+ testAuthorityOverride (ctx , HostAndPort . authority ( "localhost" , 8080 ) , "localhost:8080" , null );
752794 }
753795
754- private void testAuthorityOverride (TestContext ctx , String authority , String expectedAuthority , String expectedForwardedHost ) {
796+ private void testAuthorityOverride (TestContext ctx , HostAndPort authority , String expectedAuthority , String expectedForwardedHost ) {
755797 SocketAddress backend = startHttpBackend (ctx , 8081 , req -> {
756798 ctx .assertEquals ("/somepath" , req .uri ());
757799 ctx .assertEquals (expectedAuthority , req .authority ().toString ());
@@ -764,7 +806,8 @@ private void testAuthorityOverride(TestContext ctx, String authority, String exp
764806 @ Override
765807 public Future <ProxyResponse > handleProxyRequest (ProxyContext context ) {
766808 ProxyRequest request = context .request ();
767- ctx .assertEquals ("localhost:8080" , request .getAuthority ());
809+ ctx .assertEquals ("localhost" , request .getAuthority ().host ());
810+ ctx .assertEquals (8080 , request .getAuthority ().port ());
768811 request .setAuthority (authority );
769812 return ProxyInterceptor .super .handleProxyRequest (context );
770813 }
0 commit comments