Skip to content

Commit 16b2cf4

Browse files
committed
HTTPCLIENT-2242: RoutingSupport fails to copy InetAddress when normalizing HttpHost
1 parent fe1e095 commit 16b2cf4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/routing/RoutingSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static HttpHost normalize(final HttpHost host, final SchemePortResolver s
7373
if (host.getPort() < 0) {
7474
final int port = (schemePortResolver != null ? schemePortResolver: DefaultSchemePortResolver.INSTANCE).resolve(host);
7575
if (port > 0) {
76-
return new HttpHost(host.getSchemeName(), host.getHostName(), port);
76+
return new HttpHost(host.getSchemeName(), host.getAddress(), host.getHostName(), port);
7777
}
7878
}
7979
return host;

httpclient5/src/test/java/org/apache/hc/client5/http/impl/routing/TestRoutingSupport.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929

3030
import static org.hamcrest.MatcherAssert.assertThat;
3131

32+
import java.net.InetAddress;
3233
import java.net.URI;
3334

35+
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
3436
import org.apache.hc.client5.http.routing.RoutingSupport;
3537
import org.apache.hc.core5.http.HttpHost;
3638
import org.apache.hc.core5.http.HttpRequest;
@@ -62,4 +64,24 @@ public void testDetermineHostMissingScheme() throws Exception {
6264
RoutingSupport.determineHost(request1));
6365
}
6466

67+
@Test
68+
public void testNormalizeHost() throws Exception {
69+
Assertions.assertEquals(new HttpHost("http", "somehost", 80),
70+
RoutingSupport.normalize(
71+
new HttpHost("http", "somehost", -1),
72+
DefaultSchemePortResolver.INSTANCE));
73+
Assertions.assertEquals(new HttpHost("https", "somehost", 443),
74+
RoutingSupport.normalize(
75+
new HttpHost("https", "somehost", -1),
76+
DefaultSchemePortResolver.INSTANCE));
77+
Assertions.assertEquals(new HttpHost("http", InetAddress.getLocalHost(), "localhost", 80),
78+
RoutingSupport.normalize(
79+
new HttpHost("http", InetAddress.getLocalHost(), "localhost", -1),
80+
DefaultSchemePortResolver.INSTANCE));
81+
Assertions.assertEquals(new HttpHost("http", "somehost", 8080),
82+
RoutingSupport.normalize(
83+
new HttpHost("http", "somehost", 8080),
84+
DefaultSchemePortResolver.INSTANCE));
85+
}
86+
6587
}

0 commit comments

Comments
 (0)