Skip to content

Commit

Permalink
Fix Message comparison method to ensure there's no side-effects.
Browse files Browse the repository at this point in the history
Comparing a Message w/o message_id set to another object would set it's
Message-ID to a temporary value.  Ditto for the object compared to.
  • Loading branch information
ashcmd committed Aug 14, 2013
1 parent 88dfb3f commit 1e05177
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
9 changes: 6 additions & 3 deletions lib/mail/message.rb
Expand Up @@ -357,9 +357,12 @@ def ==(other)
else
self_message_id, other_message_id = self.message_id, other.message_id
self.message_id, other.message_id = '<temp@test>', '<temp@test>'
result = self.encoded == other.encoded
self.message_id = "<#{self_message_id}>" if self_message_id
other.message_id = "<#{other_message_id}>" if other_message_id
begin
result = self.encoded == other.encoded
ensure
self.message_id = self_message_id
other.message_id = other_message_id
end
result
end
end
Expand Down
30 changes: 10 additions & 20 deletions spec/mail/message_spec.rb
Expand Up @@ -1340,44 +1340,34 @@ def message_with_iso_8859_1_charset
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
m1.should eq m2
# confirm there are no side-effects in the comparison
m1.message_id.should be_nil
m2.message_id.should be_nil
end

it "should ignore the message id value if self has a nil message id" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m1.should eq m2
# confirm there are no side-effects in the comparison
m1.message_id.should be_nil
m2.message_id.should eq '1234@test.lindsaar.net'
end

it "should ignore the message id value if other has a nil message id" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
m1.should eq m2
# confirm there are no side-effects in the comparison
m1.message_id.should eq '1234@test.lindsaar.net'
m2.message_id.should be_nil
end

it "should not be == if both emails have different Message IDs" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <4321@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m1.should_not eq m2
end

it "should preserve the message id of self if set" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
(m1 == m2).should be_true # confirm the side-effects of the comparison
m1.message_id.should eq '1234@test.lindsaar.net'
end

it "should preserve the message id of other if set" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
(m1 == m2).should be_true # confirm the side-effects of the comparison
m2.message_id.should eq '1234@test.lindsaar.net'
end

it "should preserve the message id of both if set" do
m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <4321@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there")
(m1 == m2).should be_false # confirm the side-effects of the comparison
# confirm there are no side-effects in the comparison
m1.message_id.should eq '4321@test.lindsaar.net'
m2.message_id.should eq '1234@test.lindsaar.net'
end
Expand Down

0 comments on commit 1e05177

Please sign in to comment.