-
Notifications
You must be signed in to change notification settings - Fork 145
/
mock_validation.go
84 lines (74 loc) · 2.29 KB
/
mock_validation.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
package mailgun
import (
"net/http"
"net/mail"
"strings"
"github.com/go-chi/chi/v5"
)
func (ms *mockServer) addValidationRoutes(r chi.Router) {
r.Get("/v3/address/validate", ms.validateEmail)
r.Get("/v3/address/parse", ms.parseEmail)
r.Get("/v3/address/private/validate", ms.validateEmail)
r.Get("/v3/address/private/parse", ms.parseEmail)
r.Get("/v4/address/validate", ms.validateEmailV4)
}
func (ms *mockServer) validateEmailV4(w http.ResponseWriter, r *http.Request) {
if r.FormValue("address") == "" {
w.WriteHeader(http.StatusBadRequest)
toJSON(w, okResp{Message: "'address' parameter is required"})
return
}
var results v4EmailValidationResp
parts, err := mail.ParseAddress(r.FormValue("address"))
if err == nil {
results.IsValid = true
results.Parts.Domain = strings.Split(parts.Address, "@")[1]
results.Parts.LocalPart = strings.Split(parts.Address, "@")[0]
results.Parts.DisplayName = parts.Name
}
results.Reason = []string{"no-reason"}
results.Risk = "unknown"
results.Result = "deliverable"
results.Engagement = &EngagementData{
Engaging: false,
Behavior: "disengaged",
IsBot: false,
}
toJSON(w, results)
}
func (ms *mockServer) validateEmail(w http.ResponseWriter, r *http.Request) {
if r.FormValue("address") == "" {
w.WriteHeader(http.StatusBadRequest)
toJSON(w, okResp{Message: "'address' parameter is required"})
return
}
var results EmailVerification
parts, err := mail.ParseAddress(r.FormValue("address"))
if err == nil {
results.IsValid = true
results.Parts.Domain = strings.Split(parts.Address, "@")[1]
results.Parts.LocalPart = strings.Split(parts.Address, "@")[0]
results.Parts.DisplayName = parts.Name
}
results.Reason = "no-reason"
results.Risk = "unknown"
toJSON(w, results)
}
func (ms *mockServer) parseEmail(w http.ResponseWriter, r *http.Request) {
if r.FormValue("addresses") == "" {
w.WriteHeader(http.StatusBadRequest)
toJSON(w, okResp{Message: "'addresses' parameter is required"})
return
}
addresses := strings.Split(r.FormValue("addresses"), ",")
var results addressParseResult
for _, address := range addresses {
_, err := mail.ParseAddress(address)
if err != nil {
results.Unparseable = append(results.Unparseable, address)
} else {
results.Parsed = append(results.Parsed, address)
}
}
toJSON(w, results)
}