Add unit tests for charset and header setting in messages

This commit introduces two new unit tests: `TestMsg_Charset` and `TestMsg_SetHeader`. These tests cover the functionality of setting and retrieving the character set and headers for messages, ensuring correctness and robustness.
This commit is contained in:
Winni Neessen 2024-10-25 11:36:26 +02:00
parent 69c5f43cbf
commit a2e9dbae11
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -230,7 +230,6 @@ func TestNewMsg(t *testing.T) {
t.Errorf("NewMsg(WithNoDefaultUserAgent()) failed. Expected noDefaultUserAgent to be true, got: %t",
message.noDefaultUserAgent)
}
})
}
@ -406,6 +405,57 @@ func TestMsg_Encoding(t *testing.T) {
})
}
func TestMsg_Charset(t *testing.T) {
t.Run("Charset returns expected string", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
for _, tt := range charsetTests {
t.Run(tt.name, func(t *testing.T) {
message.SetCharset(tt.value)
if message.Charset() != tt.want.String() {
t.Errorf("failed to get charset. Expected: %s, got: %s", tt.want.String(), message.Charset())
}
})
}
})
}
func TestMsg_SetHeader(t *testing.T) {
t.Run("SetHeader on new message", func(t *testing.T) {
message := NewMsg()
if message == nil {
t.Fatal("message is nil")
}
for _, tt := range genHeaderTests {
t.Run(tt.name, func(t *testing.T) {
message.SetHeader(tt.header, "test", "foo", "bar")
values, ok := message.genHeader[tt.header]
if !ok {
t.Fatalf("failed to set header, genHeader field for %s is not set", tt.header)
}
if len(values) != 3 {
t.Fatalf("failed to set header, genHeader value count for %s is %d, want: 3",
tt.header, len(values))
}
if values[0] != "test" {
t.Errorf("failed to set header, genHeader value for %s is %s, want: %s", tt.header,
values[0], "test")
}
if values[1] != "foo" {
t.Errorf("failed to set header, genHeader value for %s is %s, want: %s", tt.header,
values[1], "foo")
}
if values[2] != "bar" {
t.Errorf("failed to set header, genHeader value for %s is %s, want: %s", tt.header,
values[1], "bar")
}
})
}
})
}
/*
// TestNewMsgWithMiddleware tests WithMiddleware