Compare commits

..

6 commits

Author SHA1 Message Date
21d7b367bd
Update user agent test in msg_test.go
Updated the test "check default user agent" in `msg_test.go` to reflect dynamic versioning. The wantUserAgent field now uses fmt.Sprintf to combine the go-mail version dynamically, improving the accuracy of testing.
2024-02-26 21:05:52 +01:00
180f6f3a63
Add a new test for no default user agent in msg_test.go
A new test function named "TestNewMsgWithNoDefaultUserAgent" has been added in `msg_test.go` file. This function is meant to test 'NewMsg' function with 'WithNoDefaultUserAgent' parameter. The addition is devised to enhance the test coverage and ensure the noDefaultUserAgent field is functioning correctly.
2024-02-26 21:03:20 +01:00
c0cf31b6c4
Merge branch 'main' into feature/179_refactor-variable-names-for-readability 2024-02-26 20:57:37 +01:00
5c143cb74c
Merge pull request #178 from gegorov2030/no-default-user-agent
Add an option to skip adding a User-Agent
2024-02-26 20:56:50 +01:00
grigorii
299490f8fa Add an option to skip adding a User-Agent (tests) 2024-02-26 10:30:33 -08:00
grigorii
a24906d185 Add an option to skip adding a User-Agent 2024-02-23 14:41:58 -08:00
2 changed files with 71 additions and 0 deletions

13
msg.go
View file

@ -117,6 +117,9 @@ type Msg struct {
// sendError holds the SendError in case a Msg could not be delivered during the Client.Send operation
sendError error
// noDefaultUserAgent indicates whether the default User Agent will be excluded for the Msg when it's sent.
noDefaultUserAgent bool
}
// SendmailPath is the default system path to the sendmail binary
@ -192,6 +195,13 @@ func WithPGPType(pt PGPType) MsgOption {
}
}
// WithNoDefaultUserAgent configures the Msg to not use the default User Agent
func WithNoDefaultUserAgent() MsgOption {
return func(m *Msg) {
m.noDefaultUserAgent = true
}
}
// SetCharset sets the encoding charset of the Msg
func (m *Msg) SetCharset(c Charset) {
m.charset = c
@ -1181,6 +1191,9 @@ func (m *Msg) setEncoder() {
// checkUserAgent checks if a useragent/x-mailer is set and if not will set a default
// version string
func (m *Msg) checkUserAgent() {
if m.noDefaultUserAgent {
return
}
_, uaok := m.genHeader[HeaderUserAgent]
_, xmok := m.genHeader[HeaderXMailer]
if !uaok && !xmok {

View file

@ -3161,3 +3161,61 @@ func TestMsg_BccFromString(t *testing.T) {
})
}
}
// TestMsg_checkUserAgent tests the checkUserAgent method of the Msg
func TestMsg_checkUserAgent(t *testing.T) {
tests := []struct {
name string
noDefaultUserAgent bool
genHeader map[Header][]string
wantUserAgent string
sf bool
}{
{
name: "check default user agent",
noDefaultUserAgent: false,
wantUserAgent: fmt.Sprintf("go-mail v%s // https://github.com/wneessen/go-mail", VERSION),
sf: false,
},
{
name: "check no default user agent",
noDefaultUserAgent: true,
wantUserAgent: "",
sf: true,
},
{
name: "check if ua and xm is already set",
noDefaultUserAgent: false,
genHeader: map[Header][]string{
HeaderUserAgent: {"custom UA"},
HeaderXMailer: {"custom XM"},
},
wantUserAgent: "custom UA",
sf: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg := &Msg{
noDefaultUserAgent: tt.noDefaultUserAgent,
genHeader: tt.genHeader,
}
msg.checkUserAgent()
gotUserAgent := ""
if val, ok := msg.genHeader[HeaderUserAgent]; ok {
gotUserAgent = val[0] // Assuming the first one is the needed value
}
if gotUserAgent != tt.wantUserAgent && !tt.sf {
t.Errorf("UserAgent got = %v, want = %v", gotUserAgent, tt.wantUserAgent)
}
})
}
}
// TestNewMsgWithMIMEVersion tests WithMIMEVersion and Msg.SetMIMEVersion
func TestNewMsgWithNoDefaultUserAgent(t *testing.T) {
m := NewMsg(WithNoDefaultUserAgent())
if m.noDefaultUserAgent != true {
t.Errorf("WithNoDefaultUserAgent() failed. Expected: %t, got: %t", true, false)
}
}