Search in sources :

Example 76 with RestRequestBuilder

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

the class TestNettyRequestAdapter method testRestToNettyRequest.

@Test
public void testRestToNettyRequest() throws Exception {
    RestRequestBuilder restRequestBuilder = new RestRequestBuilder(new URI(ANY_URI));
    restRequestBuilder.setMethod("POST");
    restRequestBuilder.setEntity(ByteString.copyString(ANY_ENTITY, Charset.defaultCharset()));
    restRequestBuilder.setHeader("Content-Length", Integer.toString(restRequestBuilder.getEntity().length()));
    restRequestBuilder.setHeader("Content-Type", "application/json");
    restRequestBuilder.setCookies(Collections.singletonList(ANY_COOKIE));
    RestRequest restRequest = restRequestBuilder.build();
    HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(restRequest);
    Assert.assertEquals(nettyRequest.uri(), "/foo/bar?q=baz");
    Assert.assertEquals(nettyRequest.method(), HttpMethod.POST);
    Assert.assertEquals(nettyRequest.protocolVersion(), HttpVersion.HTTP_1_1);
    Assert.assertEquals(nettyRequest.headers().get("Content-Length"), Integer.toString(restRequestBuilder.getEntity().length()));
    Assert.assertEquals(nettyRequest.headers().get("Content-Type"), "application/json");
    Assert.assertEquals(nettyRequest.headers().get("Cookie"), ANY_COOKIE);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 77 with RestRequestBuilder

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

the class TestChannelPoolManagerFactorySharingConnection method makeRequestsWithClients.

/**
 * Helper function that creates clientFactory, makes request sequentially, checks the result and shutdowns everything
 */
public void makeRequestsWithClients(boolean shareConnection, ClientGenerator clientGenerator, int expectedConnectionsWithSharing) throws Exception {
    HttpClientFactory clientFactory = new HttpClientFactory.Builder().setShareConnection(shareConnection).build();
    HttpServerBuilder.HttpServerStatsProvider httpServerStatsProvider = getHttpServerStatsProviderIgnoringOptions();
    Server server = new HttpServerBuilder().serverStatsProvider(httpServerStatsProvider).build();
    try {
        server.start();
        List<Client> clients = new ArrayList<>();
        for (int i = 0; i < TRIALS; i++) {
            clientGenerator.populate(clients, clientFactory);
        }
        for (Client c : clients) {
            RestRequest r = new RestRequestBuilder(new URI(TestHttpClientFactory.URI)).build();
            c.restRequest(r).get(30, TimeUnit.SECONDS);
            FutureCallback<None> shutdownCallback = new FutureCallback<>();
            c.shutdown(shutdownCallback);
            shutdownCallback.get(20, TimeUnit.SECONDS);
        }
        Assert.assertEquals(httpServerStatsProvider.requestCount(), NUMBER_OF_REQUESTS);
        int expectedOpenedConnections = shareConnection ? expectedConnectionsWithSharing : OPENED_CONNECTIONS_WITHOUT_SHARING;
        Assert.assertEquals(httpServerStatsProvider.clientConnections().size(), expectedOpenedConnections);
    } finally {
        server.stop();
    }
    // shutdown the client factory which will trigger the ChannelPoolManagerFactorySharingConnecion shutdown
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    clientFactory.shutdown(shutdownCallback);
    shutdownCallback.get(10, TimeUnit.SECONDS);
}
Also used : Server(org.eclipse.jetty.server.Server) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) ArrayList(java.util.ArrayList) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 78 with RestRequestBuilder

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

the class TestHttpNettyClient method testResponseSize.

public void testResponseSize(int responseSize, int expectedResult) throws InterruptedException, IOException, TimeoutException {
    TestServer testServer = new TestServer();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(50000).setIdleTimeout(10000).setShutdownTimeout(500).setMaxResponseSize(TEST_MAX_RESPONSE_SIZE).buildRestClient();
    RestRequest r = new RestRequestBuilder(testServer.getResponseOfSizeURI(responseSize)).build();
    FutureCallback<RestResponse> cb = new FutureCallback<>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
    try {
        cb.get(30, TimeUnit.SECONDS);
        if (expectedResult == TOO_LARGE) {
            Assert.fail("Max response size exceeded, expected exception. ");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, response was <= max response size.");
        }
        verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
    }
    testServer.shutdown();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 79 with RestRequestBuilder

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

the class TestHttpNettyClient method testShutdown.

@Test
public void testShutdown() throws ExecutionException, TimeoutException, InterruptedException {
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
    FutureCallback<None> shutdownCallback = new FutureCallback<>();
    client.shutdown(shutdownCallback);
    shutdownCallback.get(30, TimeUnit.SECONDS);
    // Now verify a new request will also fail
    RestRequest r = new RestRequestBuilder(URI.create("http://no.such.host.linkedin.com")).build();
    FutureCallback<RestResponse> callback = new FutureCallback<>();
    client.restRequest(r, new RequestContext(), new HashMap<>(), new TransportCallbackAdapter<>(callback));
    try {
        callback.get(30, TimeUnit.SECONDS);
    } catch (ExecutionException e) {
    // Expected
    }
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 80 with RestRequestBuilder

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

the class TestHttpNettyClient method testMakingOutboundHttpsRequest.

@Test(enabled = false)
public void testMakingOutboundHttpsRequest() throws NoSuchAlgorithmException, InterruptedException, ExecutionException, TimeoutException {
    SSLContext context = SSLContext.getDefault();
    SSLParameters sslParameters = context.getDefaultSSLParameters();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setSSLContext(context).setSSLParameters(sslParameters).buildRestClient();
    RestRequest r = new RestRequestBuilder(URI.create("https://www.howsmyssl.com/a/check")).build();
    FutureCallback<RestResponse> cb = new FutureCallback<>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
    cb.get(30, TimeUnit.SECONDS);
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) SSLContext(javax.net.ssl.SSLContext) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) SSLParameters(javax.net.ssl.SSLParameters) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)336 RestRequest (com.linkedin.r2.message.rest.RestRequest)290 Test (org.testng.annotations.Test)267 URI (java.net.URI)220 RestResponse (com.linkedin.r2.message.rest.RestResponse)192 RequestContext (com.linkedin.r2.message.RequestContext)155 ExecutionException (java.util.concurrent.ExecutionException)55 ByteString (com.linkedin.data.ByteString)46 FutureCallback (com.linkedin.common.callback.FutureCallback)43 RestException (com.linkedin.r2.message.rest.RestException)42 HashMap (java.util.HashMap)36 TimeoutException (java.util.concurrent.TimeoutException)29 AfterTest (org.testng.annotations.AfterTest)26 BeforeTest (org.testng.annotations.BeforeTest)26 Callback (com.linkedin.common.callback.Callback)25 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)25 CountDownLatch (java.util.concurrent.CountDownLatch)24 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)21 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)20