Add test for WriteToSkipMiddleware function in Msg structure

Implement a new test for the WriteToSkipMiddleware function to validate its behavior with normal mail parts. Ensure that the function properly skips the specified middleware and writes the message to a buffer correctly.
This commit is contained in:
Winni Neessen 2024-10-28 14:58:55 +01:00
parent 4a519a3b1f
commit afa65585a0
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -5271,38 +5271,56 @@ func TestMsg_WriteTo(t *testing.T) {
}) })
} }
/* func TestMsg_WriteToSkipMiddleware(t *testing.T) {
// TestApplyMiddlewares tests the applyMiddlewares for the Msg object t.Run("WriteToSkipMiddleware with two middlewares, skipping uppercase", func(t *testing.T) {
message := NewMsg(WithMiddleware(encodeMiddleware{}), WithMiddleware(uppercaseMiddleware{}))
if message == nil {
t.Fatal("failed to create new message")
}
if err := message.From(TestSenderValid); err != nil {
t.Errorf("failed to set sender address: %s", err)
}
if err := message.To(TestRcptValid); err != nil {
t.Errorf("failed to set recipient address: %s", err)
}
message.Subject("This is a test subject")
message.SetBodyString(TypeTextPlain, "Testmail")
func TestApplyMiddlewares(t *testing.T) { buffer := bytes.NewBuffer(nil)
tests := []struct { if _, err := message.WriteToSkipMiddleware(buffer, uppercaseMiddleware{}.Type()); err != nil {
name string t.Fatalf("failed to write message with middleware to buffer: %s", err)
sub string
want string
}{
{"normal subject", "This is a test subject", "THIS IS @ TEST SUBJECT"},
{"subject with one middleware effect", "This is test subject", "THIS IS TEST SUBJECT"},
{"subject with one middleware effect", "This is A test subject", "THIS IS A TEST SUBJECT"},
} }
m := NewMsg(WithMiddleware(encodeMiddleware{}), WithMiddleware(uppercaseMiddleware{})) parsed, err := EMLToMsgFromReader(buffer)
for _, tt := range tests { if err != nil {
t.Run(tt.name, func(t *testing.T) { t.Fatalf("failed to parse message in buffer: %s", err)
m.Subject(tt.sub)
if m.genHeader[HeaderSubject] == nil {
t.Errorf("Subject() method failed in applyMiddlewares() test. Generic header for subject is empty")
return
} }
m = m.applyMiddlewares(m) checkAddrHeader(t, parsed, HeaderFrom, "WriteTo", 0, 1, TestSenderValid, "")
s, ok := m.genHeader[HeaderSubject] checkAddrHeader(t, parsed, HeaderTo, "WriteTo", 0, 1, TestRcptValid, "")
if !ok { checkGenHeader(t, parsed, HeaderSubject, "WriteTo", 0, 1, "This is @ test subject")
t.Errorf("failed to get subject header") parts := parsed.GetParts()
if len(parts) != 1 {
t.Fatalf("expected 1 parts, got: %d", len(parts))
} }
if s[0] != tt.want { if parts[0].contentType != TypeTextPlain {
t.Errorf("applyMiddlewares() method failed. Expected: %s, got: %s", tt.want, s[0]) t.Errorf("expected contentType to be %s, got: %s", TypeTextPlain, parts[0].contentType)
}
if parts[0].encoding != EncodingQP {
t.Errorf("expected encoding to be %s, got: %s", EncodingQP, parts[0].encoding)
}
messageBuf := bytes.NewBuffer(nil)
_, err = parts[0].writeFunc(messageBuf)
if err != nil {
t.Errorf("writer func failed: %s", err)
}
got := strings.TrimSpace(messageBuf.String())
if !strings.HasSuffix(got, "Testmail") {
t.Errorf("expected message buffer to contain Testmail, got: %s", got)
} }
}) })
} }
}
/*
// TestApplyMiddlewares tests the applyMiddlewares for the Msg object
// TestMsg_hasAlt tests the hasAlt() method of the Msg // TestMsg_hasAlt tests the hasAlt() method of the Msg
@ -5337,22 +5355,6 @@ func TestMsg_WriteTo(t *testing.T) {
} }
} }
// TestMsg_WriteTo tests the WriteTo() method of the Msg
func TestMsg_WriteTo(t *testing.T) {
m := NewMsg()
m.SetBodyString(TypeTextPlain, "Plain")
wbuf := bytes.Buffer{}
n, err := m.WriteTo(&wbuf)
if err != nil {
t.Errorf("WriteTo() failed: %s", err)
return
}
if n != int64(wbuf.Len()) {
t.Errorf("WriteTo() failed: expected written byte length: %d, got: %d", n, wbuf.Len())
}
}
// TestMsg_WriteToSkipMiddleware tests the WriteTo() method of the Msg // TestMsg_WriteToSkipMiddleware tests the WriteTo() method of the Msg
func TestMsg_WriteToSkipMiddleware(t *testing.T) { func TestMsg_WriteToSkipMiddleware(t *testing.T) {