use of com.linkedin.data.ByteString 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.data.ByteString 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.data.ByteString in project rest.li by linkedin.
the class TestMIMEIntegrationReader method testPreambleAndEpilogue.
// Just test the preamble and epilogue here
@Test(dataProvider = "preambleEpilogueDataSource")
public void testPreambleAndEpilogue(final int chunkSize, final List<MimeBodyPart> bodyPartList, final String preamble, final String epilogue) throws Exception {
MimeMultipart multiPartMimeBody = new MimeMultipart();
// Add your body parts
for (final MimeBodyPart bodyPart : bodyPartList) {
multiPartMimeBody.addBodyPart(bodyPart);
}
if (preamble != null) {
multiPartMimeBody.setPreamble(preamble);
}
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
multiPartMimeBody.writeTo(byteArrayOutputStream);
final ByteString requestPayload;
if (epilogue != null) {
// Javax mail does not support epilogue so we add it ourselves (other then the CRLF following the final
// boundary).
byteArrayOutputStream.write(epilogue.getBytes());
requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
} else {
// Our test desired no epilogue.
// Remove the CRLF introduced by javax mail at the end. We won't want a fake epilogue.
requestPayload = trimTrailingCRLF(ByteString.copy(byteArrayOutputStream.toByteArray()));
}
executeRequestAndAssert(requestPayload, chunkSize, multiPartMimeBody);
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestMIMEIntegrationReader method testEachSingleBodyDataSourceMultipleTimes.
@Test(dataProvider = "eachSingleBodyDataSource")
public void testEachSingleBodyDataSourceMultipleTimes(final int chunkSize, final MimeBodyPart bodyPart) throws Exception {
MimeMultipart multiPartMimeBody = new MimeMultipart();
// Add your body parts
multiPartMimeBody.addBodyPart(bodyPart);
multiPartMimeBody.addBodyPart(bodyPart);
multiPartMimeBody.addBodyPart(bodyPart);
multiPartMimeBody.addBodyPart(bodyPart);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
multiPartMimeBody.writeTo(byteArrayOutputStream);
final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
executeRequestAndAssert(trimTrailingCRLF(requestPayload), chunkSize, multiPartMimeBody);
}
use of com.linkedin.data.ByteString in project rest.li by linkedin.
the class TestMIMEIntegrationReader method testMultipleNormalBodiesDataSource.
@Test(dataProvider = "multipleNormalBodiesDataSource")
public void testMultipleNormalBodiesDataSource(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
MimeMultipart multiPartMimeBody = new MimeMultipart();
// Add your body parts
for (final MimeBodyPart bodyPart : bodyPartList) {
multiPartMimeBody.addBodyPart(bodyPart);
}
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
multiPartMimeBody.writeTo(byteArrayOutputStream);
final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
executeRequestAndAssert(trimTrailingCRLF(requestPayload), chunkSize, multiPartMimeBody);
}
Aggregations