use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestServerCompressionFilter method testResponseCompressionRules.
// Test response compression rules where the server has a default threshold of Integer.MAX_VALUE.
@Test(dataProvider = "headersData")
public void testResponseCompressionRules(String acceptEncoding, int compressionThreshold, EncodingType expectedContentEncoding) throws CompressionException, URISyntaxException {
ServerCompressionFilter serverCompressionFilter = new ServerCompressionFilter(ACCEPT_COMPRESSIONS);
RequestContext context = new RequestContext();
context.putLocalAttr(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
context.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, compressionThreshold);
int originalLength = 100;
byte[] entity = new byte[originalLength];
Arrays.fill(entity, (byte) 'A');
int compressedLength = (expectedContentEncoding == null) ? originalLength : expectedContentEncoding.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
String expectedContentEncodingName = (expectedContentEncoding == null) ? null : expectedContentEncoding.getHttpName();
RestResponse restResponse = new RestResponseBuilder().setEntity(entity).build();
serverCompressionFilter.onRestResponse(restResponse, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncodingName, compressedLength));
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestChannelPoolBehavior method testChannelReuse.
@Test
public void testChannelReuse() throws Exception {
_client2.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NOT_FOUND_URI)).build(EntityStreams.newEntityStream(new SlowWriter())), new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
if (e instanceof StreamException) {
StreamException streamException = (StreamException) e;
streamException.getResponse().getEntityStream().setReader(new CancelingReader());
}
throw new RuntimeException(e);
}
@Override
public void onSuccess(StreamResponse result) {
result.getEntityStream().setReader(new DrainReader());
}
});
Future<RestResponse> responseFuture = _client2.restRequest(new RestRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build());
RestResponse response = responseFuture.get(WRITER_DELAY * 1000, TimeUnit.MILLISECONDS);
Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestDisruptor method testStreamLatencyDisrupt.
@Test
public void testStreamLatencyDisrupt() throws Exception {
final Map<String, String> properties = new HashMap<>();
final TransportClientFactory factory = new HttpClientFactory.Builder().build();
final TransportClient client = factory.getClient(properties);
final RequestContext requestContext = new RequestContext();
requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.delay(REQUEST_LATENCY));
final CountDownLatch latch = new CountDownLatch(1);
final AtomicBoolean success = new AtomicBoolean(false);
client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
success.set(!response.hasError() && response.getResponse() != null);
latch.countDown();
});
Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
Assert.assertTrue(success.get(), "Unexpected transport response");
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestHttpClient method testSimpleURI.
@Test(dataProvider = "configs")
public void testSimpleURI(Server server, Client client) throws Exception {
try {
server.start();
// Note no trailing slash; the point of the test is to ensure this URI will
// send a Request-URI of "/".
URI uri = URI.create("http://localhost:" + PORT);
RestRequestBuilder rb = new RestRequestBuilder(uri);
rb.setMethod("GET");
RestRequest request = rb.build();
Future<RestResponse> f = client.restRequest(request);
// This will block
RestResponse response = f.get();
assertEquals(response.getStatus(), 200);
final FutureCallback<None> callback = new FutureCallback<None>();
client.shutdown(callback);
callback.get();
} finally {
server.stop();
}
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class RestClient method sendRequest.
/**
* Sends a multiplexed request. Responses are provided to individual requests' callbacks. After all responses are
* received the given aggregated callback is invoked.
*
* The request is sent using the protocol version 2.0.
*
* @param multiplexedRequest the multiplexed request to send.
* @param requestContext context for the request
* @param callback the aggregated response callback.
*/
public void sendRequest(MultiplexedRequest multiplexedRequest, RequestContext requestContext, Callback<MultiplexedResponse> callback) {
MultiplexedCallback muxCallback = new MultiplexedCallback(multiplexedRequest.getCallbacks(), callback);
addDisruptContext(MULTIPLEXER_RESOURCE, requestContext);
try {
RestRequest restRequest = buildMultiplexedRequest(multiplexedRequest);
_client.restRequest(restRequest, requestContext, muxCallback);
} catch (Exception e) {
muxCallback.onError(e);
}
}
Aggregations