-
Notifications
You must be signed in to change notification settings - Fork 102
/
options_test.go
92 lines (79 loc) · 2.7 KB
/
options_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
package enmime
import (
"fmt"
"strings"
"testing"
"github.com/jhillyerd/enmime/v2/mediatype"
)
func TestSetCustomParseMediaType(t *testing.T) {
alwaysReturnHTML := func(_ string) (mtype string, params map[string]string, invalidParams []string, err error) {
return "text/html", nil, nil, err
}
changeAndUtilizeDefault := func(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
modifiedStr := strings.ReplaceAll(ctype, "application/Pamir Viewer", "application/PamirViewer")
return mediatype.Parse(modifiedStr)
}
tcases := []struct {
ctype string
want string
customParseMediaType CustomParseMediaType
}{
{
ctype: "text/plain",
want: "text/plain",
customParseMediaType: nil,
},
{
ctype: "text/plain",
want: "text/html",
customParseMediaType: alwaysReturnHTML,
},
{
ctype: "text/plain; charset=utf-8",
want: "text/html",
customParseMediaType: alwaysReturnHTML,
},
{
ctype: "application/Pamir Viewer; name=\"2023-384.pmrv\"",
want: "application/pamirviewer",
customParseMediaType: changeAndUtilizeDefault,
},
}
for _, tcase := range tcases {
p := &Part{parser: NewParser(SetCustomParseMediaType(tcase.customParseMediaType))}
got, _, _, _ := p.parseMediaType(tcase.ctype)
if got != tcase.want {
t.Errorf("Parser.parseMediaType(%v) == %v, want: %v",
tcase.ctype, got, tcase.want)
}
}
}
func ExampleSetCustomParseMediaType() {
// for the sake of simplicity replaces space in a very specific invalid content-type: "application/Pamir Viewer"
replaceSpecificContentType := func(ctype string) (mtype string, params map[string]string, invalidParams []string, err error) {
modifiedStr := strings.ReplaceAll(ctype, "application/Pamir Viewer", "application/PamirViewer")
return mediatype.Parse(modifiedStr)
}
invalidMessageContent := `From: <[email protected]>
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_000F_01D9FAC6.09EB3B60"
------=_NextPart_000_000F_01D9FAC6.09EB3B60
Content-Type: application/Pamir Viewer;
name="2023-10-13.pmrv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="2023-10-13.pmrv"
f6En7vFpNql3tfMkoKABP1iBEf+M/qF6LCAIvyRbpH6uDCqcKKGmH3e6OiqN5eCfqUk=
`
p := NewParser(SetCustomParseMediaType(replaceSpecificContentType))
e, err := p.ReadEnvelope(strings.NewReader(invalidMessageContent))
fmt.Println(err)
fmt.Println(len(e.Attachments))
fmt.Println(e.Attachments[0].ContentType)
fmt.Println(e.Attachments[0].FileName)
// Output:
// <nil>
// 1
// application/pamirviewer
// 2023-10-13.pmrv
}