Skip to content
Browse files

implement caching in the tests to speed them up

  • Loading branch information...
1 parent bfc0bbf commit b48c713ef1727ed1972d3d0288a70d6e7f9dc673 @technoweenie technoweenie committed Feb 2, 2011
Showing with 28 additions and 45 deletions.
  1. +18 −35 test/message_test.rb
  2. +5 −0 test/test_helper.rb
  3. +5 −10 test/transport_test.rb
View
53 test/message_test.rb
@@ -3,8 +3,7 @@
class MessageParsingTest < Test::Unit::TestCase
test "bad content type header" do
- raw = mail(:bad_content_type)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain :bad_content_type
expected_body = "This message is being generated automatically to notify you\nthat PowerMTA has crashed on mtasv.net.\n\nAs the information below is likely to be essential for debugging\nthe problem, please forward this message to <support@port25.com>.\nThank you.\nYo"
expected_header = "multipart/mixed; boundary=\"====boundary====\""
@@ -15,8 +14,7 @@ class MessageParsingTest < Test::Unit::TestCase
test "basic email" do
body = "---------- Forwarded message ----------\nblah blah"
- raw = mail(:basic)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain :basic
assert_equal 'a16be7390810161014n52b603e9k1aa6bb803c6735aa@mail.gmail.com', msg.message_id
expected = {'mime-version' => '1.0', 'content-type' => 'text/plain; charset=ISO-8859-1', 'date' => "Thu, 16 Oct 2008 10:14:18 -0700",
@@ -34,8 +32,7 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "iso 8859 1 encoded headers" do
- raw = mail("iso-8859-1")
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain "iso-8859-1"
s = Object.const_defined?(:Encoding) ? "Matthéw" : "Matth\351w"
assert_equal "user@example.com", msg.sender.first.address
assert_equal "cc@example.com", msg.cc.first.address
@@ -44,8 +41,7 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "gb2312 encoded body" do
- raw = mail(:gb2312_encoding)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:gb2312_encoding)
# encoding problem?
# "Dear Sirs, \r\nWe are given to understand that you are Manufacturer of plstic Bottles\r\nAdd: blah China"
s = if Object.const_defined?(:Encoding)
@@ -59,8 +55,7 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "gb2312 encoded body with invalid charset in mime version header" do
- raw = mail(:gb2312_encoding_invalid)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:gb2312_encoding_invalid)
# encoding problem?
# "Dear Sirs, \r\nWe are given to understand that you are Manufacturer of plstic Bottles\r\nAdd: blah China"
s = if Object.const_defined?(:Encoding)
@@ -74,15 +69,13 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "utf-8 encoded headers" do
- raw = mail('utf-8')
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain('utf-8')
assert_equal "isnard naik\303\251", msg.sender.first.display_name
assert_equal "user@example.com", msg.sender.first.address
end
test "multipart message with name property on Content Type" do
- raw = mail(:multipart)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multipart)
assert_kind_of Mail::Message, msg.mail
@@ -95,8 +88,7 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "multipart message with filename property on Content Disposition" do
- raw = mail(:multipart2)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multipart2)
assert_kind_of Mail::Message, msg.mail
@@ -109,8 +101,7 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "apple multipart message" do
- raw = mail(:apple_multipart)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:apple_multipart)
assert_kind_of Mail::Message, msg.mail
@@ -124,8 +115,7 @@ class MessageParsingTest < Test::Unit::TestCase
test "multiple sender/recipients" do
body = "---------- Forwarded message ----------\nblah blah"
- raw = mail(:multiple)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multiple)
assert_kind_of Mail::Message, msg.mail
assert_equal %w(processor@astrotrain.com other@example.com), msg.recipients
@@ -137,33 +127,29 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "recipients in the body" do
- raw = mail(:multiple_with_body_recipients)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multiple_with_body_recipients)
assert_equal %w(processor@astrotrain.com other@example.com processor+foobar@astrotrain.com processor+blah@astrotrain.com),
msg.recipients
end
test "with only HTML body in a multipart message" do
- raw = mail(:html_multipart)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:html_multipart)
assert_equal '', msg.body
assert_equal "<p>ABC</p>\n------", msg.html
end
test "with only HTML body in a multipart message" do
- raw = mail(:html)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:html)
assert_equal '', msg.body
assert_equal "<p>ABC</p>", msg.html
end
test "with X Original To header" do
body = "---------- Forwarded message ----------\nblah blah"
- raw = mail(:custom)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:custom)
assert_kind_of Mail::Message, msg.mail
assert_equal %w(processor-reply-57@custom.com processor-delivered@astrotrain.com processor@astrotrain.com), msg.recipients
@@ -176,22 +162,19 @@ class MessageParsingTest < Test::Unit::TestCase
end
test "with multiple Delivered To headers" do
- raw = mail(:multiple_delivered_to)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multiple_delivered_to)
assert_equal %w(processor-reply-57@custom.com processor-delivered@astrotrain.com processor@astrotrain.com), msg.recipients
end
test "parsing invalid email collection" do
- raw = mail(:bad_email_format)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:bad_email_format)
assert_equal 'ricky@foo.com', msg.from[0].address
assert_equal 'bobby@foo.com', msg.from[1].address
end
test "parsing undisclosed recipients" do
- raw = mail(:undisclosed)
- m = Astrotrain::Message.parse(raw)
- assert_equal([], m.recipients_from_to)
+ msg = astrotrain(:undisclosed)
+ assert_equal([], msg.recipients_from_to)
end
end
View
5 test/test_helper.rb
@@ -20,6 +20,11 @@ def self.teardown(&block) define_method(:teardown, &block) end
def mail(filename)
IO.read(File.join(File.dirname(__FILE__), 'fixtures', "#{filename}.txt"))
end
+
+ PARSED = {}
+ def astrotrain(filename)
+ PARSED[filename] ||= Astrotrain::Message.parse(mail(filename))
+ end
end
begin
View
15 test/transport_test.rb
@@ -12,8 +12,7 @@ def teardown
end
test "posts email to webhook" do
- raw = mail(:multipart)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multipart)
stub_http
Astrotrain.deliver(msg, "http://localhost/foo",
@@ -34,8 +33,7 @@ def teardown
test "queues email in resque" do
queue = "astrotrain-test"
klass = "TransportTest::Job"
- raw = mail(:basic)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:basic)
Astrotrain.deliver(msg, "resque://#{queue}/#{klass}", :payload => {:a => 1})
job = Resque.reserve(queue)
assert_equal TransportTest::Job, job.payload_class
@@ -53,8 +51,7 @@ def teardown
end
test "building http params with attachment" do
- raw = mail(:multipart)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:multipart)
params = Astrotrain::Transports::HttpPost.create_hash(msg, 'bar@example.com')
assert_equal 'foo@example.com', params[:to][0][:address]
@@ -69,8 +66,7 @@ def teardown
end
test "building http params without attachment" do
- raw = mail(:basic)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:basic)
params = Astrotrain::Transports::HttpPost.create_hash(msg, 'bar@example.com')
assert_equal 'processor@astrotrain.com', params[:to][0][:address]
@@ -86,8 +82,7 @@ def teardown
end
test "building resque params" do
- raw = mail(:basic)
- msg = Astrotrain::Message.parse(raw)
+ msg = astrotrain(:basic)
params = Astrotrain::Transports::Resque.create_hash(msg, 'bar@example.com')
assert_equal 'processor@astrotrain.com', params[:to][0][:address]

0 comments on commit b48c713

Please sign in to comment.
Something went wrong with that request. Please try again.