diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 571c95ddc98f21e48fccdabd5bed78bbaeaccc06..923630c49ce02143309c00365afcb04d2b401ac7 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -392,10 +392,9 @@ func (p *addrParser) consumePhrase() (phrase string, err error) { // We actually parse dot-atom here to be more permissive // than what RFC 5322 specifies. word, err = p.consumeAtom(true, true) - } - - if err == nil { - word, err = p.decodeRFC2047Word(word) + if err == nil { + word, err = p.decodeRFC2047Word(word) + } } if err != nil { diff --git a/src/net/mail/message_test.go b/src/net/mail/message_test.go index 624ed6b26ff56be9e1322149a42727a69f79ac35..4e718e2636727454ca901ef71aee450954988284 100644 --- a/src/net/mail/message_test.go +++ b/src/net/mail/message_test.go @@ -457,7 +457,7 @@ func TestAddressParser(t *testing.T) { } } -func TestAddressFormatting(t *testing.T) { +func TestAddressString(t *testing.T) { tests := []struct { addr *Address exp string @@ -503,11 +503,36 @@ func TestAddressFormatting(t *testing.T) { &Address{Name: "Böb, Jacöb", Address: "bob@example.com"}, `=?utf-8?b?QsO2YiwgSmFjw7Zi?= <bob@example.com>`, }, + { + &Address{Name: "=??Q?x?=", Address: "hello@world.com"}, + `"=??Q?x?=" <hello@world.com>`, + }, + { + &Address{Name: "=?hello", Address: "hello@world.com"}, + `"=?hello" <hello@world.com>`, + }, + { + &Address{Name: "world?=", Address: "hello@world.com"}, + `"world?=" <hello@world.com>`, + }, } for _, test := range tests { s := test.addr.String() if s != test.exp { t.Errorf("Address%+v.String() = %v, want %v", *test.addr, s, test.exp) + continue + } + + // Check round-trip. + if test.addr.Address != "" && test.addr.Address != "@" { + a, err := ParseAddress(test.exp) + if err != nil { + t.Errorf("ParseAddress(%#q): %v", test.exp, err) + continue + } + if a.Name != test.addr.Name || a.Address != test.addr.Address { + t.Errorf("ParseAddress(%#q) = %#v, want %#v", test.exp, a, test.addr) + } } } }