Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't auto detect charset when using raw content parser option #310

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ func TestEncodePartContentNonAsciiText(t *testing.T) {
}
}

func TestParseRawContentOptionTrue(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-option.raw")
// TestParseRawContentHTMLOptionTrue tests the RawContent Parser option with an HTML part only if the content isn't changed.
func TestParseRawContentHTMLOptionTrue(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-html-option.raw")
p := enmime.NewParser(enmime.RawContent(true))
e, err := p.ReadEnvelope(r)
if err != nil {
Expand All @@ -376,11 +377,12 @@ func TestParseRawContentOptionTrue(t *testing.T) {
if err := e.Root.Encode(b); err != nil {
t.Fatal(err)
}
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-option-true.raw.golden")
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-html-option-true.raw.golden")
jhillyerd marked this conversation as resolved.
Show resolved Hide resolved
}

func TestParseRawContentOptionFalse(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-option.raw")
// TestParseRawContentHTMLOptionFalse tests without the RawContent Parser option and an HTML part only if the content is normally parsed.
func TestParseRawContentHTMLOptionFalse(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-html-option.raw")
p := enmime.NewParser()
e, err := p.ReadEnvelope(r)
if err != nil {
Expand All @@ -390,5 +392,36 @@ func TestParseRawContentOptionFalse(t *testing.T) {
if err := e.Root.Encode(b); err != nil {
t.Fatal(err)
}
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-option-false.raw.golden")
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-html-option-false.raw.golden")
}

// TestParseRawContentTextOptionTrue tests the RawContent Parser option with a TEXT part only if the content isn't changed.
// This test uses Japanese characters to also ensure that the charset isn't altered.
func TestParseRawContentTextOptionTrue(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-text-option.raw")
p := enmime.NewParser(enmime.RawContent(true))
e, err := p.ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
b := &bytes.Buffer{}
if err := e.Root.Encode(b); err != nil {
t.Fatal(err)
}
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-text-option-true.raw.golden")
}

// TestParseRawContentTextOptionFalse tests without the RawContent Parser option and a TEXT part only if the content is normally parsed.
func TestParseRawContentTextOptionFalse(t *testing.T) {
r := test.OpenTestData("encode", "parser-raw-content-text-option.raw")
p := enmime.NewParser()
e, err := p.ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
b := &bytes.Buffer{}
if err := e.Root.Encode(b); err != nil {
t.Fatal(err)
}
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-text-option-false.raw.golden")
}
2 changes: 1 addition & 1 deletion part.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (p *Part) decodeContent(r io.Reader, readPartErrorPolicy ReadPartErrorPolic
encoding)
}
// Build charset decoding reader.
if validEncoding && strings.HasPrefix(p.ContentType, "text/") {
if validEncoding && strings.HasPrefix(p.ContentType, "text/") && !p.parser.rawContent {
var err error
contentReader, err = p.convertFromDetectedCharset(contentReader, readPartErrorPolicy)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset=utf-8
From: me@me.com
Mime-Version: 1.0
Subject: Test subject
To: you@you.com

Cgrmt7vku5jjg5XjgqHjgqTjg6vlkI3jg4bjgrnjg4gNCg==
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf-8
From: me@me.com
Mime-Version: 1.0
Subject: Test subject
To: you@you.com

=0A=0A=E6=B7=BB=E4=BB=98=E3=83=95=E3=82=A1=E3=
=82=A4=E3=83=AB=E5=90=8D=E3=83=86=E3=82=B9=E3=83=88
9 changes: 9 additions & 0 deletions testdata/encode/parser-raw-content-text-option.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=utf-8
From: me@me.com
Mime-Version: 1.0
Subject: Test subject
To: you@you.com

=0A=0A=E6=B7=BB=E4=BB=98=E3=83=95=E3=82=A1=E3=
=82=A4=E3=83=AB=E5=90=8D=E3=83=86=E3=82=B9=E3=83=88