diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java index b2fc5f88..c1e95a81 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java @@ -48,6 +48,9 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory public ClientHttpRequest createRequest(URI originalUri, HttpMethod httpMethod) throws IOException { String serviceId = originalUri.getHost(); + if (serviceId == null) { + throw new IOException("Invalid hostname in the URI [" + originalUri.toASCIIString() + "]"); + } ServiceInstance instance = loadBalancer.choose(serviceId); if (instance == null) { throw new IllegalStateException("No instances available for "+serviceId); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java index ad39d398..bdf17ea2 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java @@ -16,14 +16,11 @@ package org.springframework.cloud.netflix.ribbon; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import java.net.URI; -import lombok.SneakyThrows; - +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -44,11 +41,17 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; +import lombok.SneakyThrows; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * @author Spencer Gibb */ @@ -59,6 +62,9 @@ import com.netflix.loadbalancer.ServerList; @DirtiesContext public class RibbonClientHttpRequestFactoryTests { + @Rule + public final ExpectedException exceptionRule = ExpectedException.none(); + @Autowired private RestTemplate restTemplate; @@ -123,6 +129,13 @@ public class RibbonClientHttpRequestFactoryTests { assertEquals("wrong response body", "hello world", response.getBody()); } + @Test + public void invalidHostNameError() { + exceptionRule.expect(ResourceAccessException.class); + exceptionRule.expectMessage("Invalid hostname"); + restTemplate.getForEntity("http://simple_bad", String.class); + } + @Configuration @EnableAutoConfiguration @RestController