airbrake 3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/.gitignore +18 -0
  2. data/.yardopts +3 -0
  3. data/CHANGELOG +441 -0
  4. data/Gemfile +3 -0
  5. data/INSTALL +25 -0
  6. data/MIT-LICENSE +22 -0
  7. data/README.md +431 -0
  8. data/README_FOR_HEROKU_ADDON.md +93 -0
  9. data/Rakefile +188 -0
  10. data/SUPPORTED_RAILS_VERSIONS +14 -0
  11. data/TESTING.md +26 -0
  12. data/airbrake.gemspec +33 -0
  13. data/features/metal.feature +23 -0
  14. data/features/rack.feature +27 -0
  15. data/features/rails.feature +254 -0
  16. data/features/rails_with_js_notifier.feature +78 -0
  17. data/features/rake.feature +23 -0
  18. data/features/sinatra.feature +33 -0
  19. data/features/step_definitions/airbrake_shim.rb.template +15 -0
  20. data/features/step_definitions/file_steps.rb +10 -0
  21. data/features/step_definitions/metal_steps.rb +23 -0
  22. data/features/step_definitions/rack_steps.rb +20 -0
  23. data/features/step_definitions/rails_application_steps.rb +401 -0
  24. data/features/step_definitions/rake_steps.rb +17 -0
  25. data/features/support/airbrake_shim.rb.template +15 -0
  26. data/features/support/env.rb +18 -0
  27. data/features/support/matchers.rb +35 -0
  28. data/features/support/rails.rb +181 -0
  29. data/features/support/rake/Rakefile +57 -0
  30. data/features/support/terminal.rb +103 -0
  31. data/features/user_informer.feature +63 -0
  32. data/generators/airbrake/airbrake_generator.rb +90 -0
  33. data/generators/airbrake/lib/insert_commands.rb +34 -0
  34. data/generators/airbrake/lib/rake_commands.rb +24 -0
  35. data/generators/airbrake/templates/airbrake_tasks.rake +25 -0
  36. data/generators/airbrake/templates/capistrano_hook.rb +6 -0
  37. data/generators/airbrake/templates/initializer.rb +6 -0
  38. data/install.rb +1 -0
  39. data/lib/airbrake.rb +150 -0
  40. data/lib/airbrake/backtrace.rb +100 -0
  41. data/lib/airbrake/capistrano.rb +21 -0
  42. data/lib/airbrake/configuration.rb +247 -0
  43. data/lib/airbrake/notice.rb +348 -0
  44. data/lib/airbrake/rack.rb +42 -0
  45. data/lib/airbrake/rails.rb +41 -0
  46. data/lib/airbrake/rails/action_controller_catcher.rb +30 -0
  47. data/lib/airbrake/rails/controller_methods.rb +68 -0
  48. data/lib/airbrake/rails/error_lookup.rb +33 -0
  49. data/lib/airbrake/rails/javascript_notifier.rb +42 -0
  50. data/lib/airbrake/rails3_tasks.rb +82 -0
  51. data/lib/airbrake/railtie.rb +33 -0
  52. data/lib/airbrake/rake_handler.rb +65 -0
  53. data/lib/airbrake/sender.rb +83 -0
  54. data/lib/airbrake/shared_tasks.rb +30 -0
  55. data/lib/airbrake/tasks.rb +83 -0
  56. data/lib/airbrake/user_informer.rb +25 -0
  57. data/lib/airbrake/version.rb +3 -0
  58. data/lib/airbrake_tasks.rb +50 -0
  59. data/lib/rails/generators/airbrake/airbrake_generator.rb +96 -0
  60. data/lib/templates/javascript_notifier.erb +13 -0
  61. data/lib/templates/rescue.erb +91 -0
  62. data/rails/init.rb +1 -0
  63. data/script/integration_test.rb +38 -0
  64. data/test/airbrake_2_2.xsd +78 -0
  65. data/test/airbrake_tasks_test.rb +163 -0
  66. data/test/backtrace_test.rb +163 -0
  67. data/test/catcher_test.rb +333 -0
  68. data/test/configuration_test.rb +216 -0
  69. data/test/helper.rb +251 -0
  70. data/test/javascript_notifier_test.rb +52 -0
  71. data/test/logger_test.rb +85 -0
  72. data/test/notice_test.rb +459 -0
  73. data/test/notifier_test.rb +235 -0
  74. data/test/rack_test.rb +58 -0
  75. data/test/rails_initializer_test.rb +36 -0
  76. data/test/recursion_test.rb +10 -0
  77. data/test/sender_test.rb +193 -0
  78. data/test/user_informer_test.rb +29 -0
  79. metadata +365 -0
@@ -0,0 +1,235 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class NotifierTest < Test::Unit::TestCase
4
+
5
+ class OriginalException < Exception
6
+ end
7
+
8
+ class ContinuedException < Exception
9
+ end
10
+
11
+ include DefinesConstants
12
+
13
+ def setup
14
+ super
15
+ reset_config
16
+ end
17
+
18
+ def assert_sent(notice, notice_args)
19
+ assert_received(Airbrake::Notice, :new) {|expect| expect.with(has_entries(notice_args)) }
20
+ assert_received(notice, :to_xml)
21
+ assert_received(Airbrake.sender, :send_to_airbrake) {|expect| expect.with(notice.to_xml) }
22
+ end
23
+
24
+ def set_public_env
25
+ Airbrake.configure { |config| config.environment_name = 'production' }
26
+ end
27
+
28
+ def set_development_env
29
+ Airbrake.configure { |config| config.environment_name = 'development' }
30
+ end
31
+
32
+ should "yield and save a configuration when configuring" do
33
+ yielded_configuration = nil
34
+ Airbrake.configure do |config|
35
+ yielded_configuration = config
36
+ end
37
+
38
+ assert_kind_of Airbrake::Configuration, yielded_configuration
39
+ assert_equal yielded_configuration, Airbrake.configuration
40
+ end
41
+
42
+ should "not remove existing config options when configuring twice" do
43
+ first_config = nil
44
+ Airbrake.configure do |config|
45
+ first_config = config
46
+ end
47
+ Airbrake.configure do |config|
48
+ assert_equal first_config, config
49
+ end
50
+ end
51
+
52
+ should "configure the sender" do
53
+ sender = stub_sender
54
+ Airbrake::Sender.stubs(:new => sender)
55
+ configuration = nil
56
+
57
+ Airbrake.configure { |yielded_config| configuration = yielded_config }
58
+
59
+ assert_received(Airbrake::Sender, :new) { |expect| expect.with(configuration) }
60
+ assert_equal sender, Airbrake.sender
61
+ end
62
+
63
+ should "create and send a notice for an exception" do
64
+ set_public_env
65
+ exception = build_exception
66
+ stub_sender!
67
+ notice = stub_notice!
68
+
69
+ Airbrake.notify(exception)
70
+
71
+ assert_sent notice, :exception => exception
72
+ end
73
+
74
+ should "create and send a notice for a hash" do
75
+ set_public_env
76
+ notice = stub_notice!
77
+ notice_args = { :error_message => 'uh oh' }
78
+ stub_sender!
79
+
80
+ Airbrake.notify(notice_args)
81
+
82
+ assert_sent(notice, notice_args)
83
+ end
84
+
85
+ should "create and send a notice for an exception that responds to to_hash" do
86
+ set_public_env
87
+ exception = build_exception
88
+ notice = stub_notice!
89
+ notice_args = { :error_message => 'uh oh' }
90
+ exception.stubs(:to_hash).returns(notice_args)
91
+ stub_sender!
92
+
93
+ Airbrake.notify(exception)
94
+
95
+ assert_sent(notice, notice_args.merge(:exception => exception))
96
+ end
97
+
98
+ should "create and sent a notice for an exception and hash" do
99
+ set_public_env
100
+ exception = build_exception
101
+ notice = stub_notice!
102
+ notice_args = { :error_message => 'uh oh' }
103
+ stub_sender!
104
+
105
+ Airbrake.notify(exception, notice_args)
106
+
107
+ assert_sent(notice, notice_args.merge(:exception => exception))
108
+ end
109
+
110
+ should "not create a notice in a development environment" do
111
+ set_development_env
112
+ sender = stub_sender!
113
+
114
+ Airbrake.notify(build_exception)
115
+ Airbrake.notify_or_ignore(build_exception)
116
+
117
+ assert_received(sender, :send_to_airbrake) {|expect| expect.never }
118
+ end
119
+
120
+ should "not deliver an ignored exception when notifying implicitly" do
121
+ set_public_env
122
+ exception = build_exception
123
+ sender = stub_sender!
124
+ notice = stub_notice!
125
+ notice.stubs(:ignore? => true)
126
+
127
+ Airbrake.notify_or_ignore(exception)
128
+
129
+ assert_received(sender, :send_to_airbrake) {|expect| expect.never }
130
+ end
131
+
132
+ should "deliver an ignored exception when notifying manually" do
133
+ set_public_env
134
+ exception = build_exception
135
+ sender = stub_sender!
136
+ notice = stub_notice!
137
+ notice.stubs(:ignore? => true)
138
+
139
+ Airbrake.notify(exception)
140
+
141
+ assert_sent(notice, :exception => exception)
142
+ end
143
+
144
+ should "pass config to created notices" do
145
+ exception = build_exception
146
+ config_opts = { 'one' => 'two', 'three' => 'four' }
147
+ notice = stub_notice!
148
+ stub_sender!
149
+ Airbrake.configuration = stub('config', :merge => config_opts, :public? => true)
150
+
151
+ Airbrake.notify(exception)
152
+
153
+ assert_received(Airbrake::Notice, :new) do |expect|
154
+ expect.with(has_entries(config_opts))
155
+ end
156
+ end
157
+
158
+ context "building notice JSON for an exception" do
159
+ setup do
160
+ @params = { :controller => "users", :action => "create" }
161
+ @exception = build_exception
162
+ @hash = Airbrake.build_lookup_hash_for(@exception, @params)
163
+ end
164
+
165
+ should "set action" do
166
+ assert_equal @params[:action], @hash[:action]
167
+ end
168
+
169
+ should "set controller" do
170
+ assert_equal @params[:controller], @hash[:component]
171
+ end
172
+
173
+ should "set line number" do
174
+ assert @hash[:line_number] =~ /\d+/
175
+ end
176
+
177
+ should "set file" do
178
+ assert_match /test\/helper\.rb$/, @hash[:file]
179
+ end
180
+
181
+ should "set rails_env to production" do
182
+ assert_equal 'production', @hash[:environment_name]
183
+ end
184
+
185
+ should "set error class" do
186
+ assert_equal @exception.class.to_s, @hash[:error_class]
187
+ end
188
+
189
+ should "not set file or line number with no backtrace" do
190
+ @exception.stubs(:backtrace).returns([])
191
+
192
+ @hash = Airbrake.build_lookup_hash_for(@exception)
193
+
194
+ assert_nil @hash[:line_number]
195
+ assert_nil @hash[:file]
196
+ end
197
+
198
+ should "not set action or controller when not provided" do
199
+ @hash = Airbrake.build_lookup_hash_for(@exception)
200
+
201
+ assert_nil @hash[:action]
202
+ assert_nil @hash[:controller]
203
+ end
204
+
205
+ context "when an exception that provides #original_exception is raised" do
206
+ setup do
207
+ @exception.stubs(:original_exception).returns(begin
208
+ raise NotifierTest::OriginalException.new
209
+ rescue Exception => e
210
+ e
211
+ end)
212
+ end
213
+
214
+ should "unwrap exceptions that provide #original_exception" do
215
+ @hash = Airbrake.build_lookup_hash_for(@exception)
216
+ assert_equal "NotifierTest::OriginalException", @hash[:error_class]
217
+ end
218
+ end
219
+
220
+ context "when an exception that provides #continued_exception is raised" do
221
+ setup do
222
+ @exception.stubs(:continued_exception).returns(begin
223
+ raise NotifierTest::ContinuedException.new
224
+ rescue Exception => e
225
+ e
226
+ end)
227
+ end
228
+
229
+ should "unwrap exceptions that provide #continued_exception" do
230
+ @hash = Airbrake.build_lookup_hash_for(@exception)
231
+ assert_equal "NotifierTest::ContinuedException", @hash[:error_class]
232
+ end
233
+ end
234
+ end
235
+ end
data/test/rack_test.rb ADDED
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class RackTest < Test::Unit::TestCase
4
+
5
+ should "call the upstream app with the environment" do
6
+ environment = { 'key' => 'value' }
7
+ app = lambda { |env| ['response', {}, env] }
8
+ stack = Airbrake::Rack.new(app)
9
+
10
+ response = stack.call(environment)
11
+
12
+ assert_equal ['response', {}, environment], response
13
+ end
14
+
15
+ should "deliver an exception raised while calling an upstream app" do
16
+ Airbrake.stubs(:notify_or_ignore)
17
+
18
+ exception = build_exception
19
+ environment = { 'key' => 'value' }
20
+ app = lambda do |env|
21
+ raise exception
22
+ end
23
+
24
+ begin
25
+ stack = Airbrake::Rack.new(app)
26
+ stack.call(environment)
27
+ rescue Exception => raised
28
+ assert_equal exception, raised
29
+ else
30
+ flunk "Didn't raise an exception"
31
+ end
32
+
33
+ assert_received(Airbrake, :notify_or_ignore) do |expect|
34
+ expect.with(exception, :rack_env => environment)
35
+ end
36
+ end
37
+
38
+ should "deliver an exception in rack.exception" do
39
+ Airbrake.stubs(:notify_or_ignore)
40
+ exception = build_exception
41
+ environment = { 'key' => 'value' }
42
+
43
+ response = [200, {}, ['okay']]
44
+ app = lambda do |env|
45
+ env['rack.exception'] = exception
46
+ response
47
+ end
48
+ stack = Airbrake::Rack.new(app)
49
+
50
+ actual_response = stack.call(environment)
51
+
52
+ assert_equal response, actual_response
53
+ assert_received(Airbrake, :notify_or_ignore) do |expect|
54
+ expect.with(exception, :rack_env => environment)
55
+ end
56
+ end
57
+
58
+ end
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ require 'airbrake/rails'
4
+
5
+ class RailsInitializerTest < Test::Unit::TestCase
6
+ include DefinesConstants
7
+
8
+ should "trigger use of Rails' logger if logger isn't set and Rails' logger exists" do
9
+ rails = Module.new do
10
+ def self.logger
11
+ "RAILS LOGGER"
12
+ end
13
+ end
14
+ define_constant("Rails", rails)
15
+ Airbrake::Rails.initialize
16
+ assert_equal "RAILS LOGGER", Airbrake.logger
17
+ end
18
+
19
+ should "trigger use of Rails' default logger if logger isn't set and Rails.logger doesn't exist" do
20
+ define_constant("RAILS_DEFAULT_LOGGER", "RAILS DEFAULT LOGGER")
21
+
22
+ Airbrake::Rails.initialize
23
+ assert_equal "RAILS DEFAULT LOGGER", Airbrake.logger
24
+ end
25
+
26
+ should "allow overriding of the logger if already assigned" do
27
+ define_constant("RAILS_DEFAULT_LOGGER", "RAILS DEFAULT LOGGER")
28
+ Airbrake::Rails.initialize
29
+
30
+ Airbrake.configure(true) do |config|
31
+ config.logger = "OVERRIDDEN LOGGER"
32
+ end
33
+
34
+ assert_equal "OVERRIDDEN LOGGER", Airbrake.logger
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class RecursionTest < Test::Unit::TestCase
4
+ should "not allow infinite recursion" do
5
+ hash = {:a => :a}
6
+ hash[:hash] = hash
7
+ notice = Airbrake::Notice.new(:parameters => hash)
8
+ assert_equal "[possible infinite recursion halted]", notice.parameters[:hash]
9
+ end
10
+ end
@@ -0,0 +1,193 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class SenderTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ reset_config
7
+ end
8
+
9
+ def build_sender(opts = {})
10
+ config = Airbrake::Configuration.new
11
+ opts.each {|opt, value| config.send(:"#{opt}=", value) }
12
+ Airbrake::Sender.new(config)
13
+ end
14
+
15
+ def send_exception(args = {})
16
+ notice = args.delete(:notice) || build_notice_data
17
+ sender = args.delete(:sender) || build_sender(args)
18
+ sender.send_to_airbrake(notice)
19
+ end
20
+
21
+ def stub_http(options = {})
22
+ response = stub(:body => options[:body] || 'body')
23
+ http = stub(:post => response,
24
+ :read_timeout= => nil,
25
+ :open_timeout= => nil,
26
+ :ca_file= => nil,
27
+ :verify_mode= => nil,
28
+ :use_ssl= => nil)
29
+ Net::HTTP.stubs(:new => http)
30
+ http
31
+ end
32
+
33
+ should "post to Airbrake when using an HTTP proxy" do
34
+ response = stub(:body => 'body')
35
+ http = stub(:post => response,
36
+ :read_timeout= => nil,
37
+ :open_timeout= => nil,
38
+ :use_ssl= => nil)
39
+ proxy = stub(:new => http)
40
+ Net::HTTP.stubs(:Proxy => proxy)
41
+
42
+ url = "http://airbrakeapp.com:80#{Airbrake::Sender::NOTICES_URI}"
43
+ uri = URI.parse(url)
44
+
45
+ proxy_host = 'some.host'
46
+ proxy_port = 88
47
+ proxy_user = 'login'
48
+ proxy_pass = 'passwd'
49
+
50
+ send_exception(:proxy_host => proxy_host,
51
+ :proxy_port => proxy_port,
52
+ :proxy_user => proxy_user,
53
+ :proxy_pass => proxy_pass)
54
+ assert_received(http, :post) do |expect|
55
+ expect.with(uri.path, anything, Airbrake::HEADERS)
56
+ end
57
+ assert_received(Net::HTTP, :Proxy) do |expect|
58
+ expect.with(proxy_host, proxy_port, proxy_user, proxy_pass)
59
+ end
60
+ end
61
+
62
+ should "return the created group's id on successful posting" do
63
+ http = stub_http(:body => '<error-id type="integer">3799307</error-id>')
64
+ assert_equal "3799307", send_exception(:secure => false)
65
+ end
66
+
67
+ should "return nil on failed posting" do
68
+ http = stub_http
69
+ http.stubs(:post).raises(Errno::ECONNREFUSED)
70
+ assert_equal nil, send_exception(:secure => false)
71
+ end
72
+
73
+ should "not fail when posting and a timeout exception occurs" do
74
+ http = stub_http
75
+ http.stubs(:post).raises(TimeoutError)
76
+ assert_nothing_thrown do
77
+ send_exception(:secure => false)
78
+ end
79
+ end
80
+
81
+ should "not fail when posting and a connection refused exception occurs" do
82
+ http = stub_http
83
+ http.stubs(:post).raises(Errno::ECONNREFUSED)
84
+ assert_nothing_thrown do
85
+ send_exception(:secure => false)
86
+ end
87
+ end
88
+
89
+ should "not fail when posting any http exception occurs" do
90
+ http = stub_http
91
+ Airbrake::Sender::HTTP_ERRORS.each do |error|
92
+ http.stubs(:post).raises(error)
93
+ assert_nothing_thrown do
94
+ send_exception(:secure => false)
95
+ end
96
+ end
97
+ end
98
+
99
+ should "post to the right url for non-ssl" do
100
+ http = stub_http
101
+ url = "http://airbrakeapp.com:80#{Airbrake::Sender::NOTICES_URI}"
102
+ uri = URI.parse(url)
103
+ send_exception(:secure => false)
104
+ assert_received(http, :post) {|expect| expect.with(uri.path, anything, Airbrake::HEADERS) }
105
+ end
106
+
107
+ should "post to the right path for ssl" do
108
+ http = stub_http
109
+ send_exception(:secure => true)
110
+ assert_received(http, :post) {|expect| expect.with(Airbrake::Sender::NOTICES_URI, anything, Airbrake::HEADERS) }
111
+ end
112
+
113
+ should "verify the SSL peer when the use_ssl option is set to true" do
114
+ url = "https://airbrakeapp.com#{Airbrake::Sender::NOTICES_URI}"
115
+ uri = URI.parse(url)
116
+
117
+ real_http = Net::HTTP.new(uri.host, uri.port)
118
+ real_http.stubs(:post => nil)
119
+ proxy = stub(:new => real_http)
120
+ Net::HTTP.stubs(:Proxy => proxy)
121
+ File.stubs(:exist?).with(OpenSSL::X509::DEFAULT_CERT_FILE).returns(false)
122
+
123
+ send_exception(:secure => true)
124
+ assert(real_http.use_ssl)
125
+ assert_equal(OpenSSL::SSL::VERIFY_PEER, real_http.verify_mode)
126
+ assert_nil real_http.ca_file
127
+ end
128
+
129
+ should "verify the SSL peer when the use_ssl option is set to true and the default cert exists" do
130
+ url = "https://airbrakeapp.com#{Airbrake::Sender::NOTICES_URI}"
131
+ uri = URI.parse(url)
132
+
133
+ real_http = Net::HTTP.new(uri.host, uri.port)
134
+ real_http.stubs(:post => nil)
135
+ proxy = stub(:new => real_http)
136
+ Net::HTTP.stubs(:Proxy => proxy)
137
+ File.stubs(:exist?).with(OpenSSL::X509::DEFAULT_CERT_FILE).returns(true)
138
+
139
+ send_exception(:secure => true)
140
+ assert(real_http.use_ssl)
141
+ assert_equal(OpenSSL::SSL::VERIFY_PEER, real_http.verify_mode)
142
+ assert_equal(OpenSSL::X509::DEFAULT_CERT_FILE, real_http.ca_file)
143
+ end
144
+
145
+ should "default the open timeout to 2 seconds" do
146
+ http = stub_http
147
+ send_exception
148
+ assert_received(http, :open_timeout=) {|expect| expect.with(2) }
149
+ end
150
+
151
+ should "default the read timeout to 5 seconds" do
152
+ http = stub_http
153
+ send_exception
154
+ assert_received(http, :read_timeout=) {|expect| expect.with(5) }
155
+ end
156
+
157
+ should "allow override of the open timeout" do
158
+ http = stub_http
159
+ send_exception(:http_open_timeout => 4)
160
+ assert_received(http, :open_timeout=) {|expect| expect.with(4) }
161
+ end
162
+
163
+ should "allow override of the read timeout" do
164
+ http = stub_http
165
+ send_exception(:http_read_timeout => 10)
166
+ assert_received(http, :read_timeout=) {|expect| expect.with(10) }
167
+ end
168
+
169
+ should "connect to the right port for ssl" do
170
+ stub_http
171
+ send_exception(:secure => true)
172
+ assert_received(Net::HTTP, :new) {|expect| expect.with("airbrakeapp.com", 443) }
173
+ end
174
+
175
+ should "connect to the right port for non-ssl" do
176
+ stub_http
177
+ send_exception(:secure => false)
178
+ assert_received(Net::HTTP, :new) {|expect| expect.with("airbrakeapp.com", 80) }
179
+ end
180
+
181
+ should "use ssl if secure" do
182
+ stub_http
183
+ send_exception(:secure => true, :host => 'example.org')
184
+ assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 443) }
185
+ end
186
+
187
+ should "not use ssl if not secure" do
188
+ stub_http
189
+ send_exception(:secure => false, :host => 'example.org')
190
+ assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 80) }
191
+ end
192
+
193
+ end