use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToHttp2HeadersBlacklist.
@Test
public void testStreamToHttp2HeadersBlacklist() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
HEADER_BLACKLIST.forEach(header -> streamRequestBuilder.addHeaderValue(header, ANY_HEADER));
StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
Assert.assertNotNull(headers);
HEADER_BLACKLIST.forEach(header -> Assert.assertFalse(headers.contains(header), header));
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToHttp2HeadersPseudoHeaders.
@Test
public void testStreamToHttp2HeadersPseudoHeaders() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
Assert.assertNotNull(headers);
Assert.assertEquals(headers.authority(), "localhost:8080");
Assert.assertEquals(headers.method(), "GET");
Assert.assertEquals(headers.path(), "/foo/bar?q=baz");
Assert.assertEquals(headers.scheme(), "http");
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestHttpNettyClient method testUnsupportedStreamRequest.
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedStreamRequest() throws UnsupportedOperationException {
HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRestClient();
client.streamRequest(null, new RequestContext(), new HashMap<>(), null);
Assert.fail("The Http Rest client should throw UnsupportedOperationException when streamRequest is called");
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class TestHttpNettyStreamClient method testSlowReaderTimeout.
/**
* Tests slow EntityStream {@link Reader} implementation should be subject to streaming timeout even
* if the entire response entity can be buffered in memory.
*
* @throws Exception
*/
@Test(dataProvider = "slowReaderTimeoutClientProvider")
public void testSlowReaderTimeout(AbstractNettyStreamClient client) throws Exception {
// Sets the response size to be greater than zero but smaller than the in-memory buffer for HTTP/1.1
// and smaller than the receiving window size for HTTP/2 so the receiver will not block sender
Server server = new HttpServerBuilder().responseSize(R2Constants.DEFAULT_DATA_CHUNK_SIZE).build();
StreamRequest request = new StreamRequestBuilder(new URI(URL)).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.emptyStream());
final CountDownLatch responseLatch = new CountDownLatch(1);
final CountDownLatch streamLatch = new CountDownLatch(1);
final AtomicReference<TransportResponse<StreamResponse>> atomicTransportResponse = new AtomicReference<>();
final AtomicReference<Throwable> atomicThrowable = new AtomicReference<>();
try {
server.start();
client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> {
atomicTransportResponse.set(response);
responseLatch.countDown();
// Sets a reader that does not consume any byte
response.getResponse().getEntityStream().setReader(new Reader() {
@Override
public void onInit(ReadHandle rh) {
}
@Override
public void onDataAvailable(ByteString data) {
}
@Override
public void onDone() {
}
@Override
public void onError(Throwable e) {
atomicThrowable.set(e);
streamLatch.countDown();
}
});
});
} finally {
responseLatch.await(5, TimeUnit.SECONDS);
streamLatch.await(5, TimeUnit.SECONDS);
server.stop();
}
TransportResponse<StreamResponse> transportResponse = atomicTransportResponse.get();
Assert.assertNotNull(transportResponse, "Expected to receive a response");
Assert.assertFalse(transportResponse.hasError(), "Expected to receive a response without error");
Assert.assertNotNull(transportResponse.getResponse());
Assert.assertNotNull(transportResponse.getResponse().getEntityStream());
Throwable throwable = atomicThrowable.get();
Assert.assertNotNull(throwable, "Expected onError invoked with TimeoutException");
Assert.assertTrue(throwable instanceof RemoteInvocationException);
Assert.assertNotNull(throwable.getCause());
Assert.assertTrue(throwable.getCause() instanceof TimeoutException);
}
use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.
the class RAPStreamRequestEncoder method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (msg instanceof StreamRequest) {
StreamRequest request = (StreamRequest) msg;
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(request);
ctx.write(nettyRequest, promise);
_currentReader = new OrderedEntityStreamReader(ctx, new BufferedReader(ctx, MAX_BUFFERED_CHUNKS, FLUSH_THRESHOLD));
request.getEntityStream().setReader(_currentReader);
} else {
_currentReader = null;
ctx.write(msg, promise);
}
}
Aggregations