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
data/test/helper.rb ADDED
@@ -0,0 +1,251 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
5
+
6
+ require 'thread'
7
+
8
+ require "bundler/setup"
9
+
10
+ require 'shoulda'
11
+ require 'mocha'
12
+
13
+ require 'action_controller'
14
+ require 'action_controller/test_process'
15
+ require 'active_record'
16
+ require 'active_support'
17
+ require 'nokogiri'
18
+ require 'rack'
19
+ require 'bourne'
20
+ require 'sham_rack'
21
+
22
+ require "airbrake"
23
+
24
+ begin require 'redgreen'; rescue LoadError; end
25
+
26
+ module TestMethods
27
+ def rescue_action e
28
+ raise e
29
+ end
30
+
31
+ def do_raise
32
+ raise "Airbrake"
33
+ end
34
+
35
+ def do_not_raise
36
+ render :text => "Success"
37
+ end
38
+
39
+ def do_raise_ignored
40
+ raise ActiveRecord::RecordNotFound.new("404")
41
+ end
42
+
43
+ def do_raise_not_ignored
44
+ raise ActiveRecord::StatementInvalid.new("Statement invalid")
45
+ end
46
+
47
+ def manual_notify
48
+ notify_airbrake(Exception.new)
49
+ render :text => "Success"
50
+ end
51
+
52
+ def manual_notify_ignored
53
+ notify_airbrake(ActiveRecord::RecordNotFound.new("404"))
54
+ render :text => "Success"
55
+ end
56
+ end
57
+
58
+ class AirbrakeController < ActionController::Base
59
+ include TestMethods
60
+ end
61
+
62
+ class Test::Unit::TestCase
63
+ def request(action = nil, method = :get, user_agent = nil, params = {})
64
+ @request = ActionController::TestRequest.new
65
+ @request.action = action ? action.to_s : ""
66
+
67
+ if user_agent
68
+ if @request.respond_to?(:user_agent=)
69
+ @request.user_agent = user_agent
70
+ else
71
+ @request.env["HTTP_USER_AGENT"] = user_agent
72
+ end
73
+ end
74
+ @request.query_parameters = @request.query_parameters.merge(params)
75
+ @response = ActionController::TestResponse.new
76
+ @controller.process(@request, @response)
77
+ end
78
+
79
+ # Borrowed from ActiveSupport 2.3.2
80
+ def assert_difference(expression, difference = 1, message = nil, &block)
81
+ b = block.send(:binding)
82
+ exps = Array.wrap(expression)
83
+ before = exps.map { |e| eval(e, b) }
84
+
85
+ yield
86
+
87
+ exps.each_with_index do |e, i|
88
+ error = "#{e.inspect} didn't change by #{difference}"
89
+ error = "#{message}.\n#{error}" if message
90
+ assert_equal(before[i] + difference, eval(e, b), error)
91
+ end
92
+ end
93
+
94
+ def assert_no_difference(expression, message = nil, &block)
95
+ assert_difference expression, 0, message, &block
96
+ end
97
+
98
+ def stub_sender
99
+ stub('sender', :send_to_airbrake => nil)
100
+ end
101
+
102
+ def stub_sender!
103
+ Airbrake.sender = stub_sender
104
+ end
105
+
106
+ def stub_notice
107
+ stub('notice', :to_xml => 'some yaml', :ignore? => false)
108
+ end
109
+
110
+ def stub_notice!
111
+ stub_notice.tap do |notice|
112
+ Airbrake::Notice.stubs(:new => notice)
113
+ end
114
+ end
115
+
116
+ def create_dummy
117
+ Airbrake::DummySender.new
118
+ end
119
+
120
+ def reset_config
121
+ Airbrake.configuration = nil
122
+ Airbrake.configure do |config|
123
+ config.api_key = 'abc123'
124
+ end
125
+ end
126
+
127
+ def clear_backtrace_filters
128
+ Airbrake.configuration.backtrace_filters.clear
129
+ end
130
+
131
+ def build_exception(opts = {})
132
+ backtrace = ["airbrake/test/helper.rb:132:in `build_exception'",
133
+ "airbrake/test/backtrace.rb:4:in `build_notice_data'",
134
+ "/var/lib/gems/1.8/gems/airbrake-2.4.5/rails/init.rb:2:in `send_exception'"]
135
+ opts = {:backtrace => backtrace}.merge(opts)
136
+ BacktracedException.new(opts)
137
+ end
138
+
139
+ class BacktracedException < Exception
140
+ attr_accessor :backtrace
141
+ def initialize(opts)
142
+ @backtrace = opts[:backtrace]
143
+ end
144
+ def set_backtrace(bt)
145
+ @backtrace = bt
146
+ end
147
+ end
148
+
149
+ def build_notice_data(exception = nil)
150
+ exception ||= build_exception
151
+ {
152
+ :api_key => 'abc123',
153
+ :error_class => exception.class.name,
154
+ :error_message => "#{exception.class.name}: #{exception.message}",
155
+ :backtrace => exception.backtrace,
156
+ :environment => { 'PATH' => '/bin', 'REQUEST_URI' => '/users/1' },
157
+ :request => {
158
+ :params => { 'controller' => 'users', 'action' => 'show', 'id' => '1' },
159
+ :rails_root => '/path/to/application',
160
+ :url => "http://test.host/users/1"
161
+ },
162
+ :session => {
163
+ :key => '123abc',
164
+ :data => { 'user_id' => '5', 'flash' => { 'notice' => 'Logged in successfully' } }
165
+ }
166
+ }
167
+ end
168
+
169
+ def assert_caught_and_sent
170
+ assert !Airbrake.sender.collected.empty?
171
+ end
172
+
173
+ def assert_caught_and_not_sent
174
+ assert Airbrake.sender.collected.empty?
175
+ end
176
+
177
+ def assert_array_starts_with(expected, actual)
178
+ assert_respond_to actual, :to_ary
179
+ array = actual.to_ary.reverse
180
+ expected.reverse.each_with_index do |value, i|
181
+ assert_equal value, array[i]
182
+ end
183
+ end
184
+
185
+ def assert_valid_node(document, xpath, content)
186
+ nodes = document.xpath(xpath)
187
+ assert nodes.any?{|node| node.content == content },
188
+ "Expected xpath #{xpath} to have content #{content}, " +
189
+ "but found #{nodes.map { |n| n.content }} in #{nodes.size} matching nodes." +
190
+ "Document:\n#{document.to_s}"
191
+ end
192
+ end
193
+
194
+ module DefinesConstants
195
+ def setup
196
+ @defined_constants = []
197
+ end
198
+
199
+ def teardown
200
+ @defined_constants.each do |constant|
201
+ Object.__send__(:remove_const, constant)
202
+ end
203
+ end
204
+
205
+ def define_constant(name, value)
206
+ Object.const_set(name, value)
207
+ @defined_constants << name
208
+ end
209
+ end
210
+
211
+ # Also stolen from AS 2.3.2
212
+ class Array
213
+ # Wraps the object in an Array unless it's an Array. Converts the
214
+ # object to an Array using #to_ary if it implements that.
215
+ def self.wrap(object)
216
+ case object
217
+ when nil
218
+ []
219
+ when self
220
+ object
221
+ else
222
+ if object.respond_to?(:to_ary)
223
+ object.to_ary
224
+ else
225
+ [object]
226
+ end
227
+ end
228
+ end
229
+
230
+ end
231
+
232
+ class CollectingSender
233
+ attr_reader :collected
234
+
235
+ def initialize
236
+ @collected = []
237
+ end
238
+
239
+ def send_to_airbrake(data)
240
+ @collected << data
241
+ end
242
+ end
243
+
244
+ class FakeLogger
245
+ def info(*args); end
246
+ def debug(*args); end
247
+ def warn(*args); end
248
+ def error(*args); end
249
+ def fatal(*args); end
250
+ end
251
+
@@ -0,0 +1,52 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'airbrake/rails/javascript_notifier'
3
+ require 'ostruct'
4
+
5
+ class JavascriptNotifierTest < Test::Unit::TestCase
6
+ module FakeRenderer
7
+ def javascript_tag(text)
8
+ "<script>#{text}</script>"
9
+ end
10
+ def escape_javascript(text)
11
+ "ESC#{text}ESC"
12
+ end
13
+ end
14
+
15
+ class FakeController
16
+ def self.helper_method(*args)
17
+ end
18
+
19
+ include Airbrake::Rails::JavascriptNotifier
20
+
21
+ def action_name
22
+ "action"
23
+ end
24
+
25
+ def controller_name
26
+ "controller"
27
+ end
28
+
29
+ def request
30
+ @request ||= OpenStruct.new
31
+ end
32
+
33
+ def render_to_string(options)
34
+ context = OpenStruct.new(options[:locals])
35
+ context.extend(FakeRenderer)
36
+ context.instance_eval do
37
+ erb = ERB.new(IO.read(options[:file]))
38
+ erb.result(binding)
39
+ end
40
+ end
41
+ end
42
+
43
+ should "make sure escape_javacript is called on the request.url" do
44
+ Airbrake.configure do
45
+ end
46
+ controller = FakeController.new
47
+ controller.request.url = "bad_javascript"
48
+ assert controller.send(:airbrake_javascript_notifier)['"ESCbad_javascriptESC"']
49
+ assert ! controller.send(:airbrake_javascript_notifier)['"bad_javascript"']
50
+ end
51
+ end
52
+
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ def stub_http(response, body = nil)
5
+ response.stubs(:body => body) if body
6
+ @http = stub(:post => response,
7
+ :read_timeout= => nil,
8
+ :open_timeout= => nil,
9
+ :use_ssl= => nil)
10
+ Net::HTTP.stubs(:new).returns(@http)
11
+ end
12
+
13
+ def send_notice
14
+ Airbrake.sender.send_to_airbrake('data')
15
+ end
16
+
17
+ def stub_verbose_log
18
+ Airbrake.stubs(:write_verbose_log)
19
+ end
20
+
21
+ def assert_logged(expected)
22
+ assert_received(Airbrake, :write_verbose_log) do |expect|
23
+ expect.with {|actual| actual =~ expected }
24
+ end
25
+ end
26
+
27
+ def assert_not_logged(expected)
28
+ assert_received(Airbrake, :write_verbose_log) do |expect|
29
+ expect.with {|actual| actual =~ expected }.never
30
+ end
31
+ end
32
+
33
+ def configure
34
+ Airbrake.configure { |config| }
35
+ end
36
+
37
+ should "report that notifier is ready when configured" do
38
+ stub_verbose_log
39
+ configure
40
+ assert_logged /Notifier (.*) ready/
41
+ end
42
+
43
+ should "not report that notifier is ready when internally configured" do
44
+ stub_verbose_log
45
+ Airbrake.configure(true) { |config| }
46
+ assert_not_logged /.*/
47
+ end
48
+
49
+ should "print environment info a successful notification without a body" do
50
+ reset_config
51
+ stub_verbose_log
52
+ stub_http(Net::HTTPSuccess)
53
+ send_notice
54
+ assert_logged /Environment Info:/
55
+ assert_not_logged /Response from Airbrake:/
56
+ end
57
+
58
+ should "print environment info on a failed notification without a body" do
59
+ reset_config
60
+ stub_verbose_log
61
+ stub_http(Net::HTTPError)
62
+ send_notice
63
+ assert_logged /Environment Info:/
64
+ assert_not_logged /Response from Airbrake:/
65
+ end
66
+
67
+ should "print environment info and response on a success with a body" do
68
+ reset_config
69
+ stub_verbose_log
70
+ stub_http(Net::HTTPSuccess, 'test')
71
+ send_notice
72
+ assert_logged /Environment Info:/
73
+ assert_logged /Response from Airbrake:/
74
+ end
75
+
76
+ should "print environment info and response on a failure with a body" do
77
+ reset_config
78
+ stub_verbose_log
79
+ stub_http(Net::HTTPError, 'test')
80
+ send_notice
81
+ assert_logged /Environment Info:/
82
+ assert_logged /Response from Airbrake:/
83
+ end
84
+
85
+ end
@@ -0,0 +1,459 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class NoticeTest < Test::Unit::TestCase
4
+
5
+ include DefinesConstants
6
+
7
+ def configure
8
+ Airbrake::Configuration.new.tap do |config|
9
+ config.api_key = 'abc123def456'
10
+ end
11
+ end
12
+
13
+ def build_notice(args = {})
14
+ configuration = args.delete(:configuration) || configure
15
+ Airbrake::Notice.new(configuration.merge(args))
16
+ end
17
+
18
+ def stub_request(attrs = {})
19
+ stub('request', { :parameters => { 'one' => 'two' },
20
+ :protocol => 'http',
21
+ :host => 'some.host',
22
+ :request_uri => '/some/uri',
23
+ :session => { :to_hash => { 'a' => 'b' } },
24
+ :env => { 'three' => 'four' } }.update(attrs))
25
+ end
26
+
27
+ should "set the api key" do
28
+ api_key = 'key'
29
+ notice = build_notice(:api_key => api_key)
30
+ assert_equal api_key, notice.api_key
31
+ end
32
+
33
+ should "accept a project root" do
34
+ project_root = '/path/to/project'
35
+ notice = build_notice(:project_root => project_root)
36
+ assert_equal project_root, notice.project_root
37
+ end
38
+
39
+ should "accept a component" do
40
+ assert_equal 'users_controller', build_notice(:component => 'users_controller').controller
41
+ end
42
+
43
+ should "alias the component as controller" do
44
+ assert_equal 'users_controller', build_notice(:controller => 'users_controller').component
45
+ assert_equal 'users_controller', build_notice(:component => 'users_controller').controller
46
+ end
47
+
48
+ should "accept a action" do
49
+ assert_equal 'index', build_notice(:action => 'index').action
50
+ end
51
+
52
+ should "accept a url" do
53
+ url = 'http://some.host/uri'
54
+ notice = build_notice(:url => url)
55
+ assert_equal url, notice.url
56
+ end
57
+
58
+ should "set the host name" do
59
+ notice = build_notice
60
+ assert_equal hostname, notice.hostname
61
+ end
62
+
63
+ should "accept a backtrace from an exception or hash" do
64
+ array = ["user.rb:34:in `crazy'"]
65
+ exception = build_exception
66
+ exception.set_backtrace array
67
+ backtrace = Airbrake::Backtrace.parse(array)
68
+ notice_from_exception = build_notice(:exception => exception)
69
+
70
+
71
+ assert_equal backtrace,
72
+ notice_from_exception.backtrace,
73
+ "backtrace was not correctly set from an exception"
74
+
75
+ notice_from_hash = build_notice(:backtrace => array)
76
+ assert_equal backtrace,
77
+ notice_from_hash.backtrace,
78
+ "backtrace was not correctly set from a hash"
79
+ end
80
+
81
+ should "pass its backtrace filters for parsing" do
82
+ backtrace_array = ['my/file/backtrace:3']
83
+ exception = build_exception
84
+ exception.set_backtrace(backtrace_array)
85
+ Airbrake::Backtrace.expects(:parse).with(backtrace_array, {:filters => 'foo'})
86
+
87
+ notice = Airbrake::Notice.new({:exception => exception, :backtrace_filters => 'foo'})
88
+ end
89
+
90
+ should "set the error class from an exception or hash" do
91
+ assert_accepts_exception_attribute :error_class do |exception|
92
+ exception.class.name
93
+ end
94
+ end
95
+
96
+ should "set the error message from an exception or hash" do
97
+ assert_accepts_exception_attribute :error_message do |exception|
98
+ "#{exception.class.name}: #{exception.message}"
99
+ end
100
+ end
101
+
102
+ should "accept parameters from a request or hash" do
103
+ parameters = { 'one' => 'two' }
104
+ notice_from_hash = build_notice(:parameters => parameters)
105
+ assert_equal notice_from_hash.parameters, parameters
106
+ end
107
+
108
+ should "accept session data from a session[:data] hash" do
109
+ data = { 'one' => 'two' }
110
+ notice = build_notice(:session => { :data => data })
111
+ assert_equal data, notice.session_data
112
+ end
113
+
114
+ should "accept session data from a session_data hash" do
115
+ data = { 'one' => 'two' }
116
+ notice = build_notice(:session_data => data)
117
+ assert_equal data, notice.session_data
118
+ end
119
+
120
+ should "accept an environment name" do
121
+ assert_equal 'development', build_notice(:environment_name => 'development').environment_name
122
+ end
123
+
124
+ should "accept CGI data from a hash" do
125
+ data = { 'string' => 'value' }
126
+ notice = build_notice(:cgi_data => data)
127
+ assert_equal data, notice.cgi_data, "should take CGI data from a hash"
128
+ end
129
+
130
+ should "accept notifier information" do
131
+ params = { :notifier_name => 'a name for a notifier',
132
+ :notifier_version => '1.0.5',
133
+ :notifier_url => 'http://notifiers.r.us/download' }
134
+ notice = build_notice(params)
135
+ assert_equal params[:notifier_name], notice.notifier_name
136
+ assert_equal params[:notifier_version], notice.notifier_version
137
+ assert_equal params[:notifier_url], notice.notifier_url
138
+ end
139
+
140
+ should "set sensible defaults without an exception" do
141
+ backtrace = Airbrake::Backtrace.parse(build_backtrace_array)
142
+ notice = build_notice(:backtrace => build_backtrace_array)
143
+
144
+ assert_equal 'Notification', notice.error_message
145
+ assert_array_starts_with backtrace.lines, notice.backtrace.lines
146
+ assert_equal({}, notice.parameters)
147
+ assert_equal({}, notice.session_data)
148
+ end
149
+
150
+ should "use the caller as the backtrace for an exception without a backtrace" do
151
+ filters = Airbrake::Configuration.new.backtrace_filters
152
+ backtrace = Airbrake::Backtrace.parse(caller, :filters => filters)
153
+ notice = build_notice(:exception => StandardError.new('error'), :backtrace => nil)
154
+
155
+ assert_array_starts_with backtrace.lines, notice.backtrace.lines
156
+ end
157
+
158
+ should "convert unserializable objects to strings" do
159
+ assert_serializes_hash(:parameters)
160
+ assert_serializes_hash(:cgi_data)
161
+ assert_serializes_hash(:session_data)
162
+ end
163
+
164
+ should "filter parameters" do
165
+ assert_filters_hash(:parameters)
166
+ end
167
+
168
+ should "filter cgi data" do
169
+ assert_filters_hash(:cgi_data)
170
+ end
171
+
172
+ should "filter session" do
173
+ assert_filters_hash(:session_data)
174
+ end
175
+
176
+ should "remove rack.request.form_vars" do
177
+ original = {
178
+ "rack.request.form_vars" => "story%5Btitle%5D=The+TODO+label",
179
+ "abc" => "123"
180
+ }
181
+
182
+ notice = build_notice(:cgi_data => original)
183
+ assert_equal({"abc" => "123"}, notice.cgi_data)
184
+ end
185
+
186
+ context "a Notice turned into XML" do
187
+ setup do
188
+ Airbrake.configure do |config|
189
+ config.api_key = "1234567890"
190
+ end
191
+
192
+ @exception = build_exception
193
+
194
+ @notice = build_notice({
195
+ :notifier_name => 'a name',
196
+ :notifier_version => '1.2.3',
197
+ :notifier_url => 'http://some.url/path',
198
+ :exception => @exception,
199
+ :controller => "controller",
200
+ :action => "action",
201
+ :url => "http://url.com",
202
+ :parameters => { "paramskey" => "paramsvalue",
203
+ "nestparentkey" => { "nestkey" => "nestvalue" } },
204
+ :session_data => { "sessionkey" => "sessionvalue" },
205
+ :cgi_data => { "cgikey" => "cgivalue" },
206
+ :project_root => "RAILS_ROOT",
207
+ :environment_name => "RAILS_ENV"
208
+ })
209
+
210
+ @xml = @notice.to_xml
211
+
212
+ @document = Nokogiri::XML::Document.parse(@xml)
213
+ end
214
+
215
+ should "validate against the XML schema" do
216
+ assert_valid_notice_document @document
217
+ end
218
+
219
+ should "serialize a Notice to XML when sent #to_xml" do
220
+ assert_valid_node(@document, "//api-key", @notice.api_key)
221
+
222
+ assert_valid_node(@document, "//notifier/name", @notice.notifier_name)
223
+ assert_valid_node(@document, "//notifier/version", @notice.notifier_version)
224
+ assert_valid_node(@document, "//notifier/url", @notice.notifier_url)
225
+
226
+ assert_valid_node(@document, "//error/class", @notice.error_class)
227
+ assert_valid_node(@document, "//error/message", @notice.error_message)
228
+
229
+ assert_valid_node(@document, "//error/backtrace/line/@number", @notice.backtrace.lines.first.number)
230
+ assert_valid_node(@document, "//error/backtrace/line/@file", @notice.backtrace.lines.first.file)
231
+ assert_valid_node(@document, "//error/backtrace/line/@method", @notice.backtrace.lines.first.method)
232
+
233
+ assert_valid_node(@document, "//request/url", @notice.url)
234
+ assert_valid_node(@document, "//request/component", @notice.controller)
235
+ assert_valid_node(@document, "//request/action", @notice.action)
236
+
237
+ assert_valid_node(@document, "//request/params/var/@key", "paramskey")
238
+ assert_valid_node(@document, "//request/params/var", "paramsvalue")
239
+ assert_valid_node(@document, "//request/params/var/@key", "nestparentkey")
240
+ assert_valid_node(@document, "//request/params/var/var/@key", "nestkey")
241
+ assert_valid_node(@document, "//request/params/var/var", "nestvalue")
242
+ assert_valid_node(@document, "//request/session/var/@key", "sessionkey")
243
+ assert_valid_node(@document, "//request/session/var", "sessionvalue")
244
+ assert_valid_node(@document, "//request/cgi-data/var/@key", "cgikey")
245
+ assert_valid_node(@document, "//request/cgi-data/var", "cgivalue")
246
+
247
+ assert_valid_node(@document, "//server-environment/project-root", "RAILS_ROOT")
248
+ assert_valid_node(@document, "//server-environment/environment-name", "RAILS_ENV")
249
+ assert_valid_node(@document, "//server-environment/hostname", hostname)
250
+ end
251
+ end
252
+
253
+ should "not send empty request data" do
254
+ notice = build_notice
255
+ assert_nil notice.url
256
+ assert_nil notice.controller
257
+ assert_nil notice.action
258
+
259
+ xml = notice.to_xml
260
+ document = Nokogiri::XML.parse(xml)
261
+ assert_nil document.at('//request/url')
262
+ assert_nil document.at('//request/component')
263
+ assert_nil document.at('//request/action')
264
+
265
+ assert_valid_notice_document document
266
+ end
267
+
268
+ %w(url controller action).each do |var|
269
+ should "send a request if #{var} is present" do
270
+ notice = build_notice(var.to_sym => 'value')
271
+ xml = notice.to_xml
272
+ document = Nokogiri::XML.parse(xml)
273
+ assert_not_nil document.at('//request')
274
+ end
275
+ end
276
+
277
+ %w(parameters cgi_data session_data).each do |var|
278
+ should "send a request if #{var} is present" do
279
+ notice = build_notice(var.to_sym => { 'key' => 'value' })
280
+ xml = notice.to_xml
281
+ document = Nokogiri::XML.parse(xml)
282
+ assert_not_nil document.at('//request')
283
+ end
284
+ end
285
+
286
+ should "not ignore an exception not matching ignore filters" do
287
+ notice = build_notice(:error_class => 'ArgumentError',
288
+ :ignore => ['Argument'],
289
+ :ignore_by_filters => [lambda { |notice| false }])
290
+ assert !notice.ignore?
291
+ end
292
+
293
+ should "ignore an exception with a matching error class" do
294
+ notice = build_notice(:error_class => 'ArgumentError',
295
+ :ignore => [ArgumentError])
296
+ assert notice.ignore?
297
+ end
298
+
299
+ should "ignore an exception with a matching error class name" do
300
+ notice = build_notice(:error_class => 'ArgumentError',
301
+ :ignore => ['ArgumentError'])
302
+ assert notice.ignore?
303
+ end
304
+
305
+ should "ignore an exception with a matching filter" do
306
+ filter = lambda {|notice| notice.error_class == 'ArgumentError' }
307
+ notice = build_notice(:error_class => 'ArgumentError',
308
+ :ignore_by_filters => [filter])
309
+ assert notice.ignore?
310
+ end
311
+
312
+ should "not raise without an ignore list" do
313
+ notice = build_notice(:ignore => nil, :ignore_by_filters => nil)
314
+ assert_nothing_raised do
315
+ notice.ignore?
316
+ end
317
+ end
318
+
319
+ ignored_error_classes = %w(
320
+ ActiveRecord::RecordNotFound
321
+ AbstractController::ActionNotFound
322
+ ActionController::RoutingError
323
+ ActionController::InvalidAuthenticityToken
324
+ CGI::Session::CookieStore::TamperedWithCookie
325
+ ActionController::UnknownAction
326
+ )
327
+
328
+ ignored_error_classes.each do |ignored_error_class|
329
+ should "ignore #{ignored_error_class} error by default" do
330
+ notice = build_notice(:error_class => ignored_error_class)
331
+ assert notice.ignore?
332
+ end
333
+ end
334
+
335
+ should "act like a hash" do
336
+ notice = build_notice(:error_message => 'some message')
337
+ assert_equal notice.error_message, notice[:error_message]
338
+ end
339
+
340
+ should "return params on notice[:request][:params]" do
341
+ params = { 'one' => 'two' }
342
+ notice = build_notice(:parameters => params)
343
+ assert_equal params, notice[:request][:params]
344
+ end
345
+
346
+ should "ensure #to_hash is called on objects that support it" do
347
+ assert_nothing_raised do
348
+ build_notice(:session => { :object => stub(:to_hash => {}) })
349
+ end
350
+ end
351
+
352
+ should "extract data from a rack environment hash" do
353
+ url = "https://subdomain.happylane.com:100/test/file.rb?var=value&var2=value2"
354
+ parameters = { 'var' => 'value', 'var2' => 'value2' }
355
+ env = Rack::MockRequest.env_for(url)
356
+
357
+ notice = build_notice(:rack_env => env)
358
+
359
+ assert_equal url, notice.url
360
+ assert_equal parameters, notice.parameters
361
+ assert_equal 'GET', notice.cgi_data['REQUEST_METHOD']
362
+ end
363
+
364
+ should "extract data from a rack environment hash with action_dispatch info" do
365
+ params = { 'controller' => 'users', 'action' => 'index', 'id' => '7' }
366
+ env = Rack::MockRequest.env_for('/', { 'action_dispatch.request.parameters' => params })
367
+
368
+ notice = build_notice(:rack_env => env)
369
+
370
+ assert_equal params, notice.parameters
371
+ assert_equal params['controller'], notice.component
372
+ assert_equal params['action'], notice.action
373
+ end
374
+
375
+ should "extract session data from a rack environment" do
376
+ session_data = { 'something' => 'some value' }
377
+ env = Rack::MockRequest.env_for('/', 'rack.session' => session_data)
378
+
379
+ notice = build_notice(:rack_env => env)
380
+
381
+ assert_equal session_data, notice.session_data
382
+ end
383
+
384
+ should "prefer passed session data to rack session data" do
385
+ session_data = { 'something' => 'some value' }
386
+ env = Rack::MockRequest.env_for('/')
387
+
388
+ notice = build_notice(:rack_env => env, :session_data => session_data)
389
+
390
+ assert_equal session_data, notice.session_data
391
+ end
392
+
393
+ def assert_accepts_exception_attribute(attribute, args = {}, &block)
394
+ exception = build_exception
395
+ block ||= lambda { exception.send(attribute) }
396
+ value = block.call(exception)
397
+
398
+ notice_from_exception = build_notice(args.merge(:exception => exception))
399
+
400
+ assert_equal notice_from_exception.send(attribute),
401
+ value,
402
+ "#{attribute} was not correctly set from an exception"
403
+
404
+ notice_from_hash = build_notice(args.merge(attribute => value))
405
+ assert_equal notice_from_hash.send(attribute),
406
+ value,
407
+ "#{attribute} was not correctly set from a hash"
408
+ end
409
+
410
+ def assert_serializes_hash(attribute)
411
+ [File.open(__FILE__), Proc.new { puts "boo!" }, Module.new].each do |object|
412
+ hash = {
413
+ :strange_object => object,
414
+ :sub_hash => {
415
+ :sub_object => object
416
+ },
417
+ :array => [object]
418
+ }
419
+ notice = build_notice(attribute => hash)
420
+ hash = notice.send(attribute)
421
+ assert_equal object.to_s, hash[:strange_object], "objects should be serialized"
422
+ assert_kind_of Hash, hash[:sub_hash], "subhashes should be kept"
423
+ assert_equal object.to_s, hash[:sub_hash][:sub_object], "subhash members should be serialized"
424
+ assert_kind_of Array, hash[:array], "arrays should be kept"
425
+ assert_equal object.to_s, hash[:array].first, "array members should be serialized"
426
+ end
427
+ end
428
+
429
+ def assert_valid_notice_document(document)
430
+ xsd_path = File.join(File.dirname(__FILE__), "airbrake_2_2.xsd")
431
+ schema = Nokogiri::XML::Schema.new(IO.read(xsd_path))
432
+ errors = schema.validate(document)
433
+ assert errors.empty?, errors.collect{|e| e.message }.join
434
+ end
435
+
436
+ def assert_filters_hash(attribute)
437
+ filters = ["abc", :def]
438
+ original = { 'abc' => "123", 'def' => "456", 'ghi' => "789", 'nested' => { 'abc' => '100' } }
439
+ filtered = { 'abc' => "[FILTERED]",
440
+ 'def' => "[FILTERED]",
441
+ 'ghi' => "789",
442
+ 'nested' => { 'abc' => '[FILTERED]' } }
443
+
444
+ notice = build_notice(:params_filters => filters, attribute => original)
445
+
446
+ assert_equal(filtered,
447
+ notice.send(attribute))
448
+ end
449
+
450
+ def build_backtrace_array
451
+ ["app/models/user.rb:13:in `magic'",
452
+ "app/controllers/users_controller.rb:8:in `index'"]
453
+ end
454
+
455
+ def hostname
456
+ `hostname`.chomp
457
+ end
458
+
459
+ end