forked from airbrake/airbrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.rb
332 lines (274 loc) · 8.04 KB
/
helper.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
require 'test/unit'
require 'rubygems'
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
require 'thread'
require 'shoulda'
require 'mocha'
require 'abstract_controller'
require 'action_controller'
require 'action_dispatch'
require 'active_support/dependencies'
require 'active_model'
require 'active_record'
require 'active_support/core_ext/kernel/reporting'
require 'nokogiri'
require 'rack'
require 'bourne'
require 'sham_rack'
require "airbrake"
begin require 'redgreen'; rescue LoadError; end
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
FIXTURES = Pathname.new(FIXTURE_LOAD_PATH)
SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
class RoutedRackApp
attr_reader :routes
def initialize(routes, &blk)
@routes = routes
@stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes)
end
def call(env)
@stack.call(env)
end
end
class ActionDispatch::IntegrationTest < ActiveSupport::TestCase
setup do
@routes = SharedTestRoutes
end
end
class ActionController::IntegrationTest < ActiveSupport::TestCase
def self.build_app(routes = nil)
RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware|
middleware.use "ActionDispatch::Callbacks"
middleware.use "ActionDispatch::ParamsParser"
middleware.use "ActionDispatch::Cookies"
middleware.use "ActionDispatch::Flash"
middleware.use "ActionDispatch::Head"
yield(middleware) if block_given?
end
end
self.app = build_app
# Stub Rails dispatcher so it does not get controller references and
# simply return the controller#action as Rack::Body.
class StubDispatcher < ::ActionDispatch::Routing::RouteSet::Dispatcher
protected
def controller_reference(controller_param)
controller_param
end
def dispatch(controller, action, env)
[200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]]
end
end
def self.stub_controllers
old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher
ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, StubDispatcher }
yield ActionDispatch::Routing::RouteSet.new
ensure
ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher }
ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher }
end
def with_routing(&block)
temporary_routes = ActionDispatch::Routing::RouteSet.new
old_app, self.class.app = self.class.app, self.class.build_app(temporary_routes)
old_routes = SharedTestRoutes
silence_warnings { Object.const_set(:SharedTestRoutes, temporary_routes) }
yield temporary_routes
ensure
self.class.app = old_app
silence_warnings { Object.const_set(:SharedTestRoutes, old_routes) }
end
def with_autoload_path(path)
path = File.join(File.dirname(__FILE__), "fixtures", path)
if ActiveSupport::Dependencies.autoload_paths.include?(path)
yield
else
begin
ActiveSupport::Dependencies.autoload_paths << path
yield
ensure
ActiveSupport::Dependencies.autoload_paths.reject! {|p| p == path}
ActiveSupport::Dependencies.clear
end
end
end
end
module ActionController
class Base
include ActionController::Testing
end
Base.view_paths = FIXTURE_LOAD_PATH
class TestCase
include ActionDispatch::TestProcess
setup do
@routes = SharedTestRoutes
end
end
end
# This stub emulates the Railtie including the URL helpers from a Rails application
module ActionController
class Base
include SharedTestRoutes.url_helpers
end
end
module TestMethods
def rescue_action e
raise e
end
def do_raise
raise "Airbrake"
end
def do_not_raise
render :text => "Success"
end
def do_raise_ignored
raise ActiveRecord::RecordNotFound.new("404")
end
def do_raise_not_ignored
raise ActiveRecord::StatementInvalid.new("Statement invalid")
end
def manual_notify
notify_airbrake(Exception.new)
render :text => "Success"
end
def manual_notify_ignored
notify_airbrake(ActiveRecord::RecordNotFound.new("404"))
render :text => "Success"
end
end
class Test::Unit::TestCase
def stub_sender
stub('sender', :send_to_airbrake => nil)
end
def stub_sender!
Airbrake.sender = stub_sender
end
def stub_notice
stub('notice', :to_xml => 'some yaml', :ignore? => false)
end
def stub_notice!
stub_notice.tap do |notice|
Airbrake::Notice.stubs(:new => notice)
end
end
def reset_config
Airbrake.configuration = nil
Airbrake.configure do |config|
config.api_key = 'abc123'
end
end
def clear_backtrace_filters
Airbrake.configuration.backtrace_filters.clear
end
def build_exception(opts = {})
backtrace = ["airbrake/test/helper.rb:132:in `build_exception'",
"airbrake/test/backtrace.rb:4:in `build_notice_data'",
"/var/lib/gems/1.8/gems/airbrake-2.4.5/rails/init.rb:2:in `send_exception'"]
opts = {:backtrace => backtrace}.merge(opts)
BacktracedException.new(opts)
end
def build_notice_data(exception = nil)
exception ||= build_exception
{
:api_key => 'abc123',
:error_class => exception.class.name,
:error_message => "#{exception.class.name}: #{exception.message}",
:backtrace => exception.backtrace,
:environment => { 'PATH' => '/bin', 'REQUEST_URI' => '/users/1' },
:request => {
:params => { 'controller' => 'users', 'action' => 'show', 'id' => '1' },
:rails_root => '/path/to/application',
:url => "http://test.host/users/1"
},
:session => {
:key => '123abc',
:data => { 'user_id' => '5', 'flash' => { 'notice' => 'Logged in successfully' } }
}
}
end
def assert_caught_and_sent
assert !Airbrake.sender.collected.empty?
end
def assert_caught_and_not_sent
assert Airbrake.sender.collected.empty?
end
def assert_array_starts_with(expected, actual)
assert_respond_to actual, :to_ary
array = actual.to_ary.reverse
expected.reverse.each_with_index do |value, i|
assert_equal value, array[i]
end
end
def assert_valid_node(document, xpath, content)
nodes = document.xpath(xpath)
assert nodes.any?{|node| node.content == content },
"Expected xpath #{xpath} to have content #{content}, " +
"but found #{nodes.map { |n| n.content }} in #{nodes.size} matching nodes." +
"Document:\n#{document.to_s}"
end
def assert_logged(expected)
assert_received(Airbrake, :write_verbose_log) do |expect|
expect.with {|actual| actual =~ expected }
end
end
def assert_not_logged(expected)
assert_received(Airbrake, :write_verbose_log) do |expect|
expect.with {|actual| actual =~ expected }.never
end
end
end
module DefinesConstants
def setup
@defined_constants = []
end
def teardown
@defined_constants.each do |constant|
Object.__send__(:remove_const, constant)
end
end
def define_constant(name, value)
Object.const_set(name, value)
@defined_constants << name
end
end
# Also stolen from AS 2.3.2
class Array
# Wraps the object in an Array unless it's an Array. Converts the
# object to an Array using #to_ary if it implements that.
def self.wrap(object)
case object
when nil
[]
when self
object
else
if object.respond_to?(:to_ary)
object.to_ary
else
[object]
end
end
end
end
class CollectingSender
attr_reader :collected
def initialize
@collected = []
end
def send_to_airbrake(data)
@collected << data
end
end
class BacktracedException < Exception
attr_accessor :backtrace
def initialize(opts)
@backtrace = opts[:backtrace]
end
def set_backtrace(bt)
@backtrace = bt
end
def message
"Something went wrong. Did you press the red button?"
end
end