Skip to content

Commit

Permalink
feat: don't auto detect charset when using raw content parser option (#…
Browse files Browse the repository at this point in the history
…310)

* feat: don't auto detect charset when using raw content parser option
* tests: add comment on RawContent test cases
  • Loading branch information
jerjako committed Nov 9, 2023
1 parent c35a936 commit 2f48e1b
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
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")
}

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

0 comments on commit 2f48e1b

Please sign in to comment.