HTTPCLIENT-2242: RoutingSupport fails to copy InetAddress when normalizing HttpHost

This commit is contained in:
Oleg Kalnichevski 2022-11-06 11:18:15 +01:00
parent fe1e095ef9
commit 16b2cf467d
2 changed files with 23 additions and 1 deletions

View File

@ -73,7 +73,7 @@ public final class RoutingSupport {
if (host.getPort() < 0) {
final int port = (schemePortResolver != null ? schemePortResolver: DefaultSchemePortResolver.INSTANCE).resolve(host);
if (port > 0) {
return new HttpHost(host.getSchemeName(), host.getHostName(), port);
return new HttpHost(host.getSchemeName(), host.getAddress(), host.getHostName(), port);
}
}
return host;

View File

@ -29,8 +29,10 @@ package org.apache.hc.client5.http.impl.routing;
import static org.hamcrest.MatcherAssert.assertThat;
import java.net.InetAddress;
import java.net.URI;
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
import org.apache.hc.client5.http.routing.RoutingSupport;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
@ -62,4 +64,24 @@ public class TestRoutingSupport {
RoutingSupport.determineHost(request1));
}
@Test
public void testNormalizeHost() throws Exception {
Assertions.assertEquals(new HttpHost("http", "somehost", 80),
RoutingSupport.normalize(
new HttpHost("http", "somehost", -1),
DefaultSchemePortResolver.INSTANCE));
Assertions.assertEquals(new HttpHost("https", "somehost", 443),
RoutingSupport.normalize(
new HttpHost("https", "somehost", -1),
DefaultSchemePortResolver.INSTANCE));
Assertions.assertEquals(new HttpHost("http", InetAddress.getLocalHost(), "localhost", 80),
RoutingSupport.normalize(
new HttpHost("http", InetAddress.getLocalHost(), "localhost", -1),
DefaultSchemePortResolver.INSTANCE));
Assertions.assertEquals(new HttpHost("http", "somehost", 8080),
RoutingSupport.normalize(
new HttpHost("http", "somehost", 8080),
DefaultSchemePortResolver.INSTANCE));
}
}