Search in sources :

Example 41 with RibbonCommandContext

use of org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext in project spring-cloud-netflix by spring-cloud.

the class RibbonApacheHttpRequestTests method testEntity.

void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException {
    String lengthString = String.valueOf(entityValue.length());
    Long length = null;
    URI uri = URI.create("http://example.com");
    LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
    if (addContentLengthHeader) {
        headers.add("Content-Length", lengthString);
        length = (long) entityValue.length();
    }
    RibbonRequestCustomizer requestCustomizer = new RibbonRequestCustomizer<RequestBuilder>() {

        @Override
        public boolean accepts(Class builderClass) {
            return builderClass == RequestBuilder.class;
        }

        @Override
        public void customize(RequestBuilder builder) {
            builder.addHeader("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(context);
    HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build());
    assertThat("request is wrong type", request, is(instanceOf(HttpEntityEnclosingRequest.class)));
    assertThat("uri is wrong", request.getURI().toString(), startsWith(uri.toString()));
    if (addContentLengthHeader) {
        assertThat("Content-Length is missing", request.getFirstHeader("Content-Length"), is(notNullValue()));
        assertThat("Content-Length is wrong", request.getFirstHeader("Content-Length").getValue(), is(equalTo(lengthString)));
    }
    assertThat("from-customizer is missing", request.getFirstHeader("from-customizer"), is(notNullValue()));
    assertThat("from-customizer is wrong", request.getFirstHeader("from-customizer").getValue(), is(equalTo("foo")));
    HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
    assertThat("entity is missing", entityRequest.getEntity(), is(notNullValue()));
    HttpEntity entity = entityRequest.getEntity();
    assertThat("contentLength is wrong", entity.getContentLength(), is(equalTo((long) entityValue.length())));
    assertThat("content is missing", entity.getContent(), is(notNullValue()));
    String string = StreamUtils.copyToString(entity.getContent(), Charset.forName("UTF-8"));
    assertThat("content is wrong", string, is(equalTo(entityValue)));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) RequestBuilder(org.apache.http.client.methods.RequestBuilder) HttpEntity(org.apache.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) URI(java.net.URI) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest)

Example 42 with RibbonCommandContext

use of org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testDoubleEncodingWithRetry.

@Test
public void testDoubleEncodingWithRetry() throws Exception {
    int retriesNextServer = 0;
    int retriesSameServer = 0;
    boolean retryable = true;
    boolean retryOnAllOps = true;
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    final URI uri = new URI("https://" + host + ":" + port + "/a%20b");
    RibbonCommandContext context = new RibbonCommandContext(serviceName, method.toString(), uri.toString(), true, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new ByteArrayInputStream(new String("bar").getBytes()), new ArrayList<RibbonRequestCustomizer>());
    RibbonApacheHttpRequest request = new RibbonApacheHttpRequest(context);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    doReturn(200).when(statusLine).getStatusCode();
    doReturn(statusLine).when(response).getStatusLine();
    doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
    ILoadBalancer lb = mock(ILoadBalancer.class);
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", null, true);
    client.execute(request, null);
    verify(response, times(0)).close();
    verify(delegate, times(1)).execute(argThat(new ArgumentMatcher<HttpUriRequest>() {

        @Override
        public boolean matches(HttpUriRequest argument) {
            if (argument instanceof HttpUriRequest) {
                HttpUriRequest arg = (HttpUriRequest) argument;
                return arg.getURI().equals(uri);
            }
            return false;
        }
    }));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ArgumentMatcher(org.mockito.ArgumentMatcher) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 43 with RibbonCommandContext

use of org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext in project spring-cloud-netflix by spring-cloud.

the class RibbonRoutingFilter method buildCommandContext.

protected RibbonCommandContext buildCommandContext(RequestContext context) {
    HttpServletRequest request = context.getRequest();
    MultiValueMap<String, String> headers = this.helper.buildZuulRequestHeaders(request);
    MultiValueMap<String, String> params = this.helper.buildZuulRequestQueryParams(request);
    String verb = getVerb(request);
    InputStream requestEntity = getRequestBody(request);
    if (request.getContentLength() < 0 && !verb.equalsIgnoreCase("GET")) {
        context.setChunkedRequestBody();
    }
    String serviceId = (String) context.get(SERVICE_ID_KEY);
    Boolean retryable = (Boolean) context.get(RETRYABLE_KEY);
    Object loadBalancerKey = context.get(LOAD_BALANCER_KEY);
    String uri = this.helper.buildZuulRequestURI(request);
    // remove double slashes
    uri = uri.replace("//", "/");
    long contentLength = useServlet31 ? request.getContentLengthLong() : request.getContentLength();
    return new RibbonCommandContext(serviceId, verb, uri, retryable, headers, params, requestEntity, this.requestCustomizers, contentLength, loadBalancerKey);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) InputStream(java.io.InputStream)

Aggregations

RibbonCommandContext (org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext)43 Test (org.junit.Test)38 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)21 IClientConfig (com.netflix.client.config.IClientConfig)20 SpringClientFactory (org.springframework.cloud.netflix.ribbon.SpringClientFactory)20 ZuulProperties (org.springframework.cloud.netflix.zuul.filters.ZuulProperties)20 FallbackProvider (org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider)20 RibbonLoadBalancingHttpClient (org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient)11 OkHttpLoadBalancingClient (org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient)11 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)7 RibbonRequestCustomizer (org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer)6 URI (java.net.URI)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 HttpRequest (com.netflix.client.http.HttpRequest)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Request (okhttp3.Request)2 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)2