forked from emersion/go-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_test.go
117 lines (107 loc) · 4.88 KB
/
header_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
package message
import (
"reflect"
"testing"
)
func TestHeader(t *testing.T) {
mediaType := "text/plain"
mediaParams := map[string]string{"charset": "utf-8"}
desc := "Plan de complémentarité de l'Homme"
disp := "attachment"
dispParams := map[string]string{"filename": "complémentarité.txt"}
h := make(Header)
h.SetContentType(mediaType, mediaParams)
h.SetContentDescription(desc)
h.SetContentDisposition(disp, dispParams)
if gotMediaType, gotParams, err := h.ContentType(); err != nil {
t.Error("Expected no error when parsing content type, but got:", err)
} else if gotMediaType != mediaType {
t.Errorf("Expected media type %q but got %q", mediaType, gotMediaType)
} else if !reflect.DeepEqual(gotParams, mediaParams) {
t.Errorf("Expected media params %v but got %v", mediaParams, gotParams)
}
if gotDesc, err := h.ContentDescription(); err != nil {
t.Error("Expected no error when parsing content description, but got:", err)
} else if gotDesc != desc {
t.Errorf("Expected content description %q but got %q", desc, gotDesc)
}
if gotDisp, gotParams, err := h.ContentDisposition(); err != nil {
t.Error("Expected no error when parsing content disposition, but got:", err)
} else if gotDisp != disp {
t.Errorf("Expected disposition %q but got %q", disp, gotDisp)
} else if !reflect.DeepEqual(gotParams, dispParams) {
t.Errorf("Expected disposition params %v but got %v", dispParams, gotParams)
}
}
var formatHeaderFieldTests = []struct {
k, v string
formatted string
}{
{
k: "From",
v: "Mitsuha Miyamizu <[email protected]>",
formatted: "From: Mitsuha Miyamizu <[email protected]>\r\n",
},
{
k: "Subject",
v: "This is a very long subject, much longer than just the 76 characters limit that applies to message header fields",
formatted: "Subject: This is a very long subject, much longer than just the 76\r\n characters limit that applies to message header fields\r\n",
},
{
k: "Subject",
v: "This is yet \t another subject \t with many whitespace characters",
formatted: "Subject: This is yet \t another subject \t \r\n with many whitespace characters\r\n",
},
{
k: "Subject",
v: "=?utf-8?q?=E2=80=9CDeveloper_reads_customer_requested_change.=E2=80=9D=0A?= =?utf-8?q?=0ACaravaggio=0A=0AOil_on...?=",
formatted: "Subject: =?utf-8?q?=E2=80=9CDeveloper_reads_customer_requested_change.\r\n =E2=80=9D=0A?= =?utf-8?q?=0ACaravaggio=0A=0AOil_on...?=\r\n",
},
{
k: "Subject",
v: "=?utf-8?q?=E2=80=9CShort subject=E2=80=9D=0A?= =?utf-8?q?=0AAuthor=0A=0AOil_on...?=",
formatted: "Subject: =?utf-8?q?=E2=80=9CShort subject=E2=80=9D=0A?= =?utf-8?q?\r\n =0AAuthor=0A=0AOil_on...?=\r\n",
},
{
k: "Subject",
v: "=?utf-8?q?=E2=80=9CVery long subject very long subject very long subject very long subject=E2=80=9D=0A?= =?utf-8?q?=0ALong second part of subject long second part of subject long second part of subject long subject=0A=0AOil_on...?=",
formatted: "Subject: =?utf-8?q?=E2=80=9CVery long subject very long subject very long\r\n subject very long subject=E2=80=9D=0A?= =?utf-8?q?=0ALong second part of\r\n subject long second part of subject long second part of subject long\r\n subject=0A=0AOil_on...?=\r\n",
},
{
k: "DKIM-Signature",
v: "v=1;\r\n h=From:To:Reply-To:Subject:Message-ID:References:In-Reply-To:MIME-Version;\r\n d=example.org\r\n",
formatted: "DKIM-Signature: v=1;\r\n h=From:To:Reply-To:Subject:Message-ID:References:In-Reply-To:MIME-Version;\r\n d=example.org\r\n",
},
{
k: "DKIM-Signature",
v: "v=1; h=From; d=example.org; b=AuUoFEfDxTDkHlLXSZEpZj79LICEps6eda7W3deTVFOk4yAUoqOB4nujc7YopdG5dWLSdNg6xNAZpOPr+kHxt1IrE+NahM6L/LbvaHutKVdkLLkpVaVVQPzeRDI009SO2Il5Lu7rDNH6mZckBdrIx0orEtZV4bmp/YzhwvcubU4=\r\n",
formatted: "DKIM-Signature: v=1; h=From; d=example.org;\r\n b=AuUoFEfDxTDkHlLXSZEpZj79LICEps6eda7W3deTVFOk4yAUoqOB4nujc7YopdG5dWLSdNg6x\r\n NAZpOPr+kHxt1IrE+NahM6L/LbvaHutKVdkLLkpVaVVQPzeRDI009SO2Il5Lu7rDNH6mZckBdrI\r\n x0orEtZV4bmp/YzhwvcubU4=\r\n",
},
{
k: "Bcc",
v: "",
formatted: "Bcc: \r\n",
},
{
k: "Bcc",
v: " ",
formatted: "Bcc: \r\n",
},
}
func TestFormatHeaderField(t *testing.T) {
for _, test := range formatHeaderFieldTests {
s := formatHeaderField(test.k, test.v)
if s != test.formatted {
t.Errorf("Expected formatted header to be %q but got %q", test.formatted, s)
}
}
}
func TestEmptyContentType(t *testing.T) {
h := make(Header)
mediaType := "text/plain"
if gotMediaType, _, err := h.ContentType(); err != nil {
t.Error("Expected no error when parsing empty content type, but got:", err)
} else if gotMediaType != mediaType {
t.Errorf("Expected media type %q but got %q", mediaType, gotMediaType)
}
}