pop3_test.go

  1package pop3
  2
  3import (
  4	"testing"
  5
  6	"github.com/emersion/go-message"
  7	pop3client "github.com/knadh/go-pop3"
  8)
  9
 10func TestEntityToEmail_ReplyTo(t *testing.T) {
 11	tests := []struct {
 12		name          string
 13		replyToHeader string
 14		wantReplyTo   []string
 15	}{
 16		{
 17			name:          "single bare address",
 18			replyToHeader: "alice@example.com",
 19			wantReplyTo:   []string{"alice@example.com"},
 20		},
 21		{
 22			name:          "single address with display name",
 23			replyToHeader: "Alice Smith <alice@example.com>",
 24			wantReplyTo:   []string{"alice@example.com"},
 25		},
 26		{
 27			name:          "display name with comma",
 28			replyToHeader: `"Doe, John" <john@example.com>`,
 29			wantReplyTo:   []string{"john@example.com"},
 30		},
 31		{
 32			name:          "multiple addresses",
 33			replyToHeader: "alice@example.com, bob@example.com",
 34			wantReplyTo:   []string{"alice@example.com", "bob@example.com"},
 35		},
 36		{
 37			name:          "multiple addresses with display names containing commas",
 38			replyToHeader: `"Doe, John" <john@example.com>, "Smith, Jane" <jane@example.com>`,
 39			wantReplyTo:   []string{"john@example.com", "jane@example.com"},
 40		},
 41		{
 42			name:          "empty reply-to",
 43			replyToHeader: "",
 44			wantReplyTo:   nil,
 45		},
 46	}
 47
 48	for _, tt := range tests {
 49		t.Run(tt.name, func(t *testing.T) {
 50			var header message.Header
 51			header.Set("From", "sender@example.com")
 52			header.Set("Subject", "Test")
 53			if tt.replyToHeader != "" {
 54				header.Set("Reply-To", tt.replyToHeader)
 55			}
 56
 57			msgInfo := pop3client.MessageID{ID: 1, UID: "test-uid"}
 58			email := entityToEmail(&header, msgInfo, "test-account")
 59
 60			if len(email.ReplyTo) != len(tt.wantReplyTo) {
 61				t.Fatalf("ReplyTo length = %d, want %d; got %v", len(email.ReplyTo), len(tt.wantReplyTo), email.ReplyTo)
 62			}
 63			for i, want := range tt.wantReplyTo {
 64				if email.ReplyTo[i] != want {
 65					t.Errorf("ReplyTo[%d] = %q, want %q", i, email.ReplyTo[i], want)
 66				}
 67			}
 68		})
 69	}
 70}
 71
 72func TestEntityToEmail_To(t *testing.T) {
 73	tests := []struct {
 74		name     string
 75		toHeader string
 76		wantTo   []string
 77	}{
 78		{
 79			name:     "display name with comma",
 80			toHeader: `"Doe, John" <john@example.com>`,
 81			wantTo:   []string{"john@example.com"},
 82		},
 83		{
 84			name:     "multiple addresses with display names",
 85			toHeader: `"Doe, John" <john@example.com>, Alice <alice@example.com>`,
 86			wantTo:   []string{"john@example.com", "alice@example.com"},
 87		},
 88	}
 89
 90	for _, tt := range tests {
 91		t.Run(tt.name, func(t *testing.T) {
 92			var header message.Header
 93			header.Set("From", "sender@example.com")
 94			header.Set("To", tt.toHeader)
 95
 96			msgInfo := pop3client.MessageID{ID: 1, UID: "test-uid"}
 97			email := entityToEmail(&header, msgInfo, "test-account")
 98
 99			if len(email.To) != len(tt.wantTo) {
100				t.Fatalf("To length = %d, want %d; got %v", len(email.To), len(tt.wantTo), email.To)
101			}
102			for i, want := range tt.wantTo {
103				if email.To[i] != want {
104					t.Errorf("To[%d] = %q, want %q", i, email.To[i], want)
105				}
106			}
107		})
108	}
109}