-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathinbound_test.go
180 lines (160 loc) · 4.27 KB
/
inbound_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package inbound
import (
"bytes"
"fmt"
"log"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func createRequest(filename string) *http.Request {
file, err := os.ReadFile(filename)
if err != nil {
return nil
}
// Build POST request
req, _ := http.NewRequest(http.MethodPost, "", bytes.NewReader(file))
req.Header.Set("Content-Type", "multipart/form-data; boundary=xYzZY")
req.Header.Set("User-Agent", "Twilio-SendGrid-Test")
return req
}
func TestParse(t *testing.T) {
// Build a table of tests to run with each one having a name, the sample data file to post,
// and the expected HTTP response from the handler
tests := []struct {
name string
file string
expectedError error
}{
{
name: "NoAttachment",
file: "./sample_data/raw_data.txt",
},
{
name: "Attachment",
file: "./sample_data/raw_data_with_attachments.txt",
},
{
name: "DefaultData",
file: "./sample_data/default_data.txt",
},
{
name: "BadData",
file: "./sample_data/bad_data.txt",
expectedError: fmt.Errorf("multipart: NextPart: EOF"),
},
}
for _, test := range tests {
t.Run(test.name, func(subTest *testing.T) {
//Load POST body
req := createRequest(test.file)
// Invoke callback handler
email, err := Parse(req)
if test.expectedError != nil {
assert.Error(subTest, err, "expected an error to occur")
return
}
assert.NoError(subTest, err, "did NOT expect an error to occur")
from := "Example User <[email protected]>"
assert.Equalf(subTest, email.Headers["From"], from, "Expected From: %s, Got: %s", from, email.Headers["From"])
})
}
}
func ExampleParsedEmail_parseHeaders() {
headers := `
Foo: foo
Bar: baz
`
email := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
rawRequest: nil,
}
email.parseHeaders(headers)
fmt.Println(email.Headers["Foo"])
fmt.Println(email.Headers["Bar"])
// Output:
// foo
// baz
}
func ExampleParsedEmail_parseRawEmail() {
rawEmail := `
From: [email protected]
Subject: Test Email
Content-Type: multipart/mixed; boundary=TwiLIo
--TwiLIo
Content-Type: text/plain; charset=UTF-8
Hello Twilio SendGrid!
--TwiLIo
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html><body><strong>Hello Twilio SendGrid!</body></html>
--TwiLIo--
`
email := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
rawRequest: nil,
}
if err := email.parseRawEmail(rawEmail); err != nil {
log.Fatal(err)
}
for key, value := range email.Headers {
fmt.Println(key, value)
}
fmt.Println(email.Body["text/plain; charset=UTF-8"])
// Unordered Output:
// To [email protected]
// From [email protected]
// Subject Test Email
// Content-Type multipart/mixed; boundary=TwiLIo
// Hello Twilio SendGrid!
}
func TestValidate(t *testing.T) {
tests := []struct {
name string
values map[string][]string
expectedError error
}{
{
name: "MissingHeaders",
values: map[string][]string{},
expectedError: fmt.Errorf("missing DKIM and SPF score"),
},
{
name: "FailedDkim",
values: map[string][]string{"dkim": {"pass", "fail", "pass"}, "SPF": {"pass"}},
expectedError: fmt.Errorf("DKIM validation failed"),
},
{
name: "FailedSpf",
values: map[string][]string{"dkim": {"pass", "pass", "pass"}, "SPF": {"pass", "fail", "pass"}},
expectedError: fmt.Errorf("SPF validation failed"),
},
{
name: "FailedSpfandDkim",
values: map[string][]string{"dkim": {"pass", "pass", "fail"}, "SPF": {"pass", "fail", "pass"}},
expectedError: fmt.Errorf("DKIM validation failed"),
},
{
name: "success",
values: map[string][]string{"dkim": {"pass", "pass", "pass"}, "SPF": {"pass", "pass", "pass"}},
},
}
for _, test := range tests {
t.Run(test.name, func(subTest *testing.T) {
//Load POST body
email := ParsedEmail{rawValues: test.values}
err := email.Validate()
if test.expectedError != nil {
assert.EqualError(subTest, test.expectedError, err.Error())
return
}
assert.NoError(subTest, err, "did NOT expect an error to occur")
})
}
}