From 4cd94bf9fa290cfe1967d0575914a0b9d32a91b3 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Wed, 1 Mar 2017 23:50:28 +0900 Subject: [PATCH] Handle unknown encoding --- lib/chupa-text/decomposers/mail.rb | 8 +++++++- test/fixture/unknown-encoding.eml | 9 +++++++++ test/test-mail.rb | 13 +++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/fixture/unknown-encoding.eml diff --git a/lib/chupa-text/decomposers/mail.rb b/lib/chupa-text/decomposers/mail.rb index ef547ba..d3bf108 100644 --- a/lib/chupa-text/decomposers/mail.rb +++ b/lib/chupa-text/decomposers/mail.rb @@ -43,7 +43,13 @@ def decompose(data) end parts.each_with_index do |part, i| body = part.body.decoded - body.force_encoding(part.charset) if part.charset + if part.charset + begin + body.force_encoding(part.charset) + rescue ArgumentError + raise UnknownEncodingError.new(data, part.charset) + end + end part_data = TextData.new(body, :source_data => data) uri = data.uri.dup diff --git a/test/fixture/unknown-encoding.eml b/test/fixture/unknown-encoding.eml new file mode 100644 index 0000000..88c9542 --- /dev/null +++ b/test/fixture/unknown-encoding.eml @@ -0,0 +1,9 @@ +Mime-Version: 1.0 +Subject: Hello +From: Sender +To: Recipient +Date: Sun, 19 Feb 2017 00:27:55 +0900 (JST) +Content-Type: Text/Plain; charset=iso-2022-jp-3 +Content-Transfer-Encoding: 8bit + +World diff --git a/test/test-mail.rb b/test/test-mail.rb index 8cab4ee..f6905a2 100644 --- a/test/test-mail.rb +++ b/test/test-mail.rb @@ -190,5 +190,18 @@ def decompose super(fixture_path("nested-rfc822.eml")) end end + + sub_test_case("unknown encoding") do + def test_body + assert_raise(ChupaText::UnknownEncodingError) do + decompose + end + end + + private + def decompose + super(fixture_path("unknown-encoding.eml")) + end + end end end