Skip to content

Commit

Permalink
Move Part and Envelope tests into enmime_test package
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillyerd committed Jan 8, 2018
1 parent d4e73ad commit 9d33ba6
Show file tree
Hide file tree
Showing 11 changed files with 497 additions and 522 deletions.
2 changes: 1 addition & 1 deletion base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (bc *base64Cleaner) Read(p []byte) (n int, err error) {
case -1:
// Strip these, but warn the client
bc.Errors = append(bc.Errors, Error{
Name: string(errorMalformedBase64),
Name: ErrorMalformedBase64,
Detail: fmt.Sprintf("Unexpected %q in Base64 stream", buf[i]),
Severe: false,
})
Expand Down
4 changes: 2 additions & 2 deletions base64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func TestBase64CleanerErrors(t *testing.T) {
t.Fatal(err)
}
if len(cleaner.Errors) == 1 {
if cleaner.Errors[0].Name != string(errorMalformedBase64) {
t.Errorf("got: %q, want: %q", cleaner.Errors[0].Name, errorMalformedBase64)
if cleaner.Errors[0].Name != ErrorMalformedBase64 {
t.Errorf("got: %q, want: %q", cleaner.Errors[0].Name, ErrorMalformedBase64)
}
} else {
t.Errorf("got %d Errors, wanted 1", len(cleaner.Errors))
Expand Down
118 changes: 113 additions & 5 deletions detect_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package enmime

import "testing"
import (
"net/textproto"
"os"
"path/filepath"
"testing"
)

func TestDetectSinglePart(t *testing.T) {
r := openTestData("mail", "non-mime.raw")
r, _ := os.Open(filepath.Join("testdata", "mail", "non-mime.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
Expand All @@ -15,7 +20,7 @@ func TestDetectSinglePart(t *testing.T) {
}

func TestDetectMultiPart(t *testing.T) {
r := openTestData("mail", "html-mime-inline.raw")
r, _ := os.Open(filepath.Join("testdata", "mail", "html-mime-inline.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
Expand All @@ -27,7 +32,7 @@ func TestDetectMultiPart(t *testing.T) {
}

func TestDetectUnknownMultiPart(t *testing.T) {
r := openTestData("mail", "unknown-part-type.raw")
r, _ := os.Open(filepath.Join("testdata", "mail", "unknown-part-type.raw"))
msg, err := ReadParts(r)
if err != nil {
t.Fatal(err)
Expand All @@ -48,7 +53,7 @@ func TestDetectBinaryBody(t *testing.T) {
{filename: "attachment-only-no-disposition.raw", disposition: "none"},
}
for _, tt := range ttable {
r := openTestData("mail", tt.filename)
r, _ := os.Open(filepath.Join("testdata", "mail", tt.filename))
root, err := ReadParts(r)
if err != nil {
t.Fatal(err)
Expand All @@ -59,3 +64,106 @@ func TestDetectBinaryBody(t *testing.T) {
}
}
}

func TestDetectAttachmentHeader(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
}{
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"ATTACHMENT; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"image/jpg; name=\"test.jpg\""},
"Content-Disposition": []string{"attachment; filename=\"test.jpg\""},
},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Type": []string{"attachment; filename=\"test.jpg\""}},
},
{
want: true,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"inline; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{
"Content-Disposition": []string{"non-attachment; filename=\"frog.jpg\""}},
},
{
want: false,
header: textproto.MIMEHeader{},
},
}

for _, s := range htests {
got := detectAttachmentHeader(s.header)
if got != s.want {
t.Errorf("detectAttachmentHeader(%v) == %v, want: %v", s.header, got, s.want)
}
}
}

func TestDetectTextHeader(t *testing.T) {
var htests = []struct {
want bool
header textproto.MIMEHeader
emptyIsPlain bool
}{
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/plain"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{"Content-Type": []string{"text/html; charset=utf-8"}},
emptyIsPlain: true,
},
{
want: true,
header: textproto.MIMEHeader{},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{},
emptyIsPlain: false,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"image/jpeg;"}},
emptyIsPlain: true,
},
{
want: false,
header: textproto.MIMEHeader{"Content-Type": []string{"application/octet-stream"}},
emptyIsPlain: true,
},
}

for _, s := range htests {
got := detectTextHeader(s.header, s.emptyIsPlain)
if got != s.want {
t.Errorf("detectTextHeader(%v, %v) == %v, want: %v",
s.header, s.emptyIsPlain, got, s.want)
}
}
}
6 changes: 3 additions & 3 deletions envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ func EnvelopeFromPart(root *Part) (*Envelope, error) {
if e.Text == "" && e.HTML != "" {
// We always warn when this happens
e.Root.addWarning(
errorPlainTextFromHTML,
ErrorPlainTextFromHTML,
"Message did not contain a text/plain part")
var err error
if e.Text, err = html2text.FromString(e.HTML); err != nil {
// Downcoversion shouldn't fail
e.Text = ""
p := e.Root.BreadthMatchFirst(matchHTMLBodyPart)
p.addError(
errorPlainTextFromHTML,
ErrorPlainTextFromHTML,
"Failed to downconvert HTML: %v",
err)
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func parseTextOnlyBody(root *Part, e *Envelope) error {
e.HTML = convHTML
} else {
// Conversion failed
root.addWarning(errorCharsetConversion, err.Error())
root.addWarning(ErrorCharsetConversion, err.Error())
}
}
}
Expand Down
Loading

0 comments on commit 9d33ba6

Please sign in to comment.