Merge pull request #669 from zanella/invalid_hostname
* invalid_hostname: Added check for invalid hostname
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user