Skip to content

Commit

Permalink
Added tests for UNIXMbox.fromaddr method. Also changed behaviour of H…
Browse files Browse the repository at this point in the history
…eaderField.new_from_port to return nil if it does not find a matching header in the port provided. Extended UNIXMbox.fromaddr to also search for EnvelopeSender and also made it a public method, not private - Thanks to Maarten O. for alerting these bugs
  • Loading branch information
mikel committed Jan 10, 2008
1 parent 47755da commit e5f52a9
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/tmail/header.rb
Expand Up @@ -69,13 +69,18 @@ def new( name, body, conf = DEFAULT_CONFIG )
# the emial)
#
# Other fields can be passed as normal, "Reply-To", "Received" etc.
#
# Note: Change of behaviour in 1.2.1 => returns nil if it does not find the specified
# header field, otherwise returns an instantiated object of the correct header class
#
# For example:
# port = TMail::FilePort.new("/test/fixtures/raw_email_simple")
# h = TMail::HeaderField.new_from_port(port, "From")
# h.addrs.to_s #=> "Mikel Lindsaar <mikel@nowhere.com>"
# h = TMail::HeaderField.new_from_port(port, "EvelopeSender")
# h.addrs.to_s #=> "mike@anotherplace.com.au"
# h = TMail::HeaderField.new_from_port(port, "SomeWeirdHeaderField")
# h #=> nil
def new_from_port( port, name, conf = DEFAULT_CONFIG )
if name == "EnvelopeSender"
name = "From"
Expand All @@ -93,7 +98,7 @@ def new_from_port( port, name, conf = DEFAULT_CONFIG )
end
end
}
new(name, str, Config.to_config(conf))
new(name, str, Config.to_config(conf)) if str
end

def internal_new( name, conf )
Expand Down
5 changes: 2 additions & 3 deletions lib/tmail/mailbox.rb
Expand Up @@ -215,12 +215,11 @@ def UNIXMbox.create_from_line( port )

def UNIXMbox.fromaddr(port)
h = HeaderField.new_from_port(port, 'Return-Path') ||
HeaderField.new_from_port(port, 'From') ||
HeaderField.new_from_port(port, 'EnvelopeSender') || or return 'nobody'
HeaderField.new_from_port(port, 'From') ||
HeaderField.new_from_port(port, 'EnvelopeSender') or return 'nobody'
a = h.addrs[0] or return 'nobody'
a.spec
end
private_class_method :fromaddr

def close
return if @closed
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/mailbox
@@ -1,10 +1,10 @@
From mike@anotherplace.com.au
Return-Path: <mikel@nowhere.com>
From mike@envelope_sender.com.au
Return-Path: <mikel@return_path.com>
Received: from mikel091a by oaamta05sl.mx.bigpond.com with SMTP id
<20071021093820.JFMT24025.oaamta05sl.mx.bigpond.com@mikel091a> for
<mikel@nowhere.com.else>; Sun, 21 Oct 2007 19:38:20 +1000
Date: Sun, 21 Oct 2007 19:38:13 +1000
From: Mikel Lindsaar <mikel@nowhere.com>
From: Mikel Lindsaar <mikel@from_address.com>
To: Mikel <mikel@somewhere.com>
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
Subject: Testing outlook
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/mailbox_without_any_from_or_sender
@@ -0,0 +1,10 @@
Received: from mikel091a by oaamta05sl.mx.bigpond.com with SMTP id
<20071021093820.JFMT24025.oaamta05sl.mx.bigpond.com@mikel091a> for
<mikel@nowhere.com.else>; Sun, 21 Oct 2007 19:38:20 +1000
Date: Sun, 21 Oct 2007 19:38:13 +1000
To: Mikel <mikel@somewhere.com>
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
Subject: Testing outlook

Hello Mikel

11 changes: 11 additions & 0 deletions test/fixtures/mailbox_without_from
@@ -0,0 +1,11 @@
From mike@envelope_sender.com.au
Received: from mikel091a by oaamta05sl.mx.bigpond.com with SMTP id
<20071021093820.JFMT24025.oaamta05sl.mx.bigpond.com@mikel091a> for
<mikel@nowhere.com.else>; Sun, 21 Oct 2007 19:38:20 +1000
Date: Sun, 21 Oct 2007 19:38:13 +1000
To: Mikel <mikel@somewhere.com>
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
Subject: Testing outlook

Hello Mikel

12 changes: 12 additions & 0 deletions test/fixtures/mailbox_without_return_path
@@ -0,0 +1,12 @@
From mike@envelope_sender.com.au
Received: from mikel091a by oaamta05sl.mx.bigpond.com with SMTP id
<20071021093820.JFMT24025.oaamta05sl.mx.bigpond.com@mikel091a> for
<mikel@nowhere.com.else>; Sun, 21 Oct 2007 19:38:20 +1000
Date: Sun, 21 Oct 2007 19:38:13 +1000
From: Mikel Lindsaar <mikel@from_address.com>
To: Mikel <mikel@somewhere.com>
Message-Id: <009601c813c6$19df3510$0437d30a@mikel091a>
Subject: Testing outlook

Hello Mikel

10 changes: 8 additions & 2 deletions test/test_header.rb
Expand Up @@ -916,13 +916,13 @@ def test_new_from_port_should_produce_a_header_object_of_the_correct_class
def test_should_return_the_evelope_sender_when_given_from_without_a_colon
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
h = TMail::HeaderField.new_from_port(p, 'EnvelopeSender')
assert_equal("mike@anotherplace.com.au", h.addrs.to_s)
assert_equal("mike@envelope_sender.com.au", h.addrs.to_s)
end

def test_new_from_port_should_produce_a_header_object_that_contains_the_right_data
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
h = TMail::HeaderField.new_from_port(p, 'From')
assert_equal("Mikel Lindsaar <mikel@nowhere.com>", h.addrs.to_s)
assert_equal("Mikel Lindsaar <mikel@from_address.com>", h.addrs.to_s)
end

def test_unwrapping_a_long_header_field_using_new_from_port
Expand All @@ -931,5 +931,11 @@ def test_unwrapping_a_long_header_field_using_new_from_port
line = "from mikel091a by oaamta05sl.mx.bigpond.com with SMTP id <20071021093820.JFMT24025.oaamta05sl.mx.bigpond.com@mikel091a> for <mikel@nowhere.com.else>; Sun, 21 Oct 2007 19:38:20 +1000"
assert_equal(h.to_s, line)
end

def test_returning_nil_if_there_is_no_match
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
h = TMail::HeaderField.new_from_port(p, 'Received-Long-Header')
assert_equal(h, nil)
end

end
21 changes: 21 additions & 0 deletions test/test_mbox.rb
Expand Up @@ -123,4 +123,25 @@ def test_each_new_port
end
assert_equal 0, c
end

def test_unix_mbox_fromaddr_method
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@return_path.com")
end

def test_unix_mbox_fromaddr_method_missing_return_path
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_return_path")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mikel@from_address.com")
end

def test_unix_mbox_fromaddr_method_missing_from_address
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_from")
assert_equal(TMail::UNIXMbox.fromaddr(p), "mike@envelope_sender.com.au")
end

def test_unix_mbox_from_addr_method_missing_all_from_fields_in_the_email
p = TMail::FilePort.new("#{File.dirname(__FILE__)}/fixtures/mailbox_without_any_from_or_sender")
assert_equal(TMail::UNIXMbox.fromaddr(p), "nobody")
end

end

0 comments on commit e5f52a9

Please sign in to comment.