-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_test.go
99 lines (84 loc) · 2.53 KB
/
email_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
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package checker_test
import (
"testing"
"github.com/cinar/checker"
)
func ExampleIsEmail() {
err := checker.IsEmail("user@zdo.com")
if err != nil {
// Send the errors back to the user
}
}
func TestCheckEmailNonString(t *testing.T) {
defer checker.FailIfNoPanic(t)
type User struct {
Email int `checkers:"email"`
}
user := &User{}
checker.Check(user)
}
func TestCheckEmailValid(t *testing.T) {
type User struct {
Email string `checkers:"email"`
}
user := &User{
Email: "user@zdo.com",
}
_, valid := checker.Check(user)
if !valid {
t.Fail()
}
}
func TestIsEmailValid(t *testing.T) {
validEmails := []string{
"simple@example.com",
"very.common@example.com",
"disposable.style.email.with+symbol@example.com",
"other.email-with-hyphen@and.subdomains.example.com",
"fully-qualified-domain@example.com",
"user.name+tag+sorting@example.com",
"x@example.com",
"example-indeed@strange-example.com",
"test/test@test.com",
"example@s.example",
"\" \"@example.org",
"\"john..doe\"@example.org",
"mailhost!username@example.org",
"\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com",
"user%example.com@example.org",
"user-@example.org",
"postmaster@[123.123.123.123]",
"postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
}
for _, email := range validEmails {
if checker.IsEmail(email) != nil {
t.Fatal(email)
}
}
}
func TestIsEmailInvalid(t *testing.T) {
validEmails := []string{
"Abc.example.com",
"A@b@c@example.com",
"a\"b(c)d,e:f;g<h>i[j\\k]l@example.com",
"just\"not\"right@example.com",
"this is\"not\\allowed@example.com",
"this\\ still\\\"not\\\\allowed@example.com",
"1234567890123456789012345678901234567890123456789012345678901234+x@example.com",
"i_like_underscore@but_its_not_allowed_in_this_part.example.com",
"QA[icon]CHOCOLATE[icon]@test.com",
".cannot.start.with.dot@example.com",
"cannot.end.with.dot.@example.com",
"cannot.have..double.dots@example.com",
"user@domaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255characters.com",
}
for _, email := range validEmails {
if checker.IsEmail(email) == nil {
t.Fatal(email)
}
}
}