forked from goware/emailx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailx_test.go
142 lines (127 loc) · 3.74 KB
/
emailx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package emailx
import (
"fmt"
"testing"
)
func TestValidate(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
// Invalid format.
{in: "", err: true},
{in: "email@", err: true},
{in: "email@x", err: true},
{in: "email@@gmail.com", err: true},
{in: ".email@gmail.com", err: true},
{in: "email.@gmail.com", err: true},
{in: "email..test@gmail.com", err: true},
{in: ".email..test.@gmail.com", err: true},
{in: "email@at@gmail.com", err: true},
{in: "some whitespace@gmail.com", err: true},
{in: "email@whitespace gmail.com", err: true},
{in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@gmail.com", err: true},
{in: "email@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com", err: true},
// Unresolvable domain.
{in: "email+extra@wrong.example.com", err: true},
// Blocked range.
{in: "email@10.0.0.1.xip.io", err: true},
{in: "email@127.0.0.1.xip.io", err: true},
// Valid.
{in: "email@gmail.com"},
{in: "email@yahoo.com"},
{in: "email.email@gmail.com"},
{in: "email+extra@yahoo.com"},
{in: "EMAIL@aol.co.uk"},
{in: "EMAIL+EXTRA@aol.co.uk"},
}
for _, tt := range tests {
err := Validate(tt.in)
if err != nil {
if !tt.err {
t.Errorf(`"%s": unexpected error \"%v\"`, tt.in, err)
}
continue
}
if tt.err && err == nil {
t.Errorf(`"%s": expected error`, tt.in)
continue
}
}
}
func TestValidateFast(t *testing.T) {
tests := []struct {
in string
out string
err bool
}{
// Invalid format.
{in: "", err: true},
{in: "email@", err: true},
{in: "email@x", err: true},
{in: "email@@example.com", err: true},
{in: ".email@example.com", err: true},
{in: "email.@example.com", err: true},
{in: "email..test@example.com", err: true},
{in: ".email..test.@example.com", err: true},
{in: "email@at@example.com", err: true},
{in: "some whitespace@example.com", err: true},
{in: "email@whitespace example.com", err: true},
{in: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@example.com", err: true},
{in: "email@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com", err: true},
// Valid.
{in: "email@gmail.com"},
{in: "email.email@gmail.com"},
{in: "email+extra@example.com"},
{in: "EMAIL@aol.co.uk"},
{in: "EMAIL+EXTRA@aol.co.uk"},
}
for _, tt := range tests {
err := ValidateFast(tt.in)
if err != nil {
if !tt.err {
t.Errorf(`"%s": unexpected error \"%v\"`, tt.in, err)
}
continue
}
if tt.err && err == nil {
t.Errorf(`"%s": expected error`, tt.in)
continue
}
}
}
func ExampleValidate() {
err := Validate("My+Email@wrong.example.com")
if err != nil {
fmt.Println("Email is not valid.")
if err == ErrInvalidFormat {
fmt.Println("Wrong format.")
}
if err == ErrUnresolvableHost {
fmt.Println("Unresolvable host.")
}
}
// Output:
// Email is not valid.
// Unresolvable host.
}
func TestNormalize(t *testing.T) {
tests := []struct {
in string
out string
}{
{in: "email@EXAMPLE.COM. ", out: "email@example.com"},
{in: " Email+Me@example.com. ", out: "email+me@example.com"},
}
for _, tt := range tests {
normalized := Normalize(tt.in)
if normalized != tt.out {
t.Errorf(`%v: got "%v", want "%v"`, tt.in, normalized, tt.out)
}
}
}
func ExampleNormalize() {
fmt.Println(Normalize(" Email+Me@example.com. "))
// Output: email+me@example.com
}