Skip to content

Instantly share code, notes, and snippets.

@Crazy3lf
Created March 11, 2019 08:27
Show Gist options
  • Save Crazy3lf/5cd87ae25fb21c92284b04201f1ffc5e to your computer and use it in GitHub Desktop.
Save Crazy3lf/5cd87ae25fb21c92284b04201f1ffc5e to your computer and use it in GitHub Desktop.
package email
import (
"testing"
)
func TestIsValidEmail(t *testing.T) {
testCases := []struct {
name string
email string
expected bool
}{
//valid email
{
"Valid email",
"[email protected]",
true,
},
{
"Email contains dot in the address field",
"[email protected]",
true,
}, {
"Email contains dot with subdomain",
"[email protected]",
true,
},
{
"Plus sign is considered valid character",
"[email protected]",
true,
}, {
"Valid email",
"[email protected]",
true,
},
{
"Domain is valid IP address",
"[email protected]",
true,
}, {
"Square bracket around IP address is considered valid",
"email@[123.123.123.123]",
true,
},
{
"Digits in address are valid",
"[email protected]",
true,
},
{
"Dash in domain name is valid",
"[email protected]",
true,
}, {
"Underscore in the address field is valid",
"[email protected]",
true,
},
{
".name is valid Top Level Domain name",
"[email protected]",
true,
}, {
"Dot in Top Level Domain name also considered valid (use co.jp as example here)",
"[email protected]",
true,
},
{
"Dash in address field is valid",
"[email protected]",
true,
},
// invalid email
{
"Missing @ sign and domain",
"plainaddress",
false,
},
{
"Garbage",
"#@%^%#$@#$@#.com",
false,
}, {
"Missing username",
"@domain.com",
false,
}, {
"Encoded html within email is invalid",
"Joe Smith <[email protected]>",
false,
}, {
"Missing @",
"email.domain.com ",
false,
}, {
"Two @ sign",
"email@[email protected]",
false,
}, {
"Leading dot in address is not allowed",
"[email protected]",
false,
}, {
"Trailing dot in address is not allowed",
"[email protected]",
false,
}, {
"Multiple dots",
"[email protected]",
false,
}, {
"Unicode char as address",
"あいうえお@domain.com",
false,
}, {
"Leading dash in front of domain is invalid",
"[email protected]",
false,
}, {
"Missing top level domain (.com/.net/.org/etc)",
"email@domain",
false,
}, {
"Text followed email is not allowed",
"[email protected] (Joe Smith)",
false,
}, {
"Multiple dot in the domain portion is invalid",
"[email protected]",
false,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actual := IsValidEmail(testCase.email)
if !(actual == testCase.expected) {
t.Fail()
}
})
}
}
@Crazy3lf
Copy link
Author

This list does not cover all possible email address format specified in RFC5321 and RFC5322. Also, Unicode maybe allowed as specified in RFC6531.
More info: https://www.netmeister.org/blog/email.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment