package com.mailserver.service; import jakarta.mail.Session; import jakarta.mail.internet.MimeMessage; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; import java.util.Properties; import static org.assertj.core.api.Assertions.assertThat; class LocalDeliveryTest { private static MimeMessage parse(String text) throws Exception { return new MimeMessage(Session.getInstance(new Properties()), new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); } @Test void prefersHtmlInNestedAlternativeAndSkipsAttachments() throws Exception { MimeMessage message = parse(""" Content-Type: multipart/mixed; boundary="m"\r \r --m\r Content-Type: multipart/alternative; boundary="a"\r \r --a\r Content-Type: text/plain\r \r plain\r --a\r Content-Type: text/html\r \r
html
\r --a--\r --m\r Content-Type: text/html\r Content-Disposition: attachment; filename="x.html"\r \rattached
\r --m--\r """); LocalDelivery.Body body = LocalDelivery.body(message); assertThat(body.html()).isTrue(); assertThat(body.text()).contains("html
").doesNotContain("attached"); } @Test void plainTextMessage() throws Exception { LocalDelivery.Body body = LocalDelivery.body(parse("Subject: x\r\n\r\njust text\r\n")); assertThat(body.html()).isFalse(); assertThat(body.text()).isEqualTo("just text\r\n"); } }