From a2e9dbae11deb1e7c4a08c8938b0f91809ffe11e Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Fri, 25 Oct 2024 11:36:26 +0200 Subject: [PATCH] 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. --- msg_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/msg_test.go b/msg_test.go index 85f94b5..c162947 100644 --- a/msg_test.go +++ b/msg_test.go @@ -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