-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#552: [bug] support iCalendar events with METHOD defined in body inst…
…ead of Content-Type
- Loading branch information
Showing
6 changed files
with
394 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...mplejavamail/converter/internal/mimemessage/MimeMessageParserParseCalendarMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.simplejavamail.converter.internal.mimemessage; | ||
|
||
import jakarta.activation.DataHandler; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimePart; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class MimeMessageParserParseCalendarMethodTest { | ||
|
||
@Test | ||
public void testMethodFoundInContentType() throws Exception { | ||
MimePart mockMimePart = mock(MimePart.class); | ||
DataHandler mockDataHandler = mock(DataHandler.class); | ||
|
||
// Mock Content-Type with METHOD | ||
when(mockMimePart.getDataHandler()).thenReturn(mockDataHandler); | ||
when(mockDataHandler.getContentType()).thenReturn("text/calendar; method=REQUEST; charset=UTF-8"); | ||
|
||
String calendarContent = "BEGIN:VCALENDAR\nMETHOD:REQUEST\nEND:VCALENDAR"; | ||
|
||
assertThat(MimeMessageParser.parseCalendarMethod(mockMimePart, calendarContent)).isEqualTo("REQUEST"); | ||
} | ||
|
||
@Test | ||
public void testMethodFoundInBody() throws Exception { | ||
MimePart mockMimePart = mock(MimePart.class); | ||
DataHandler mockDataHandler = mock(DataHandler.class); | ||
|
||
// Mock Content-Type without METHOD | ||
when(mockMimePart.getDataHandler()).thenReturn(mockDataHandler); | ||
when(mockDataHandler.getContentType()).thenReturn("text/calendar; charset=UTF-8"); | ||
|
||
// Method only in calendar content | ||
String calendarContent = "BEGIN:VCALENDAR\nMETHOD:REQUEST\nEND:VCALENDAR"; | ||
|
||
assertThat(MimeMessageParser.parseCalendarMethod(mockMimePart, calendarContent)).isEqualTo("REQUEST"); | ||
} | ||
|
||
@Test | ||
public void testMethodNotFoundThrowsException() throws Exception { | ||
MimePart mockMimePart = mock(MimePart.class); | ||
DataHandler mockDataHandler = mock(DataHandler.class); | ||
|
||
// Mock Content-Type and Body without METHOD | ||
when(mockMimePart.getDataHandler()).thenReturn(mockDataHandler); | ||
when(mockDataHandler.getContentType()).thenReturn("text/calendar; charset=UTF-8"); | ||
|
||
// No method in the calendar body | ||
String calendarContent = "BEGIN:VCALENDAR\nEND:VCALENDAR"; | ||
|
||
assertThatThrownBy(() -> MimeMessageParser.parseCalendarMethod(mockMimePart, calendarContent)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("Calendar METHOD not found"); | ||
} | ||
|
||
@Test | ||
public void testMessagingExceptionThrown() throws Exception { | ||
MimePart mockMimePart = mock(MimePart.class); | ||
|
||
when(mockMimePart.getDataHandler()).thenThrow(new MessagingException("Failed to retrieve content type")); | ||
|
||
String calendarContent = "BEGIN:VCALENDAR\nMETHOD=REQUEST\nEND:VCALENDAR"; | ||
|
||
assertThatThrownBy(() -> MimeMessageParser.parseCalendarMethod(mockMimePart, calendarContent)) | ||
.isInstanceOf(MimeMessageParseException.class) | ||
.hasMessageContaining(MimeMessageParseException.ERROR_GETTING_CALENDAR_CONTENTTYPE); | ||
} | ||
|
||
@Test | ||
public void testMethodInBothContentTypeAndBody_ContentTypeTakesPriority() throws Exception { | ||
MimePart mockMimePart = mock(MimePart.class); | ||
DataHandler mockDataHandler = mock(DataHandler.class); | ||
|
||
// Mock Content-Type with METHOD | ||
when(mockMimePart.getDataHandler()).thenReturn(mockDataHandler); | ||
when(mockDataHandler.getContentType()).thenReturn("text/calendar; method=REQUEST; charset=UTF-8"); | ||
|
||
// METHOD also present in calendar content, but different from Content-Type | ||
String calendarContent = "BEGIN:VCALENDAR\nMETHOD:CANCEL\nEND:VCALENDAR"; | ||
|
||
assertThat(MimeMessageParser.parseCalendarMethod(mockMimePart, calendarContent)).isEqualTo("REQUEST"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,10 @@ | |
import java.util.Map; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.regex.Pattern.compile; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.simplejavamail.internal.util.MiscUtil.findFirstMatch; | ||
|
||
public class MiscUtilTest { | ||
|
||
|
@@ -234,4 +236,14 @@ public void testAddRecipientByInternetAddress() { | |
// next one is unparsable by InternetAddress#parse(), so it should be taken as is | ||
assertThat(MiscUtil.interpretRecipient(null, false, " \" m oo \" [email protected] ", null)).isEqualTo(new Recipient(null, " \" m oo \" [email protected] ", null)); | ||
} | ||
|
||
@Test | ||
public void testFindFirstMatch() { | ||
assertThat(findFirstMatch(compile("method=(\\w+)"), "Content-Type: text/calendar; method=REQUEST; charset=UTF-8")).hasValue("REQUEST"); | ||
assertThat(findFirstMatch(compile("method=(\\w+)"), "Content-Type: text/calendar; charset=UTF-8")).isEmpty(); | ||
assertThat(findFirstMatch(compile("method=(\\w+)"), "")).isEmpty(); | ||
assertThat(findFirstMatch(compile("method=(\\w+)"), "Content-Type: text/calendar; method=RE$QUEST; charset=UTF-8")).isNotEmpty(); | ||
assertThat(findFirstMatch(compile("method=(\\w+)"), "method=REJECT; method=REQUEST")).hasValue("REJECT"); | ||
assertThat(findFirstMatch(compile("(?i)method=(\\w+)"), "Content-Type: text/calendar; METHOD=REQUEST; charset=UTF-8")).hasValue("REQUEST"); | ||
} | ||
} |
Oops, something went wrong.