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);
}
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);
}
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();
}
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
}
}
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);
}
Aggregations