Search in sources :

Example 1 with RibbonRequestCustomizer

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

the class RibbonLoadBalancingHttpClientTests method testDoubleEncoding.

@Test
public void testDoubleEncoding() throws Exception {
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    final URI uri = new URI("https://" + host + ":" + port + "/a%2Bb");
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.setClientName(serviceName);
    ServerIntrospector introspector = mock(ServerIntrospector.class);
    RibbonCommandContext context = new RibbonCommandContext(serviceName, method.toString(), uri.toString(), false, new LinkedMultiValueMap<String, String>(), new LinkedMultiValueMap<String, String>(), new ByteArrayInputStream("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));
    RibbonLoadBalancingHttpClient client = new RibbonLoadBalancingHttpClient(delegate, clientConfig, introspector);
    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) ServerIntrospector(org.springframework.cloud.netflix.ribbon.ServerIntrospector) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) ArgumentMatcher(org.mockito.ArgumentMatcher) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl) Test(org.junit.Test)

Example 2 with RibbonRequestCustomizer

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

the class OkHttpRibbonRequestTests method testNullEntity.

@Test
public void testNullEntity() throws Exception {
    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, new ArrayList<RibbonRequestCustomizer>());
    OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
    Request request = httpRequest.toRequest();
    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("myparam is missing", request.url().queryParameter("myparam"), is(equalTo("myparamval")));
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) Request(okhttp3.Request) Test(org.junit.Test)

Example 3 with RibbonRequestCustomizer

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

the class OkHttpRibbonRequestTests method testEntity.

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

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

        @Override
        public void customize(Request.Builder builder) {
            builder.addHeader("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri, false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
    Request request = httpRequest.toRequest();
    assertThat("uri is wrong", request.url().toString(), startsWith(uri));
    if (addContentLengthHeader) {
        assertThat("Content-Length is wrong", request.header("Content-Length"), is(equalTo(lengthString)));
    }
    assertThat("from-customizer is wrong", request.header("from-customizer"), is(equalTo("foo")));
    if (!method.equalsIgnoreCase("get")) {
        assertThat("body is null", request.body(), is(notNullValue()));
        RequestBody body = request.body();
        assertThat("contentLength is wrong", body.contentLength(), is(equalTo((long) entityValue.length())));
        Buffer content = new Buffer();
        body.writeTo(content);
        String string = content.readByteString().utf8();
        assertThat("content is wrong", string, is(equalTo(entityValue)));
    }
}
Also used : Buffer(okio.Buffer) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Example 4 with RibbonRequestCustomizer

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

the class RestClientRibbonCommandTests method testEntity.

void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws Exception {
    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<HttpRequest.Builder>() {

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

        @Override
        public void customize(HttpRequest.Builder builder) {
            builder.header("from-customizer", "foo");
        }
    };
    RibbonCommandContext context = new RibbonCommandContext("example", method, uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
    context.setContentLength(length);
    RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, context, zuulProperties);
    HttpRequest request = command.createRequest();
    assertThat("uri is wrong", request.getUri().toString(), startsWith(uri.toString()));
    if (addContentLengthHeader) {
        assertThat("Content-Length is wrong", request.getHttpHeaders().getFirstValue("Content-Length"), is(equalTo(lengthString)));
    }
    assertThat("from-customizer is wrong", request.getHttpHeaders().getFirstValue("from-customizer"), is(equalTo("foo")));
    if (method.equalsIgnoreCase("DELETE")) {
        assertThat("entity is was non-null", request.getEntity(), is(nullValue()));
    } else {
        assertThat("entity is missing", request.getEntity(), is(notNullValue()));
        assertThat("entity is wrong type", InputStream.class.isAssignableFrom(request.getEntity().getClass()), is(true));
        InputStream entity = (InputStream) request.getEntity();
        String string = StreamUtils.copyToString(entity, Charset.forName("UTF-8"));
        assertThat("content is wrong", string, is(equalTo(entityValue)));
    }
}
Also used : HttpRequest(com.netflix.client.http.HttpRequest) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RibbonCommandContext(org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RibbonRequestCustomizer(org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer) URI(java.net.URI)

Example 5 with RibbonRequestCustomizer

use of org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer 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)

Aggregations

RibbonCommandContext (org.springframework.cloud.netflix.ribbon.support.RibbonCommandContext)6 RibbonRequestCustomizer (org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer)6 URI (java.net.URI)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)3 Test (org.junit.Test)3 Request (okhttp3.Request)2 StatusLine (org.apache.http.StatusLine)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 HttpMethod (org.springframework.http.HttpMethod)2 DefaultClientConfigImpl (com.netflix.client.config.DefaultClientConfigImpl)1 HttpRequest (com.netflix.client.http.HttpRequest)1 ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)1 InputStream (java.io.InputStream)1 RequestBody (okhttp3.RequestBody)1 Buffer (okio.Buffer)1