Get content length from Headers when RibbonCommandContext is built + add tests
This commit is contained in:
@@ -108,6 +108,6 @@ public class RibbonHttpRequest extends AbstractClientHttpRequest {
|
||||
}
|
||||
|
||||
private boolean isDynamic(String name) {
|
||||
return name.equalsIgnoreCase("Content-Length") || name.equalsIgnoreCase("Transfer-Encoding");
|
||||
return "Content-Length".equalsIgnoreCase(name) || "Transfer-Encoding".equalsIgnoreCase(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.List;
|
||||
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -54,17 +55,22 @@ public class RibbonCommandContext {
|
||||
private final List<RibbonRequestCustomizer> requestCustomizers;
|
||||
private Long contentLength;
|
||||
|
||||
public RibbonCommandContext(String serviceId, String method, String uri, Boolean retryable,
|
||||
MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
|
||||
InputStream requestEntity) {
|
||||
public RibbonCommandContext(String serviceId, String method, String uri,
|
||||
Boolean retryable, MultiValueMap<String, String> headers,
|
||||
MultiValueMap<String, String> params, InputStream requestEntity) {
|
||||
this(serviceId, method, uri, retryable, headers, params, requestEntity,
|
||||
new ArrayList<RibbonRequestCustomizer>(), null);
|
||||
new ArrayList<RibbonRequestCustomizer>(),
|
||||
headers.containsKey("Content-Length") && StringUtils
|
||||
.hasText(headers.getFirst("Content-Length").toString())
|
||||
? new Long(headers.getFirst("Content-Length").toString())
|
||||
: null);
|
||||
}
|
||||
|
||||
public URI uri() {
|
||||
try {
|
||||
return new URI(this.uri);
|
||||
} catch (URISyntaxException e) {
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
ReflectionUtils.rethrowRuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommand;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.netflix.client.AbstractLoadBalancerAwareClient;
|
||||
import com.netflix.client.ClientRequest;
|
||||
@@ -72,7 +71,7 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
|
||||
.getIntProperty(name, 100);
|
||||
setter.withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get());
|
||||
} else {
|
||||
// FIXME Find out which parameters can be set here
|
||||
// TODO Find out is some parameters can be set here
|
||||
}
|
||||
|
||||
return Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand"))
|
||||
@@ -84,12 +83,6 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
|
||||
@Override
|
||||
protected ClientHttpResponse run() throws Exception {
|
||||
final RequestContext context = RequestContext.getCurrentContext();
|
||||
if (context.getRequest() != null) {
|
||||
String contentLengthHeader = context.getRequest().getHeader("Content-Length");
|
||||
if (StringUtils.hasText(contentLengthHeader)) {
|
||||
this.context.setContentLength(new Long(contentLengthHeader));
|
||||
}
|
||||
}
|
||||
|
||||
RQ request = createRequest();
|
||||
RS response = this.client.executeWithLoadBalancer(request);
|
||||
|
||||
@@ -37,6 +37,7 @@ import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.methods.RequestBuilder;
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.netflix.feign.encoding.HttpEncoding;
|
||||
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -52,6 +53,7 @@ public class RibbonApacheHttpRequestTests {
|
||||
String uri = "http://example.com";
|
||||
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
headers.add("my-header", "my-value");
|
||||
headers.add(HttpEncoding.CONTENT_LENGTH, "5192");
|
||||
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("myparam", "myparamval");
|
||||
RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(new RibbonCommandContext("example", "GET", uri, false,
|
||||
@@ -63,7 +65,9 @@ public class RibbonApacheHttpRequestTests {
|
||||
assertThat("uri is wrong", request.getURI().toString(), startsWith(uri));
|
||||
assertThat("my-header is missing", request.getFirstHeader("my-header"), is(notNullValue()));
|
||||
assertThat("my-header is wrong", request.getFirstHeader("my-header").getValue(), is(equalTo("my-value")));
|
||||
assertThat("Content-Length is wrong", request.getFirstHeader(HttpEncoding.CONTENT_LENGTH).getValue(), is(equalTo("5192")));
|
||||
assertThat("myparam is missing", request.getURI().getQuery(), is(equalTo("myparam=myparamval")));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.netflix.feign.encoding.HttpEncoding;
|
||||
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -47,6 +48,7 @@ public class OkHttpRibbonRequestTests {
|
||||
String uri = "http://example.com";
|
||||
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
headers.add("my-header", "my-value");
|
||||
headers.add(HttpEncoding.CONTENT_LENGTH, "5192");
|
||||
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
||||
params.add("myparam", "myparamval");
|
||||
RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri, false, headers, params, null);
|
||||
@@ -57,6 +59,7 @@ public class OkHttpRibbonRequestTests {
|
||||
assertThat("body is not null", request.body(), is(nullValue()));
|
||||
assertThat("uri is wrong", request.url().toString(), startsWith(uri));
|
||||
assertThat("my-header is wrong", request.header("my-header"), is(equalTo("my-value")));
|
||||
assertThat("Content-Length is wrong", request.header(HttpEncoding.CONTENT_LENGTH), is(equalTo("5192")));
|
||||
assertThat("myparam is missing", request.url().queryParameter("myparam"), is(equalTo("myparamval")));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user