use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToHttp2HeadersCookies.
@Test
public void testStreamToHttp2HeadersCookies() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
IntStream.range(0, 10).forEach(i -> streamRequestBuilder.addCookie(ANY_COOKIE));
StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
Assert.assertNotNull(headers);
List<CharSequence> cookies = headers.getAll(HttpHeaderNames.COOKIE);
Assert.assertNotNull(cookies);
Assert.assertEquals(cookies.size(), 10);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.
the class TestNettyRequestAdapter method testStreamToNettyRequestWithMultipleCookies.
@Test
public void testStreamToNettyRequestWithMultipleCookies() throws Exception {
StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
streamRequestBuilder.setCookies(ANY_COOKIES);
StreamRequest streamRequest = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(streamRequest);
Assert.assertEquals(nettyRequest.headers().get("Cookie"), ENCODED_COOKIES_HEADER_VALUE);
}
use of com.linkedin.r2.message.stream.StreamRequestBuilder 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.StreamRequestBuilder 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.StreamRequestBuilder 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);
}
Aggregations