-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathinbound_test.go
139 lines (122 loc) · 3.22 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
package inbound
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func createRequest(filename string) *http.Request {
file, err := ioutil.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")
} else {
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 TestAttachments(t *testing.T) {
req := createRequest("./sample_data/raw_data_with_attachments.txt")
email := Parse(req)
contentType := "image/jpeg"
assert.Equalf(t, contentType, email.Attachments[0].ContentType,"Expected From: %s, Got: %s", contentType, email.Attachments[0].ContentType)
}
func ExampleParsedEmail_parseHeaders() {
headers := `
Foo: foo
Bar: baz
`
email := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: []ParsedAttachment{},
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: []ParsedAttachment{},
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!
}