Search in sources :

Example 36 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestQueryTunnel method testTunneledHandConstructedOverride.

@Test
public void testTunneledHandConstructedOverride() throws Exception {
    // Example of a hand-constructed "encoded" request
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279")).setMethod("POST").setHeader("X-HTTP-Method-Override", "GET").setHeader("Content-Type", "application/x-www-form-urlencoded").setEntity(new String("q=123").getBytes()).build();
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(request, requestContext);
    Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?q=123");
    Assert.assertEquals(decoded.getMethod(), "GET");
    Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 37 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestQueryTunnel method testForceQueryTunnelFlagSet.

@Test
public void testForceQueryTunnelFlagSet() throws Exception {
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?q=one&x=10&y=15")).setMethod("GET").setEntity(new String("{\name\":\"value\"}").getBytes()).setHeader("Content-Type", "application/json").build();
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.FORCE_QUERY_TUNNEL, true);
    // should encode
    RestRequest encoded = encode(request, requestContext, Integer.MAX_VALUE);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() > 0);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 38 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestQueryTunnel method testNestedMultiPartBody.

@Test
public void testNestedMultiPartBody() throws Exception {
    // Construct a request with multi-part entity as a body and make sure it can be tunneled
    // The entity will be nested - the original multi-part body will become the 2nd part of the tunneled
    // body with the query passed as the form-encoded url
    final MimeMultipart multi = new MimeMultipart("mixed");
    MimeBodyPart partOne = new MimeBodyPart();
    partOne.setContent(new String("{\"name\":\"value\"}").getBytes(), "application/json");
    partOne.setHeader("Content-Type", "application/json");
    // Encode query params as form-urlencoded
    MimeBodyPart partTwo = new MimeBodyPart();
    partTwo.setContent("x=y&z=w&q=10", "application/x-www-form-urlencoded");
    partTwo.setHeader("Content-Type", "application /x-www-form-urlencoded");
    multi.addBodyPart(partTwo);
    multi.addBodyPart(partOne);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    multi.writeTo(os);
    // Create request with multi-part body and query args
    final RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?args=xyz")).setMethod("PUT").setHeader("Content-Type", multi.getContentType()).setEntity(ByteString.copy(os.toByteArray())).build();
    // Encode and verify
    RestRequest encoded = encode(request, 0);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() > 0);
    Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
    Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
    // Decode and make sure we have the original request back
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(encoded, requestContext);
    Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?args=xyz");
    Assert.assertEquals(decoded.getMethod(), "PUT");
    Assert.assertEquals(decoded.getEntity(), request.getEntity());
    Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
    Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
    Assert.assertEquals(decoded.getHeader("Content-Length"), Integer.toString(request.getEntity().length()));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) MimeMultipart(javax.mail.internet.MimeMultipart) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RequestContext(com.linkedin.r2.message.RequestContext) MimeBodyPart(javax.mail.internet.MimeBodyPart) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 39 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestServerRetryFilter method testRetryFilter.

@Test
public void testRetryFilter() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    FilterUtil.fireRestError(filterChain, new RestException(null, new RetriableRequestException(retryMessage)), new HashMap<String, String>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 40 with RequestContext

use of com.linkedin.r2.message.RequestContext in project rest.li by linkedin.

the class TestClientRetryFilter method testRetryFilter.

@Test
public void testRetryFilter() {
    String retryMessage = "this is a retry";
    ClientRetryFilter clientRetryFilter = new ClientRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertTrue(ex instanceof RetriableRequestException);
            Assert.assertEquals(retryMessage, ex.getMessage());
        }
    };
    Map<String, String> wireAttributes = new HashMap<>();
    wireAttributes.put(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY, retryMessage);
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, clientRetryFilter);
    FilterUtil.fireRestError(filterChain, new RemoteInvocationException("exception"), wireAttributes);
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) HashMap(java.util.HashMap) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ClientRetryFilter(com.linkedin.r2.filter.transport.ClientRetryFilter) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)249 Test (org.testng.annotations.Test)201 RestRequest (com.linkedin.r2.message.rest.RestRequest)148 URI (java.net.URI)139 RestResponse (com.linkedin.r2.message.rest.RestResponse)106 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)102 HashMap (java.util.HashMap)73 ByteString (com.linkedin.data.ByteString)66 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)63 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)55 FutureCallback (com.linkedin.common.callback.FutureCallback)51 ArrayList (java.util.ArrayList)41 Callback (com.linkedin.common.callback.Callback)39 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)39 RestException (com.linkedin.r2.message.rest.RestException)38 Map (java.util.Map)37 CountDownLatch (java.util.concurrent.CountDownLatch)37 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)36 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)31 BeforeTest (org.testng.annotations.BeforeTest)31