Browse Source

Provide better error handling for invalid hostnames. Fixes #159.

pull/160/head
Ryan Baxter 8 years ago
parent
commit
43f7810080
  1. 2
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerInterceptor.java
  2. 2
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java
  3. 20
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java

2
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerInterceptor.java

@ -23,6 +23,7 @@ import org.springframework.http.HttpRequest; @@ -23,6 +23,7 @@ import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
/**
* @author Spencer Gibb
@ -42,6 +43,7 @@ public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor { @@ -42,6 +43,7 @@ public class LoadBalancerInterceptor implements ClientHttpRequestInterceptor {
final ClientHttpRequestExecution execution) throws IOException {
final URI originalUri = request.getURI();
String serviceName = originalUri.getHost();
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
return this.loadBalancer.execute(serviceName,
new LoadBalancerRequest<ClientHttpResponse>() {
@Override

2
spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java

@ -12,6 +12,7 @@ import org.springframework.retry.RetryCallback; @@ -12,6 +12,7 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.util.Assert;
/**
* @author Ryan Baxter
@ -38,6 +39,7 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto @@ -38,6 +39,7 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto
final ClientHttpRequestExecution execution) throws IOException {
final URI originalUri = request.getURI();
final String serviceName = originalUri.getHost();
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
LoadBalancedRetryPolicy retryPolicy = lbRetryPolicyFactory.create(serviceName,
loadBalancer);
retryTemplate.setRetryPolicy(

20
spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java

@ -16,6 +16,7 @@ import org.springframework.mock.http.client.MockClientHttpResponse; @@ -16,6 +16,7 @@ import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import static org.bouncycastle.crypto.tls.ConnectionEnd.client;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
@ -69,6 +70,25 @@ public class RetryLoadBalancerInterceptorTest { @@ -69,6 +70,25 @@ public class RetryLoadBalancerInterceptorTest {
verify(retryTemplate, times(1)).setRetryPolicy(any(NeverRetryPolicy.class));
}
@Test(expected = IllegalStateException.class)
public void interceptInvalidHost() throws Throwable {
HttpRequest request = mock(HttpRequest.class);
when(request.getURI()).thenReturn(new URI("http://foo_underscore"));
ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK);
LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
InterceptorRetryPolicy interceptorRetryPolicy = new InterceptorRetryPolicy(request, policy, client,"foo");
LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class);
when(lbRetryPolicyFactory.create(eq("foo_underscore"), any(ServiceInstanceChooser.class))).thenReturn(policy);
ServiceInstance serviceInstance = mock(ServiceInstance.class);
when(client.choose(eq("foo_underscore"))).thenReturn(serviceInstance);
when(client.execute(eq("foo_underscore"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
lbProperties.setEnabled(true);
RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, retryTemplate, lbProperties, lbRetryPolicyFactory);
byte[] body = new byte[]{};
ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
}
@Test
public void interceptNeverRetry() throws Throwable {
HttpRequest request = mock(HttpRequest.class);

Loading…
Cancel
Save