Browse Source

Merge pull request #669 from zanella/invalid_hostname

* invalid_hostname:
  Added check for invalid hostname
pull/6/head
Spencer Gibb 9 years ago
parent
commit
8d259b9d8f
  1. 3
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java
  2. 23
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java

3
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactory.java

@ -48,6 +48,9 @@ public class RibbonClientHttpRequestFactory implements ClientHttpRequestFactory @@ -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);

23
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientHttpRequestFactoryTests.java

@ -16,14 +16,11 @@ @@ -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; @@ -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; @@ -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 { @@ -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

Loading…
Cancel
Save