forked from rack/rack-contrib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip Rack::MailExceptions tests when tmail not installed
While here, modify other conditional tests to use the same basic warning output.
- Loading branch information
Showing
2 changed files
with
78 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,90 +1,96 @@ | |||
require 'rack/mock' | require 'rack/mock' | ||
require 'rack/mailexceptions' | |||
|
|
||
require File.dirname(__FILE__) + '/mail_settings.rb' | begin | ||
require 'tmail' | |||
require 'rack/mailexceptions' | |||
|
|
||
class TestError < RuntimeError | require File.dirname(__FILE__) + '/mail_settings.rb' | ||
end | |||
|
|||
def test_exception | |||
raise TestError, 'Suffering Succotash!' | |||
rescue => boom | |||
return boom | |||
end | |||
|
|||
context 'Rack::MailExceptions' do | |||
|
|
||
setup do | class TestError < RuntimeError | ||
@app = lambda { |env| raise TestError, 'Why, I say' } | |||
@env = Rack::MockRequest.env_for("/foo", | |||
'FOO' => 'BAR', | |||
:method => 'GET', | |||
:input => 'THE BODY' | |||
) | |||
@smtp_settings = { | |||
:server => 'example.com', | |||
:domain => 'example.com', | |||
:port => 500, | |||
:authentication => :login, | |||
:user_name => 'joe', | |||
:password => 'secret' | |||
} | |||
end | end | ||
|
|
||
specify 'yields a configuration object to the block when created' do | def test_exception | ||
called = false | raise TestError, 'Suffering Succotash!' | ||
mailer = | rescue => boom | ||
Rack::MailExceptions.new(@app) do |mail| | return boom | ||
called = true | |||
mail.to 'foo@example.org' | |||
mail.from 'bar@example.org' | |||
mail.subject '[ERROR] %s' | |||
mail.smtp @smtp_settings | |||
end | |||
called.should.be == true | |||
end | end | ||
|
|
||
specify 'generates a TMail object with configured settings' do | context 'Rack::MailExceptions' do | ||
mailer = | |||
Rack::MailExceptions.new(@app) do |mail| | |||
mail.to 'foo@example.org' | |||
mail.from 'bar@example.org' | |||
mail.subject '[ERROR] %s' | |||
mail.smtp @smtp_settings | |||
end | |||
|
|
||
tmail = mailer.send(:generate_mail, test_exception, @env) | setup do | ||
tmail.to.should.equal ['foo@example.org'] | @app = lambda { |env| raise TestError, 'Why, I say' } | ||
tmail.from.should.equal ['bar@example.org'] | @env = Rack::MockRequest.env_for("/foo", | ||
tmail.subject.should.equal '[ERROR] Suffering Succotash!' | 'FOO' => 'BAR', | ||
tmail.body.should.not.be.nil | :method => 'GET', | ||
tmail.body.should.be =~ /FOO:\s+"BAR"/ | :input => 'THE BODY' | ||
tmail.body.should.be =~ /^\s*THE BODY\s*$/ | ) | ||
end | @smtp_settings = { | ||
:server => 'example.com', | |||
:domain => 'example.com', | |||
:port => 500, | |||
:authentication => :login, | |||
:user_name => 'joe', | |||
:password => 'secret' | |||
} | |||
end | |||
|
|
||
specify 'catches exceptions raised from app, sends mail, and re-raises' do | specify 'yields a configuration object to the block when created' do | ||
mailer = | called = false | ||
Rack::MailExceptions.new(@app) do |mail| | mailer = | ||
mail.to 'foo@example.org' | Rack::MailExceptions.new(@app) do |mail| | ||
mail.from 'bar@example.org' | called = true | ||
mail.subject '[ERROR] %s' | mail.to 'foo@example.org' | ||
mail.smtp @smtp_settings | mail.from 'bar@example.org' | ||
end | mail.subject '[ERROR] %s' | ||
lambda { mailer.call(@env) }.should.raise(TestError) | mail.smtp @smtp_settings | ||
@env['mail.sent'].should.be == true | end | ||
end | called.should.be == true | ||
end | |||
|
|||
specify 'generates a TMail object with configured settings' do | |||
mailer = | |||
Rack::MailExceptions.new(@app) do |mail| | |||
mail.to 'foo@example.org' | |||
mail.from 'bar@example.org' | |||
mail.subject '[ERROR] %s' | |||
mail.smtp @smtp_settings | |||
end | |||
|
|||
tmail = mailer.send(:generate_mail, test_exception, @env) | |||
tmail.to.should.equal ['foo@example.org'] | |||
tmail.from.should.equal ['bar@example.org'] | |||
tmail.subject.should.equal '[ERROR] Suffering Succotash!' | |||
tmail.body.should.not.be.nil | |||
tmail.body.should.be =~ /FOO:\s+"BAR"/ | |||
tmail.body.should.be =~ /^\s*THE BODY\s*$/ | |||
end | |||
|
|
||
if TEST_SMTP && ! TEST_SMTP.empty? | specify 'catches exceptions raised from app, sends mail, and re-raises' do | ||
specify 'sends mail' do | |||
mailer = | mailer = | ||
Rack::MailExceptions.new(@app) do |mail| | Rack::MailExceptions.new(@app) do |mail| | ||
mail.config.merge! TEST_SMTP | mail.to 'foo@example.org' | ||
mail.from 'bar@example.org' | |||
mail.subject '[ERROR] %s' | |||
mail.smtp @smtp_settings | |||
end | end | ||
lambda { mailer.call(@env) }.should.raise(TestError) | lambda { mailer.call(@env) }.should.raise(TestError) | ||
@env['mail.sent'].should.be == true | @env['mail.sent'].should.be == true | ||
end | end | ||
else | |||
STDERR.puts 'skipping SMTP tests - edit test/mail_settings.rb to enable' | |||
end | |||
|
|
||
if TEST_SMTP && ! TEST_SMTP.empty? | |||
specify 'sends mail' do | |||
mailer = | |||
Rack::MailExceptions.new(@app) do |mail| | |||
mail.config.merge! TEST_SMTP | |||
end | |||
lambda { mailer.call(@env) }.should.raise(TestError) | |||
@env['mail.sent'].should.be == true | |||
end | |||
else | |||
STDERR.puts 'WARN: Skipping SMTP tests (edit test/mail_settings.rb to enable)' | |||
end | |||
|
|||
end | |||
rescue LoadError => boom | |||
STDERR.puts "WARN: Skipping Rack::MailExceptions tests (tmail not installed)" | |||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters