forked from jhillyerd/enmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
126 lines (110 loc) · 2.96 KB
/
error_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
package enmime
import "testing"
func TestErrorStringConversion(t *testing.T) {
e := &Error{
Name: "WarnName",
Detail: "Warn Details",
Severe: false,
}
want := "[W] WarnName: Warn Details"
got := e.String()
if got != want {
t.Error("got:", got, "want:", want)
}
e = &Error{
Name: "ErrorName",
Detail: "Error Details",
Severe: true,
}
want = "[E] ErrorName: Error Details"
got = e.String()
if got != want {
t.Error("got:", got, "want:", want)
}
}
func TestErrorAddError(t *testing.T) {
p := &Part{}
p.addError(errorMalformedHeader, "1 %v %q", 2, "three")
if len(p.Errors) != 1 {
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1")
}
e := p.Errors[0]
if e.Name != string(errorMalformedHeader) {
t.Errorf("e.Name == %q, want: %q", e.Name, errorMalformedHeader)
}
if !e.Severe {
t.Errorf("e.Severe == %v, want: true", e.Severe)
}
want := "1 2 \"three\""
if e.Detail != want {
t.Errorf("e.Detail == %q, want: %q", e.Detail, want)
}
}
func TestErrorAddWarning(t *testing.T) {
p := &Part{}
p.addWarning(errorMalformedHeader, "1 %v %q", 2, "three")
if len(p.Errors) != 1 {
t.Fatal("len(p.Errors) ==", len(p.Errors), ", want: 1")
}
e := p.Errors[0]
if e.Name != string(errorMalformedHeader) {
t.Errorf("e.Name == %q, want: %q", e.Name, errorMalformedHeader)
}
if e.Severe {
t.Errorf("e.Severe == %v, want: false", e.Severe)
}
want := "1 2 \"three\""
if e.Detail != want {
t.Errorf("e.Detail == %q, want: %q", e.Detail, want)
}
}
func TestErrorEnvelopeWarnings(t *testing.T) {
// To pass each file below must error one or more times with the specified errorName, and no
// other errorNames.
var files = []struct {
filename string
perror errorName
}{
{"bad-final-boundary.raw", errorMissingBoundary},
{"bad-header-wrap.raw", errorMalformedHeader},
{"html-only-inline.raw", errorPlainTextFromHTML},
{"missing-content-type.raw", errorMissingContentType},
{"missing-content-type2.raw", errorMissingContentType},
{"empty-header.raw", errorMissingContentType},
{"unk-encoding-part.raw", errorContentEncoding},
{"unk-charset-html-only.raw", errorCharsetConversion},
{"unk-charset-part.raw", errorCharsetConversion},
}
for _, tt := range files {
// Mail with disposition attachment
msg := openTestData("low-quality", tt.filename)
e, err := ReadEnvelope(msg)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
if len(e.Errors) == 0 {
t.Error("Got 0 warnings, expected at least one for:", tt.filename)
}
satisfied := false
for _, perr := range e.Errors {
if perr.Name == string(tt.perror) {
satisfied = true
if perr.Severe {
t.Errorf("Expected Severe to be false, got true for %q", perr.Name)
}
}
}
if !satisfied {
var errorList string
for _, perr := range e.Errors {
errorList += perr.String()
errorList += "\n"
}
t.Errorf(
"File %q should have error of type %q, got these instead:\n%s",
tt.filename,
tt.perror,
errorList)
}
}
}