use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEReaderExceptions method boundaryPrematurelyTerminatedNoSubsequentCRLFs.
@Test(dataProvider = "multiplePartsDataSource")
public void boundaryPrematurelyTerminatedNoSubsequentCRLFs(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 byte[] mimePayload = byteArrayOutputStream.toByteArray();
//At this point the mimePayload's ending looks something like the following. Consider that
//--1234 is the boundary:
//<ending of some part data>--1234--/r/n
//What we want to test this particular code path is:
//<ending of some part data>--1234678
//So we trim off an element in the array at the end which results in:
//<ending of some part data>--1234--/r
//And then we modify the last three bytes to end up with:
//<ending of some part data>--1234678
final byte[] trimmedMimePayload = Arrays.copyOf(mimePayload, mimePayload.length - 1);
trimmedMimePayload[trimmedMimePayload.length - 1] = 8;
trimmedMimePayload[trimmedMimePayload.length - 2] = 7;
trimmedMimePayload[trimmedMimePayload.length - 3] = 6;
final ByteString requestPayload = ByteString.copy(trimmedMimePayload);
executeRequestWithDesiredException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), "Malformed multipart mime request. Premature termination of multipart " + "mime body due to a boundary without a subsequent consecutive CRLF.");
//In this case we want all the parts to still make it over
List<SinglePartMIMEExceptionReaderCallbackImpl> singlePartMIMEReaderCallbacks = _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), multiPartMimeBody.getCount());
//Everything should have made it over
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
//Actual
final SinglePartMIMEExceptionReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = multiPartMimeBody.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback.getHeaders(), expectedHeaders);
//Verify the body matches
Assert.assertNotNull(currentCallback.getFinishedData());
if (currentExpectedPart.getContent() instanceof byte[]) {
Assert.assertEquals(currentCallback.getFinishedData().copyBytes(), currentExpectedPart.getContent());
} else {
//Default is String
Assert.assertEquals(new String(currentCallback.getFinishedData().copyBytes()), currentExpectedPart.getContent());
}
}
}
use of javax.mail.internet.MimeBodyPart 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 javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEIntegrationReaderDrain method testSinglePartialTopRemaining.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSinglePartialTopRemaining(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Execute the request, verify the correct header came back to ensure the server took the proper draining actions
//and return the payload so we can assert deeper.
MimeMultipart mimeMultipart = executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_PARTIAL_TOP_REMAINING, "onDrainComplete");
//Single part drains the first 6 then the top level drains all of remaining
List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestDrainHandler.getTestMultiPartMIMEReaderCallback().getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 6);
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback._headers, expectedHeaders);
//Verify that the bodies are empty
Assert.assertEquals(currentCallback._finishedData, ByteString.empty());
}
}
use of javax.mail.internet.MimeBodyPart in project rest.li by linkedin.
the class TestMIMEIntegrationReaderDrain method testSingleAll.
@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testSingleAll(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
//Execute the request, verify the correct header came back to ensure the server took the proper drain actions
//and return the payload so we can assert deeper.
MimeMultipart mimeMultipart = executeRequestWithDrainStrategy(chunkSize, bodyPartList, SINGLE_ALL, "onFinished");
//Single part drains all, one by one
List<SinglePartMIMEDrainReaderCallbackImpl> singlePartMIMEReaderCallbacks = _mimeServerRequestDrainHandler.getTestMultiPartMIMEReaderCallback().getSinglePartMIMEReaderCallbacks();
Assert.assertEquals(singlePartMIMEReaderCallbacks.size(), 12);
//Verify everything was drained
for (int i = 0; i < singlePartMIMEReaderCallbacks.size(); i++) {
//Actual
final SinglePartMIMEDrainReaderCallbackImpl currentCallback = singlePartMIMEReaderCallbacks.get(i);
//Expected
final BodyPart currentExpectedPart = mimeMultipart.getBodyPart(i);
//Construct expected headers and verify they match
final Map<String, String> expectedHeaders = new HashMap<String, String>();
@SuppressWarnings("unchecked") final Enumeration<Header> allHeaders = currentExpectedPart.getAllHeaders();
while (allHeaders.hasMoreElements()) {
final Header header = allHeaders.nextElement();
expectedHeaders.put(header.getName(), header.getValue());
}
Assert.assertEquals(currentCallback._headers, expectedHeaders);
//Verify that the bodies are empty
Assert.assertEquals(currentCallback._finishedData, ByteString.empty());
}
}
use of javax.mail.internet.MimeBodyPart in project jodd by oblac.
the class SendMailTest method testTextHtml.
@Test
public void testTextHtml() throws MessagingException, IOException {
Email email = Email.create().from("from@example.com").to("to@example.com").subject("sub").addText("Hello!").addHtml("<html><body><h1>Hey!</h1></body></html>");
Message message = createMessage(email);
assertEquals(1, message.getFrom().length);
assertEquals("from@example.com", message.getFrom()[0].toString());
assertEquals(1, message.getRecipients(Message.RecipientType.TO).length);
assertEquals("to@example.com", message.getRecipients(Message.RecipientType.TO)[0].toString());
assertEquals("sub", message.getSubject());
// wrapper
MimeMultipart multipart = (MimeMultipart) message.getContent();
assertEquals(1, multipart.getCount());
assertTrue(multipart.getContentType().contains("multipart/mixed"));
// inner content
MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0);
MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent();
assertEquals(2, mimeMultipart.getCount());
assertTrue(mimeMultipart.getContentType().contains("multipart/alternative"));
MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0);
assertEquals("Hello!", bodyPart.getContent());
assertTrue(bodyPart.getDataHandler().getContentType().contains("text/plain"));
bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(1);
assertEquals("<html><body><h1>Hey!</h1></body></html>", bodyPart.getContent());
assertTrue(bodyPart.getDataHandler().getContentType().contains("text/html"));
}
Aggregations