Skip to content

Commit

Permalink
Merge pull request #87 from HubSpot/src_fix_envelopeparser
Browse files Browse the repository at this point in the history
Add log to collect info about imap envelope parsing issue
  • Loading branch information
bookreaderdev authored Sep 5, 2024
2 parents 0c5fccd + d68220f commit 3459e9a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public class EnvelopeParser {
public static Envelope parse(List<Object> in) {
LOGGER.debug("Parsing envelope response: {}", in);

if (in.size() < 10) {
LOGGER.warn(
"Envelope response must have at least 10 elements, but found {}. The response is: {}",
in.size(),
in
);
}

String dateString = castToString(in.get(0));
String subject = castToString(in.get(1));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.hubspot.imap.utils.parsers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;

import com.hubspot.imap.protocol.message.Envelope;
import com.hubspot.imap.protocol.message.ImapAddress;
import com.hubspot.imap.utils.parsers.fetch.EnvelopeParser;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

public class EnvelopeParserTest {

@Test
public void testParseWithAllElements() {
List<Object> envelopeResponse = Arrays.asList(
"Tue, 21 Jul 2015 16:02:13 EDT", // date
"Test Subject", // subject
Arrays.asList(List.of("John Doe", "smtp", "john.doe", "example.com")), // from
Arrays.asList(List.of("Jane Doe", "smtp", "jane.doe", "example.com")), // sender
Arrays.asList(List.of("Reply To", "smtp", "reply.to", "example.com")), // reply-to
Arrays.asList(List.of("Recipient", "smtp", "recipient", "example.com")), // to
Arrays.asList(List.of("CC", "smtp", "cc", "example.com")), // cc
Arrays.asList(List.of("BCC", "smtp", "bcc", "example.com")), // bcc
"In-Reply-To", // in-reply-to
"Message-ID" // message-id
);

Envelope envelope = EnvelopeParser.parse(envelopeResponse);

assertThat(envelope.getDateString()).isEqualTo("Tue, 21 Jul 2015 16:02:13 EDT");
assertThat(envelope.getSubject()).isEqualTo("Test Subject");
assertThat(envelope.getFrom())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getSender())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getReplyTo())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getTo())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getCc())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getBcc())
.extracting(ImapAddress::getAddress)
.containsExactly("[email protected]");
assertThat(envelope.getInReplyTo()).isEqualTo("In-Reply-To");
assertThat(envelope.getMessageId()).isEqualTo("Message-ID");
}

@Test
public void testParseWithOnly9Elements() {
List<Object> envelopeResponse = Arrays.asList(
"Tue, 21 Jul 2015 16:02:13 EDT", // date
"Test Subject", // subject
Arrays.asList(List.of("John Doe", "smtp", "john.doe", "example.com")), // from
Arrays.asList(List.of("Jane Doe", "smtp", "jane.doe", "example.com")), // sender
Arrays.asList(List.of("Reply To", "smtp", "reply.to", "example.com")), // reply-to
Arrays.asList(List.of("Recipient", "smtp", "recipient", "example.com")), // to
Arrays.asList(List.of("CC", "smtp", "cc", "example.com")), // cc
Arrays.asList(List.of("BCC", "smtp", "bcc", "example.com")), // bcc
// missing in-reply-to
"Message-ID" // message-id
);

assertThrows(
ArrayIndexOutOfBoundsException.class,
() -> {
EnvelopeParser.parse(envelopeResponse);
}
);
}
}

0 comments on commit 3459e9a

Please sign in to comment.