Skip to content

Commit

Permalink
Fix YAML round-tripping message parts (mikel#1347)
Browse files Browse the repository at this point in the history
* Add init_with method to Mail::Body
* Add spec for PartsList deserialisation
* Add specs for ensuring empty body does not break
  • Loading branch information
lesleh authored and jeremy committed Jun 18, 2019
1 parent 955f292 commit 3204c0b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mail/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def initialize(string = '')
set_charset
end

def init_with(coder)
coder.map.each { |k, v| instance_variable_set(:"@#{k}", v) }
@parts = Mail::PartsList.new(coder['parts'])
end

# Matches this body with another body. Also matches the decoded value of this
# body with a string.
#
Expand Down
8 changes: 8 additions & 0 deletions spec/mail/body_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,12 @@ def assert_split_into(body, pre, epi, parts)
expect(body.encoded).to eq 'The Body'
end
end

describe "Partslist empty" do
it "should not break on empty PartsList on body" do
body = Mail::Body.new('The Body')
body.sort_parts!
expect(body.parts.count).to eq 0
end
end
end
7 changes: 7 additions & 0 deletions spec/mail/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ def create_mail_with_splat_args
expect(deserialized.delivery_handler).to be_nil
end

it "should deserialize parts as an instance of Mail::PartsList" do
yaml = @yaml_mail.to_yaml
yaml_hash = YAML.load(yaml)
deserialized = Mail::Message.from_yaml(yaml_hash.to_yaml)
expect(deserialized.parts).to be_kind_of(Mail::PartsList)
end

it "should handle multipart mail" do
@yaml_mail.add_part Mail::Part.new(:content_type => 'text/html', :body => '<b>body</b>')
deserialized = Mail::Message.from_yaml(@yaml_mail.to_yaml)
Expand Down
6 changes: 6 additions & 0 deletions spec/mail/parts_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
expect(p.sort!(order)).to eq [plain_text_part, html_text_part, no_content_type_part]
end

it "should not fail on empty PartsList" do
p = Mail::PartsList.new
order = ['text/plain']
p.sort!(order)
end

it "should sort attachments to end" do
p = Mail::PartsList.new
order = ['text/plain', 'text/html']
Expand Down

0 comments on commit 3204c0b

Please sign in to comment.