-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add more lenient address list parsing for MySQL lookup
- Loading branch information
Showing
5 changed files
with
93 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package srsmilter | ||
|
||
import ( | ||
"net/mail" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func split(email string) (string, Domain) { | ||
at := strings.LastIndexByte(email, '@') | ||
if at < 0 { | ||
return email, "" | ||
} | ||
return email[:at], ToDomain(email[at+1:]) | ||
} | ||
|
||
func parseAddressList(in string) (list []*mail.Address, err error) { | ||
parts := strings.FieldsFunc(in, func(r rune) bool { | ||
return r == ',' || unicode.IsSpace(r) || r == '\r' || r == '\n' | ||
}) | ||
for _, p := range parts { | ||
if len(p) == 0 { | ||
continue | ||
} | ||
if a, err := mail.ParseAddress(p); err != nil { | ||
return nil, err | ||
} else { | ||
list = append(list, a) | ||
} | ||
} | ||
return list, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package srsmilter | ||
|
||
import ( | ||
"net/mail" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_split(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
email string | ||
wantLocal string | ||
wantDomain Domain | ||
}{ | ||
{"empty", "", "", ""}, | ||
{"without domain", "root", "root", ""}, | ||
{"with domain", "root@localhost", "root", ToDomain("localhost")}, | ||
{"with two ats", "root@crazy@localhost", "root@crazy", ToDomain("localhost")}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotLocal, gotDomain := split(tt.email) | ||
if gotLocal != tt.wantLocal { | ||
t.Errorf("split() gotLocal = %v, want %v", gotLocal, tt.wantLocal) | ||
} | ||
if gotDomain != tt.wantDomain { | ||
t.Errorf("split() gotDomain = %v, want %v", gotDomain, tt.wantDomain) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_parseAddressList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
wantList []*mail.Address | ||
wantErr bool | ||
}{ | ||
{"empty", "", nil, false}, | ||
{"one", "root@localhost", []*mail.Address{{Address: "root@localhost"}}, false}, | ||
{"padded", " ,,,root@localhost\n,", []*mail.Address{{Address: "root@localhost"}}, false}, | ||
{"two", " ,,,root@localhost\n,[email protected]\r\r\n", []*mail.Address{{Address: "root@localhost"}, {Address: "[email protected]"}}, false}, | ||
{"broken", " ,,,root@localhost\n,[email protected]\rbroken\r\n", nil, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
gotList, err := parseAddressList(tt.in) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("parseAddressList() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(gotList, tt.wantList) { | ||
t.Errorf("parseAddressList() gotList = %v, want %v", gotList, tt.wantList) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters