use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.
the class TestMIMEWriter method testPrependDataSources.
@Test(dataProvider = "prependDataSources")
public void testPrependDataSources(final MultiPartMIMEWriter.Builder builder, final List<MultiPartMIMEDataSourceWriter> prependDataSources, final List<MIMEDataPart> expectedParts, final String expectedPreamble, final int expectedSize) throws Exception {
for (final MultiPartMIMEDataSourceWriter dataSourceWriter : prependDataSources) {
builder.prependDataSource(dataSourceWriter);
}
Assert.assertEquals(builder.getCurrentSize(), expectedSize);
final MultiPartMIMEWriter writer = builder.build();
final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
writer.getEntityStream().setReader(fullEntityReader);
futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", writer, Collections.<String, String>emptyMap());
final JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
javaxMailMultiPartMIMEReader.parseRequestIntoParts();
List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
Assert.assertEquals(dataSourceList.size(), 3);
for (int i = 0; i < dataSourceList.size(); i++) {
Assert.assertEquals(dataSourceList.get(i), expectedParts.get(i));
}
// Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble != null ? javaxMailMultiPartMIMEReader._preamble.trim() : null, expectedPreamble);
}
use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.
the class TestMIMEWriter method testSingleDataSource.
@Test(dataProvider = "singleDataSources")
public void testSingleDataSource(final ByteString body, final Map<String, String> headers) throws Exception {
final MIMEDataPart expectedMultiPartMIMEDataPart = new MIMEDataPart(body, headers);
final MultiPartMIMEInputStream singleDataSource = new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(body.copyBytes()), _scheduledExecutorService, headers).build();
final MultiPartMIMEWriter multiPartMIMEWriter = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSource(singleDataSource).build();
final FutureCallback<ByteString> futureCallback = new FutureCallback<>();
final FullEntityReader fullEntityReader = new FullEntityReader(futureCallback);
multiPartMIMEWriter.getEntityStream().setReader(fullEntityReader);
futureCallback.get(_testTimeout, TimeUnit.MILLISECONDS);
final StreamRequest multiPartMIMEStreamRequest = MultiPartMIMEStreamRequestFactory.generateMultiPartMIMEStreamRequest(URI.create("localhost"), "mixed", multiPartMIMEWriter, Collections.<String, String>emptyMap());
JavaxMailMultiPartMIMEReader javaxMailMultiPartMIMEReader = new JavaxMailMultiPartMIMEReader(multiPartMIMEStreamRequest.getHeader(MultiPartMIMEUtils.CONTENT_TYPE_HEADER), futureCallback.get());
javaxMailMultiPartMIMEReader.parseRequestIntoParts();
List<MIMEDataPart> dataSourceList = javaxMailMultiPartMIMEReader._dataSourceList;
Assert.assertEquals(dataSourceList.size(), 1);
Assert.assertEquals(dataSourceList.get(0), expectedMultiPartMIMEDataPart);
// Javax mail incorrectly adds the CRLF for the first boundary to the end of the preamble, so we trim
Assert.assertEquals(javaxMailMultiPartMIMEReader._preamble.trim(), "preamble");
}
use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.
the class TestClientStreamCompressionFilter method testRequestCompressionRules.
@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
Executor executor = Executors.newCachedThreadPool();
ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
// The entity should be compressible for this test.
int original = 100;
byte[] entity = new byte[original];
Arrays.fill(entity, (byte) 'A');
StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
RequestContext context = new RequestContext();
if (operation != null) {
context.putLocalAttr(R2Constants.OPERATION, operation);
}
context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
int entityLength = headerShouldBePresent ? compressed : original;
FutureCallback<ByteString> callback = new FutureCallback<>();
FullEntityReader reader = new FullEntityReader(callback);
HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength, reader);
clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
Assert.assertEquals(entityRead.length(), entityLength);
}
use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.
the class TestRestLiServer method testValidReactiveUnstructuredDataRequest.
@Test(dataProvider = "validClientProtocolVersionDataStreamOnly")
public void testValidReactiveUnstructuredDataRequest(RestLiServer server, ProtocolVersion clientProtocolVersion, String headerConstant) throws URISyntaxException, IOException {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/reactiveFeedDownloads/1")).setHeader(headerConstant, clientProtocolVersion.toString()).build(EntityStreams.emptyStream());
final FeedDownloadResourceReactive resource = getMockResource(FeedDownloadResourceReactive.class);
resource.get(eq(1L), anyObject());
EasyMock.expectLastCall().andDelegateTo(new FeedDownloadResourceReactive()).once();
replay(resource);
@SuppressWarnings("unchecked") Callback<StreamResponse> r2Callback = createMock(Callback.class);
final Capture<StreamResponse> streamResponse = EasyMock.newCapture();
r2Callback.onSuccess(capture(streamResponse));
expectLastCall().once();
replay(r2Callback);
RequestContext requestContext = new RequestContext();
server.handleRequest(streamRequest, requestContext, r2Callback);
verify(resource);
verify(r2Callback);
assertNotNull(streamResponse);
assertEquals(streamResponse.getValue().getHeader(RestConstants.HEADER_CONTENT_TYPE), FeedDownloadResourceReactive.CONTENT_TYPE);
FullEntityReader fullEntityReader = new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
fail("Error inside callback!! Failed to read response data from stream!", e);
}
@Override
public void onSuccess(ByteString result) {
assertEquals(result, FeedDownloadResourceReactive.CONTENT);
}
});
streamResponse.getValue().getEntityStream().setReader(fullEntityReader);
}
use of com.linkedin.r2.message.stream.entitystream.FullEntityReader in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnResponse.
@Test
public void testRequestFilterAdapterCallsOnResponse() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onResponse(simpleRestResponse(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
StreamResponse capturedReq = _beforeFilter.getResponse();
capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("shouldn't have error");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
}
Aggregations