forked from emersion/go-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_test.go
161 lines (130 loc) · 3.88 KB
/
entity_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
package message
import (
"bytes"
"io"
"io/ioutil"
"strings"
"testing"
)
func testMakeEntity() *Entity {
h := make(Header)
h.Set("Content-Type", "text/plain; charset=US-ASCII")
h.Set("Content-Transfer-Encoding", "base64")
r := strings.NewReader("Y2Mgc2F2YQ==")
return NewEntity(h, r)
}
func TestNewEntity(t *testing.T) {
e := testMakeEntity()
expected := "cc sava"
if b, err := ioutil.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading entity body, got", err)
} else if s := string(b); s != expected {
t.Errorf("Expected %q as entity body but got %q", expected, s)
}
}
func testMakeMultipart() *Entity {
h1 := make(Header)
h1.Set("Content-Type", "text/plain")
r1 := strings.NewReader("Text part")
e1 := NewEntity(h1, r1)
h2 := make(Header)
h2.Set("Content-Type", "text/html")
r2 := strings.NewReader("<p>HTML part</p>")
e2 := NewEntity(h2, r2)
h := make(Header)
h.Set("Content-Type", "multipart/alternative; boundary=IMTHEBOUNDARY")
return NewMultipart(h, []*Entity{e1, e2})
}
const testMultipartHeader = "Content-Type: multipart/alternative; boundary=IMTHEBOUNDARY\r\n" +
"\r\n"
const testMultipartBody = "--IMTHEBOUNDARY\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"Text part\r\n" +
"--IMTHEBOUNDARY\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<p>HTML part</p>\r\n" +
"--IMTHEBOUNDARY--\r\n"
var testMultipartText = testMultipartHeader + testMultipartBody
func testMultipart(t *testing.T, e *Entity) {
mr := e.MultipartReader()
if mr == nil {
t.Fatalf("Expected MultipartReader not to return nil")
}
defer mr.Close()
i := 0
for {
p, err := mr.NextPart()
if err == io.EOF {
break
} else if err != nil {
t.Fatal("Expected no error while reading multipart entity, got", err)
}
var expectedType string
var expectedBody string
switch i {
case 0:
expectedType = "text/plain"
expectedBody = "Text part"
case 1:
expectedType = "text/html"
expectedBody = "<p>HTML part</p>"
}
if mediaType := p.Header.Get("Content-Type"); mediaType != expectedType {
t.Errorf("Expected part Content-Type to be %q, got %q", expectedType, mediaType)
}
if b, err := ioutil.ReadAll(p.Body); err != nil {
t.Error("Expected no error while reading part body, got", err)
} else if s := string(b); s != expectedBody {
t.Errorf("Expected %q as part body but got %q", expectedBody, s)
}
i++
}
if i != 2 {
t.Fatalf("Expected multipart entity to contain exactly 2 parts, got %v", i)
}
}
func TestNewMultipart(t *testing.T) {
testMultipart(t, testMakeMultipart())
}
func TestNewMultipart_read(t *testing.T) {
e := testMakeMultipart()
if b, err := ioutil.ReadAll(e.Body); err != nil {
t.Error("Expected no error while reading multipart body, got", err)
} else if s := string(b); s != testMultipartBody {
t.Errorf("Expected %q as multipart body but got %q", testMultipartBody, s)
}
}
func TestRead_multipart(t *testing.T) {
e, err := Read(strings.NewReader(testMultipartText))
if err != nil {
t.Fatal("Expected no error while reading multipart, got", err)
}
testMultipart(t, e)
}
func TestEntity_WriteTo(t *testing.T) {
e := testMakeEntity()
e.Header.SetContentType("text/plain", map[string]string{"charset": "utf-8"})
e.Header.Del("Content-Transfer-Encoding")
var b bytes.Buffer
if err := e.WriteTo(&b); err != nil {
t.Fatal("Expected no error while writing entity, got", err)
}
expected := "Content-Type: text/plain; charset=utf-8\r\n" +
"\r\n" +
"cc sava"
if s := b.String(); s != expected {
t.Errorf("Expected written entity to be:\n%s\nbut got:\n%s", expected, s)
}
}
func TestEntity_WriteTo_multipart(t *testing.T) {
e := testMakeMultipart()
var b bytes.Buffer
if err := e.WriteTo(&b); err != nil {
t.Fatal("Expected no error while writing entity, got", err)
}
if s := b.String(); s != testMultipartText {
t.Errorf("Expected written entity to be:\n%s\nbut got:\n%s", testMultipartText, s)
}
}