Commit 7c7513ee by Rafael Zanella Committed by Spencer Gibb

Added check for invalid hostname

The exception class has to be "fixed" You may register a serviceId with invalid URI characters (e.g.: '_'), but the URI::getHost() fails when such chars are found.
parent 4ffeff20
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment