@@ -582,6 +582,50 @@ void requestURLIPv6() {
582582 assertEquals ("http://[::1]:8080/path?q=1" , ctx .getRequestURL ());
583583 }
584584
585+ @ Test
586+ void hostAndPortIPv6ServerFallback () {
587+ ServerOptions options = new ServerOptions ().setPort (9090 ).setHost ("::1" );
588+ doReturn (options ).when (ctx ).require (ServerOptions .class );
589+ doReturn (false ).when (ctx ).isSecure ();
590+
591+ Value mockHostHeader = mock (Value .class );
592+ when (mockHostHeader .valueOrNull ()).thenReturn (null );
593+ doReturn (mockHostHeader ).when (ctx ).header ("Host" );
594+
595+ // Without brackets in the fallback, this would fail and return "[::1:9090]" / 80
596+ assertEquals ("[::1]:9090" , ctx .getHostAndPort ());
597+ assertEquals ("[::1]" , ctx .getHost ());
598+ assertEquals (9090 , ctx .getPort ());
599+ }
600+
601+ @ Test
602+ void hostAndPortBareIPv6 () {
603+ doReturn (false ).when (ctx ).isSecure ();
604+
605+ Value mockHostHeader = mock (Value .class );
606+ when (mockHostHeader .valueOrNull ()).thenReturn ("2001:db8::1" );
607+ doReturn (mockHostHeader ).when (ctx ).header ("Host" );
608+
609+ // Ensures getHost() successfully detects and wraps unbracketed IPv6 headers
610+ assertEquals ("2001:db8::1" , ctx .getHostAndPort ());
611+ assertEquals ("[2001:db8::1]" , ctx .getHost ());
612+ assertEquals (80 , ctx .getPort ());
613+ }
614+
615+ @ Test
616+ void hostAndPortIPv6WithProxy () {
617+ when (router .getRouterOptions ()).thenReturn (new RouterOptions ().setTrustProxy (true ));
618+ Value mockProxyHeader = mock (Value .class );
619+
620+ // Multiple proxy hops, first one is IPv6 with a port
621+ when (mockProxyHeader .toOptional ()).thenReturn (Optional .of ("[2001:db8::1]:8443, [::1]" ));
622+ doReturn (mockProxyHeader ).when (ctx ).header ("X-Forwarded-Host" );
623+
624+ assertEquals ("[2001:db8::1]:8443" , ctx .getHostAndPort ());
625+ assertEquals ("[2001:db8::1]" , ctx .getHost ());
626+ assertEquals (8443 , ctx .getPort ());
627+ }
628+
585629 @ Test
586630 void securePortFallback () {
587631 ServerOptions options = new ServerOptions ().setPort (8080 ); // securePort is null
0 commit comments