forked from rack/rack-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_rack_mailexceptions.rb
96 lines (84 loc) · 2.69 KB
/
spec_rack_mailexceptions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'rack/mock'
begin
require 'tmail'
require 'rack/mailexceptions'
require File.dirname(__FILE__) + '/mail_settings.rb'
class TestError < RuntimeError
end
def test_exception
raise TestError, 'Suffering Succotash!'
rescue => boom
return boom
end
context 'Rack::MailExceptions' do
setup do
@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
specify 'yields a configuration object to the block when created' do
called = false
mailer =
Rack::MailExceptions.new(@app) do |mail|
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
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
specify 'catches exceptions raised from app, sends mail, and re-raises' 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
lambda { mailer.call(@env) }.should.raise(TestError)
@env['mail.sent'].should.be == true
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