Search in sources :

Example 31 with RequestContext

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

the class TestStreamFilterAdapters method testResponseFilterAdapterChangeResponse.

@Test
public void testResponseFilterAdapterChangeResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(res.builder().setEntity(res.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedResponse = _beforeFilter.getResponse();
    capturedResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "02345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 32 with RequestContext

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

the class TestStreamFilterAdapters method testResponseFilterAdapterCallsOnErrorInOnResponse.

@Test
public void testResponseFilterAdapterCallsOnErrorInOnResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(simpleRestException(res.getEntity().asString("UTF8")), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    Throwable capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof StreamException);
    ((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
    fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 33 with RequestContext

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

the class TestQueryTunnel method testTunneledLongQuery.

@Test
public void testTunneledLongQuery() throws Exception {
    // Just test a true long query, to go beyond the simple 0 threshold tests above
    String query = "q=queryString";
    for (int i = 0; i < 10000; i++) query += "&a=" + i;
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
    // Set threshold < query length
    RestRequest encoded = encode(request, query.length() - 1);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() == query.length());
    Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
    Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(encoded, requestContext);
    Assert.assertEquals(decoded.getURI(), request.getURI());
    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 34 with RequestContext

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

the class TestQueryTunnel method testModifiedQuery.

@Test
public void testModifiedQuery() throws Exception {
    // Test case where someone has added a query param to the request underneath us
    // This test is motivated by some test frameworks that add params to queries in EI
    String query = "q=queryString&a=1&b=2";
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
    // Set threshold < query length
    RestRequest encoded = encode(request, query.length() - 1);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() == query.length());
    Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
    RestRequestBuilder rb = encoded.builder().setURI(new URI(encoded.getURI().toString() + "?uh=f"));
    RestRequest modified = rb.build();
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(modified, requestContext);
    Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?uh=f&" + query);
    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 35 with RequestContext

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

the class TestQueryTunnel method testModifiedQuestionQuery.

@Test
public void testModifiedQuestionQuery() throws Exception {
    // Test case where someone has added a "?" underneath us with no args - make sure
    // we re-append the query correctly
    // This test is motivated by some test frameworks that add params to queries in EI
    String query = "q=queryString&a=1&b=2";
    RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?" + query)).setMethod("GET").build();
    // Set threshold < query length
    RestRequest encoded = encode(request, query.length() - 1);
    Assert.assertEquals(encoded.getMethod(), "POST");
    Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
    Assert.assertTrue(encoded.getEntity().length() == query.length());
    Assert.assertEquals(encoded.getHeader("Content-Type"), "application/x-www-form-urlencoded");
    RestRequestBuilder rb = encoded.builder().setURI(new URI(encoded.getURI().toString() + "?"));
    RestRequest modified = rb.build();
    RequestContext requestContext = new RequestContext();
    RestRequest decoded = decode(modified, requestContext);
    Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?" + query);
    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)

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