-
Notifications
You must be signed in to change notification settings - Fork 6
/
mail_test.go
78 lines (68 loc) · 1.81 KB
/
mail_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestValidation(t *testing.T) {
handler := http.Handler(NewHandler("localhost", "[email protected]"))
var tests = []struct {
url string
code int
body string
}{
{"/[email protected]", 200, ""},
{"/?invalid=reqeust", 400, ""},
{"/?email=invalid", 200, "invalid email: mail: missing phrase"},
{"/[email protected]", 200, "MX lookup failed for example.com"},
}
for _, test := range tests {
req, _ := http.NewRequest("GET", test.url, nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
assert.Equal(t, test.code, recorder.Code)
assert.Contains(t, recorder.Body.String(), test.body)
}
}
func TestValidationWithCallback(t *testing.T) {
log.SetLevel(log.DebugLevel)
handler := http.Handler(NewHandler("localhost", "[email protected]"))
callbackBody := make(chan string, 1)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
body, err := ioutil.ReadAll(r.Body)
assert.NoError(t, err)
callbackBody <- string(body)
}))
defer ts.Close()
var tests = []struct {
url string
code int
body string
}{
{
"/[email protected]&callback=" + ts.URL,
201,
`"is_valid":true`,
},
{
fmt.Sprintf("/?email=invalid-email%[email protected]&callback=%s", time.Now().Unix(), ts.URL),
201,
`"is_valid":false`,
},
}
for _, test := range tests {
req, _ := http.NewRequest("POST", test.url, nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
assert.Equal(t, test.code, recorder.Code)
// Waiting for callback
body := <-callbackBody
assert.Contains(t, body, test.body)
}
}