forked from Pritoj/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_test.go
128 lines (120 loc) · 2.89 KB
/
inspect_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
package enmime_test
import (
"bytes"
"io"
"io/ioutil"
"net/mail"
"net/textproto"
"strings"
"testing"
"github.com/jhillyerd/enmime"
"github.com/jhillyerd/enmime/internal/test"
)
func TestDecodeHeaders(t *testing.T) {
t.Run("rfc2047 sample", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
b, err := ioutil.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Errorf("Error decoding RFC2047 header value")
}
})
t.Run("no break between headers and content", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header-no-break.raw")
b, err := ioutil.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("To"), "Mirosław Marczak") {
t.Errorf("Error decoding RFC2047 header value")
}
})
t.Run("textproto header read error", func(t *testing.T) {
r := test.OpenTestData("low-quality", "bad-header-start.raw")
b, err := ioutil.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
_, err = enmime.DecodeHeaders(b)
switch err.(type) {
case textproto.ProtocolError:
// carry on
default:
t.Fatalf("Did return expected error: %T:%+v", err, err)
}
})
t.Run("rfc2047 recursive sample", func(t *testing.T) {
r := test.OpenTestData("mail", "qp-utf8-header-recursed.raw")
b, err := ioutil.ReadAll(r)
if err != nil {
t.Errorf("%+v", err)
}
h, err := enmime.DecodeHeaders(b)
if err != nil {
t.Errorf("%+v", err)
}
if !strings.Contains(h.Get("From"), "WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984 WirelessCaller (203) 402-5984") {
t.Errorf("Error decoding recursive RFC2047 header value")
}
})
}
func BenchmarkHumanHeadersOnly(b *testing.B) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
eml, err := ioutil.ReadAll(r)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
h, err := enmime.DecodeHeaders(eml)
if err != nil {
b.Fatal(err)
}
_, err = mail.ParseAddressList(h.Get("From"))
if err != nil {
b.Fatal(err)
}
_, err = mail.ParseAddressList(h.Get("To"))
if err != nil {
b.Fatal(err)
}
_ = h.Get("Subject")
}
}
func BenchmarkReadEnvelope(b *testing.B) {
r := test.OpenTestData("mail", "qp-utf8-header.raw")
eml, err := ioutil.ReadAll(r)
if err != nil {
b.Fatal(err)
}
reusedReader := bytes.NewReader(eml)
for i := 0; i < b.N; i++ {
env, err := enmime.ReadEnvelope(reusedReader)
if err != nil {
b.Fatal(err)
}
_, err = env.AddressList("From")
if err != nil {
b.Fatal(err)
}
_, err = env.AddressList("To")
if err != nil {
b.Fatal(err)
}
env.GetHeader("Subject")
// reset reader for next run
_, err = reusedReader.Seek(0, io.SeekStart)
if err != nil {
b.Fatal(err)
}
}
}