@@ -37,6 +37,7 @@ private Context setupBaseContext() {
3737 when (ctx .getRemoteAddress ()).thenReturn ("127.0.0.1" );
3838 when (ctx .getScheme ()).thenReturn ("http" );
3939 when (ctx .getHost ()).thenReturn ("localhost" );
40+ when (ctx .getHostAndPort ()).thenReturn ("localhost" );
4041 when (ctx .getServerPort ()).thenReturn (8080 );
4142 when (ctx .isSecure ()).thenReturn (false );
4243
@@ -90,6 +91,72 @@ void testParseIPv4WithPort() {
9091 assertEquals (9090 , peer .getPort ());
9192 }
9293
94+ @ Test
95+ @ DisplayName ("Test Host header port is kept when X-Forwarded-Host is missing" )
96+ void testParseHostHeaderWithPort () {
97+ Context ctx = setupBaseContext ();
98+ when (ctx .getHost ()).thenReturn ("api.jooby.io" );
99+ when (ctx .getHostAndPort ()).thenReturn ("api.jooby.io:9090" );
100+ mockHeader (ctx , "X-Forwarded-Proto" , "https" );
101+
102+ ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
103+
104+ assertEquals ("api.jooby.io" , peer .getHost ());
105+ assertEquals (9090 , peer .getPort ());
106+ }
107+
108+ @ Test
109+ @ DisplayName ("Test localhost Host header port wins over the server port" )
110+ void testParseLocalhostHostHeaderWithPort () {
111+ Context ctx = setupBaseContext ();
112+ when (ctx .getHostAndPort ()).thenReturn ("localhost:9090" );
113+
114+ ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
115+
116+ assertEquals ("localhost" , peer .getHost ());
117+ assertEquals (9090 , peer .getPort ());
118+ }
119+
120+ @ Test
121+ @ DisplayName ("Test IPv6 Host header port is kept when X-Forwarded-Host is missing" )
122+ void testParseIPv6HostHeaderWithPort () {
123+ Context ctx = setupBaseContext ();
124+ when (ctx .getHost ()).thenReturn ("[2001:db8::1]" );
125+ when (ctx .getHostAndPort ()).thenReturn ("[2001:db8::1]:9090" );
126+
127+ ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
128+
129+ assertEquals ("[2001:db8::1]" , peer .getHost ());
130+ assertEquals (9090 , peer .getPort ());
131+ }
132+
133+ @ Test
134+ @ DisplayName ("Test IPv6 Host header without port when X-Forwarded-Host is missing" )
135+ void testParseIPv6HostHeaderNoPort () {
136+ Context ctx = setupBaseContext ();
137+ // getHostAndPort() drops the brackets of an IPv6 address that has no port
138+ when (ctx .getHost ()).thenReturn ("[2001:db8::1]" );
139+ when (ctx .getHostAndPort ()).thenReturn ("2001:db8::1" );
140+
141+ ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
142+
143+ assertEquals ("[2001:db8::1]" , peer .getHost ());
144+ assertEquals (80 , peer .getPort ());
145+ }
146+
147+ @ Test
148+ @ DisplayName ("Test default port follows X-Forwarded-Proto" )
149+ void testDefaultPortFollowsForwardedProto () {
150+ Context ctx = setupBaseContext ();
151+ mockHeader (ctx , "X-Forwarded-Proto" , "https" );
152+ mockHeader (ctx , "X-Forwarded-Host" , "jooby.io" );
153+
154+ ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
155+
156+ assertEquals ("https" , peer .getScheme ());
157+ assertEquals (443 , peer .getPort ());
158+ }
159+
93160 @ Test
94161 @ DisplayName ("Test X-Forwarded-Port taking precedence over Host port" )
95162 void testPortPrecedence () {
@@ -108,7 +175,7 @@ void testPortPrecedence() {
108175 void testParseIPv6NoPort () {
109176 Context ctx = setupBaseContext ();
110177 mockHeader (ctx , "X-Forwarded-Host" , "[2001:db8::1]" );
111- when (ctx .isSecure ()).thenReturn (true );
178+ when (ctx .getScheme ()).thenReturn ("https" );
112179
113180 ProxyPeerAddress peer = ProxyPeerAddress .parse (ctx );
114181
0 commit comments