mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
message: Prefer To header for BeforeMessageStored event (#446)
Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -76,10 +76,12 @@ func (s *StoreManager) Deliver(
|
|||||||
|
|
||||||
// Process inbound message through extensions.
|
// Process inbound message through extensions.
|
||||||
mailboxes := make([]string, len(recipients))
|
mailboxes := make([]string, len(recipients))
|
||||||
toAddrs := make([]mail.Address, len(recipients))
|
|
||||||
for i, recip := range recipients {
|
for i, recip := range recipients {
|
||||||
mailboxes[i] = recip.Mailbox
|
mailboxes[i] = recip.Mailbox
|
||||||
toAddrs[i] = recip.Address
|
}
|
||||||
|
toAddrs := make([]mail.Address, len(toaddr))
|
||||||
|
for i, addr := range toaddr {
|
||||||
|
toAddrs[i] = *addr
|
||||||
}
|
}
|
||||||
|
|
||||||
inbound := &event.InboundMessage{
|
inbound := &event.InboundMessage{
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ func TestDeliverRespectsRecipientPolicy(t *testing.T) {
|
|||||||
assertMessageCount(t, sm, "u2@example.com", 1)
|
assertMessageCount(t, sm, "u2@example.com", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeliverEmitsBeforeMessageStoredEvent(t *testing.T) {
|
func TestDeliverEmitsBeforeMessageStoredEventToHeader(t *testing.T) {
|
||||||
sm, extHost := testStoreManager()
|
sm, extHost := testStoreManager()
|
||||||
|
|
||||||
// Register function to receive event.
|
// Register function to receive event.
|
||||||
@@ -118,7 +118,47 @@ func TestDeliverEmitsBeforeMessageStoredEvent(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// Deliver a message to trigger event.
|
// Deliver a message to trigger event, To header differs from RCPT TO.
|
||||||
|
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
|
||||||
|
recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com")
|
||||||
|
recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com")
|
||||||
|
if err := sm.Deliver(
|
||||||
|
origin,
|
||||||
|
[]*policy.Recipient{recip1, recip2},
|
||||||
|
"Received: xyz\n",
|
||||||
|
[]byte(`From: from@example.com
|
||||||
|
To: u1@example.com, u3@external.com
|
||||||
|
Subject: tsub
|
||||||
|
|
||||||
|
test email`),
|
||||||
|
); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NotNil(t, got, "BeforeMessageStored listener did not receive InboundMessage")
|
||||||
|
assert.Equal(t, []string{"u1@example.com", "u2@example.com"}, got.Mailboxes, "Mailboxes not equal")
|
||||||
|
assert.Equal(t, mail.Address{Name: "", Address: "from@example.com"}, got.From, "From not equal")
|
||||||
|
assert.Equal(t, []mail.Address{
|
||||||
|
{Name: "", Address: "u1@example.com"},
|
||||||
|
{Name: "", Address: "u3@external.com"},
|
||||||
|
}, got.To, "To not equal")
|
||||||
|
assert.Equal(t, "tsub", got.Subject, "Subject not equal")
|
||||||
|
assert.Equal(t, int64(84), got.Size, "Size not equal")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeliverEmitsBeforeMessageStoredEventRcptTo(t *testing.T) {
|
||||||
|
sm, extHost := testStoreManager()
|
||||||
|
|
||||||
|
// Register function to receive event.
|
||||||
|
var got *event.InboundMessage
|
||||||
|
extHost.Events.BeforeMessageStored.AddListener(
|
||||||
|
"test",
|
||||||
|
func(msg event.InboundMessage) *event.InboundMessage {
|
||||||
|
got = &msg
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
// Deliver a message to trigger event, lacks To header.
|
||||||
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
|
origin, _ := sm.AddrPolicy.ParseOrigin("from@example.com")
|
||||||
recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com")
|
recip1, _ := sm.AddrPolicy.NewRecipient("u1@example.com")
|
||||||
recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com")
|
recip2, _ := sm.AddrPolicy.NewRecipient("u2@example.com")
|
||||||
|
|||||||
Reference in New Issue
Block a user