Search in sources :

Example 76 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testTextPlainFormatFlowed.

@Test
public void testTextPlainFormatFlowed() throws MessagingException {
    // Create text/plain body
    Body body = new BinaryMemoryBody(BODY_TEXT_FLOWED.getBytes(StandardCharsets.UTF_8), "utf-8");
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = "K-9 Mail rocks :> flowed line\r\n" + "not flowed line";
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + "K-9 Mail rocks :&gt; flowed line<br>not flowed line" + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TextBody(com.fsck.k9.mail.internet.TextBody) Body(com.fsck.k9.mail.Body) TestMessageConstructionUtils.messageFromBody(com.fsck.k9.mail.TestMessageConstructionUtils.messageFromBody) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 77 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testShouldSanitizeOutputHtml.

@Test
public void testShouldSanitizeOutputHtml() throws MessagingException {
    // Create text/plain body
    TextBody body = new TextBody(BODY_TEXT);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, body);
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; format=flowed");
    // Prepare fixture
    HtmlProcessor htmlProcessor = mock(HtmlProcessor.class);
    MessageViewInfoExtractor messageViewInfoExtractor = new MessageViewInfoExtractor(null, htmlProcessor, new TestCoreResourceProvider());
    String value = "--sanitized html--";
    when(htmlProcessor.processForDisplay(anyString())).thenReturn(value);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText viewableExtractedText = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    assertSame(value, viewableExtractedText.html);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) HtmlProcessor(app.k9mail.html.cleaner.HtmlProcessor) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) TestCoreResourceProvider(com.fsck.k9.TestCoreResourceProvider) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 78 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testMultipartPlainTextMessage.

@Test
public void testMultipartPlainTextMessage() throws MessagingException {
    String bodyText1 = "text body 1";
    String bodyText2 = "text body 2";
    // Create text/plain bodies
    TextBody body1 = new TextBody(bodyText1);
    TextBody body2 = new TextBody(bodyText2);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(body1, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(body2, "text/plain");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = bodyText1 + "\r\n\r\n" + "------------------------------------------------------------------------\r\n\r\n" + bodyText2;
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + bodyText1 + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; " + "border-bottom: 1px solid #000\"></p>" + "<pre dir=\"auto\" class=\"k9mail\">" + bodyText2 + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 79 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testSimplePlainTextMessage.

@Test
public void testSimplePlainTextMessage() throws MessagingException {
    // Create text/plain body
    TextBody body = new TextBody(BODY_TEXT);
    // Create message
    MimeMessage message = new MimeMessage();
    message.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain");
    MimeMessageHelper.setBody(message, body);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + "K-9 Mail rocks :&gt;" + "</pre>";
    assertEquals(BODY_TEXT, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) ArrayList(java.util.ArrayList) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 80 with Body

use of com.fsck.k9.mail.Body in project k-9 by k9mail.

the class MessageViewInfoExtractorTest method testTextPlusRfc822Message.

@Test
public void testTextPlusRfc822Message() throws MessagingException {
    setLanguage("en");
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));
    String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment.";
    // Create text/plain body
    TextBody textBody = new TextBody(BODY_TEXT);
    // Create inner text/plain body
    TextBody innerBody = new TextBody(innerBodyText);
    // Create message/rfc822 body
    MimeMessage innerMessage = new MimeMessage();
    innerMessage.addSentDate(new Date(112, 2, 17), false);
    innerMessage.setHeader("To", "to@example.com");
    innerMessage.setSubject("Subject");
    innerMessage.setFrom(new Address("from@example.com"));
    MimeMessageHelper.setBody(innerMessage, innerBody);
    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822");
    bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\"");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);
    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);
    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container = messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);
    String expectedText = BODY_TEXT + "\r\n\r\n" + "----- message.eml ------------------------------------------------------" + "\r\n\r\n" + "From: from@example.com" + "\r\n" + "To: to@example.com" + "\r\n" + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012" + "\r\n" + "Subject: Subject" + "\r\n" + "\r\n" + innerBodyText;
    String expectedHtml = "<pre dir=\"auto\" class=\"k9mail\">" + BODY_TEXT_HTML + "</pre>" + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: " + "1px solid #000\">message.eml</p>" + "<table style=\"border: 0\">" + "<tr>" + "<th style=\"text-align: left; vertical-align: top;\">From:</th>" + "<td>from@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">To:</th>" + "<td>to@example.com</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>" + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>" + "</tr><tr>" + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>" + "<td>Subject</td>" + "</tr>" + "</table>" + "<pre dir=\"auto\" class=\"k9mail\">" + innerBodyText + "</pre>";
    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
}
Also used : Address(com.fsck.k9.mail.Address) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) TextBody(com.fsck.k9.mail.internet.TextBody) MimeMessage(com.fsck.k9.mail.internet.MimeMessage) MimeMultipart(com.fsck.k9.mail.internet.MimeMultipart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) BodyPart(com.fsck.k9.mail.BodyPart) Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) ViewableExtractedText(com.fsck.k9.mailstore.MessageViewInfoExtractor.ViewableExtractedText) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Aggregations

Body (com.fsck.k9.mail.Body)44 BodyPart (com.fsck.k9.mail.BodyPart)35 Multipart (com.fsck.k9.mail.Multipart)32 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)32 Part (com.fsck.k9.mail.Part)29 Test (org.junit.Test)29 TextBody (com.fsck.k9.mail.internet.TextBody)23 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)21 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)19 ArrayList (java.util.ArrayList)16 MessagingException (com.fsck.k9.mail.MessagingException)14 BinaryTempFileBody (com.fsck.k9.mail.internet.BinaryTempFileBody)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Message (com.fsck.k9.mail.Message)9 OutputStream (java.io.OutputStream)9 Stack (java.util.Stack)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)7 BinaryMemoryBody (com.fsck.k9.mailstore.BinaryMemoryBody)7 InputStream (java.io.InputStream)7