use of com.linkedin.multipart.utils.MIMEDataPart 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.multipart.utils.MIMEDataPart 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.multipart.utils.MIMEDataPart in project rest.li by linkedin.
the class TestMIMEWriter method testMultipleDataSources.
@Test
public void testMultipleDataSources() throws Exception {
final List<MIMEDataPart> expectedParts = new ArrayList<>();
expectedParts.add(_normalBody);
expectedParts.add(_normalBody);
expectedParts.add(_headerLessBody);
expectedParts.add(_normalBody);
expectedParts.add(_bodyLessBody);
expectedParts.add(_purelyEmptyBody);
expectedParts.add(_purelyEmptyBody);
expectedParts.add(_headerLessBody);
expectedParts.add(_headerLessBody);
expectedParts.add(_headerLessBody);
expectedParts.add(_normalBody);
expectedParts.add(_bodyLessBody);
final List<MultiPartMIMEDataSourceWriter> inputStreamDataSources = new ArrayList<>();
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, _bodyLessHeaders).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_headerLessBodyData), _scheduledExecutorService, Collections.<String, String>emptyMap()).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(_normalBodyData), _scheduledExecutorService, _normalBodyHeaders).build());
inputStreamDataSources.add(new MultiPartMIMEInputStream.Builder(new ByteArrayInputStream(new byte[0]), _scheduledExecutorService, _bodyLessHeaders).build());
final MultiPartMIMEWriter.Builder multiPartMIMEWriterBuilder = new MultiPartMIMEWriter.Builder("preamble", "epilogue").appendDataSources(inputStreamDataSources);
Assert.assertEquals(multiPartMIMEWriterBuilder.getCurrentSize(), inputStreamDataSources.size());
final MultiPartMIMEWriter multiPartMIMEWriter = multiPartMIMEWriterBuilder.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(), 12);
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.trim(), "preamble");
}
use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.
the class TestMIMEWriter method setup.
@BeforeTest
public void setup() {
_normalBodyData = "abc".getBytes();
_normalBodyHeaders = new HashMap<>();
_normalBodyHeaders.put("simpleheader", "simplevalue");
// Second body has no headers
_headerLessBodyData = "def".getBytes();
// Third body has only headers
_bodyLessHeaders = new HashMap<>();
_normalBodyHeaders.put("header1", "value1");
_normalBodyHeaders.put("header2", "value2");
_normalBodyHeaders.put("header3", "value3");
_normalBody = new MIMEDataPart(ByteString.copy(_normalBodyData), _normalBodyHeaders);
_headerLessBody = new MIMEDataPart(ByteString.copy(_headerLessBodyData), Collections.<String, String>emptyMap());
_bodyLessBody = new MIMEDataPart(ByteString.empty(), _bodyLessHeaders);
_purelyEmptyBody = new MIMEDataPart(ByteString.empty(), Collections.<String, String>emptyMap());
}
use of com.linkedin.multipart.utils.MIMEDataPart in project rest.li by linkedin.
the class TestMIMEIntegrationReaderWriter method dataSourceSetup.
@BeforeClass
public void dataSourceSetup() {
scheduledExecutorService = Executors.newScheduledThreadPool(10);
_normalBodyData = "some normal body that is relatively small".getBytes();
_normalBodyHeaders = new HashMap<>();
_normalBodyHeaders.put("simpleheader", "simplevalue");
// Second body has no headers
_headerLessBodyData = "a body without headers".getBytes();
// Third body has only headers
_bodyLessHeaders = new HashMap<>();
_normalBodyHeaders.put("header1", "value1");
_normalBodyHeaders.put("header2", "value2");
_normalBodyHeaders.put("header3", "value3");
_normalBody = new MIMEDataPart(ByteString.copy(_normalBodyData), _normalBodyHeaders);
_headerLessBody = new MIMEDataPart(ByteString.copy(_headerLessBodyData), Collections.<String, String>emptyMap());
_bodyLessBody = new MIMEDataPart(ByteString.empty(), _bodyLessHeaders);
_purelyEmptyBody = new MIMEDataPart(ByteString.empty(), Collections.<String, String>emptyMap());
}
Aggregations