hydraulic_brake 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,288 @@
1
+ require File.expand_path '../helper', __FILE__
2
+
3
+ class SenderTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ reset_config
7
+ end
8
+
9
+ def build_sender(opts = {})
10
+ HydraulicBrake.configure do |conf|
11
+ opts.each {|opt, value| conf.send(:"#{opt}=", value) }
12
+ end
13
+ end
14
+
15
+ def send_exception(args = {})
16
+ notice = args.delete(:notice) || build_notice_data
17
+ notice.stubs(:to_xml)
18
+ sender = args.delete(:sender) || build_sender(args)
19
+ sender.send_to_airbrake(notice)
20
+ end
21
+
22
+ def stub_http(options = {})
23
+ response = stub(:body => options[:body] || 'body')
24
+ http = stub(:post => response,
25
+ :read_timeout= => nil,
26
+ :open_timeout= => nil,
27
+ :ca_file= => nil,
28
+ :verify_mode= => nil,
29
+ :use_ssl= => nil)
30
+ Net::HTTP.stubs(:new => http)
31
+ http
32
+ end
33
+
34
+ should "post to Airbrake with XML passed" do
35
+ xml_notice = HydraulicBrake::Notice.new(:error_class => "FooBar", :error_message => "Foo Bar").to_xml
36
+
37
+ http = stub_http
38
+
39
+ sender = build_sender
40
+ sender.send_to_airbrake(xml_notice)
41
+
42
+ assert_received(http, :post) do |expect|
43
+ expect.with(anything, xml_notice, HydraulicBrake::HEADERS)
44
+ end
45
+ end
46
+
47
+ should "post to Airbrake with a Notice instance passed" do
48
+ notice = HydraulicBrake::Notice.new(:error_class => "FooBar", :error_message => "Foo Bar")
49
+
50
+ http = stub_http
51
+
52
+ sender = build_sender
53
+ sender.send_to_airbrake(notice)
54
+
55
+ assert_received(http, :post) do |expect|
56
+ expect.with(anything, notice.to_xml, HydraulicBrake::HEADERS)
57
+ end
58
+ end
59
+
60
+ should "post to Airbrake when using an HTTP proxy" do
61
+ response = stub(:body => 'body')
62
+ http = stub(:post => response,
63
+ :read_timeout= => nil,
64
+ :open_timeout= => nil,
65
+ :use_ssl= => nil)
66
+ proxy = stub(:new => http)
67
+ Net::HTTP.stubs(:Proxy => proxy)
68
+
69
+ url = "http://api.airbrake.io:80#{HydraulicBrake::Sender::NOTICES_URI}"
70
+ uri = URI.parse(url)
71
+
72
+ proxy_host = 'some.host'
73
+ proxy_port = 88
74
+ proxy_user = 'login'
75
+ proxy_pass = 'passwd'
76
+
77
+ send_exception(:proxy_host => proxy_host,
78
+ :proxy_port => proxy_port,
79
+ :proxy_user => proxy_user,
80
+ :proxy_pass => proxy_pass)
81
+ assert_received(http, :post) do |expect|
82
+ expect.with(uri.path, anything, HydraulicBrake::HEADERS)
83
+ end
84
+ assert_received(Net::HTTP, :Proxy) do |expect|
85
+ expect.with(proxy_host, proxy_port, proxy_user, proxy_pass)
86
+ end
87
+ end
88
+
89
+ should "return the created group's id on successful posting" do
90
+ http = stub_http(:body => '<id type="integer">3799307</id>')
91
+ assert_equal "3799307", send_exception(:secure => false)
92
+ end
93
+
94
+ context "when encountering exceptions: " do
95
+ context "HTTP connection setup problems" do
96
+ should "not be rescued" do
97
+ proxy = stub()
98
+ proxy.stubs(:new).raises(NoMemoryError)
99
+ Net::HTTP.stubs(:Proxy => proxy)
100
+
101
+ assert_raise NoMemoryError do
102
+ build_sender.send(:setup_http_connection)
103
+ end
104
+ end
105
+
106
+ should "be logged" do
107
+ proxy = stub()
108
+ proxy.stubs(:new).raises(RuntimeError)
109
+ Net::HTTP.stubs(:Proxy => proxy)
110
+
111
+ sender = build_sender
112
+ sender.expects(:log)
113
+
114
+ assert_raise RuntimeError do
115
+ sender.send(:setup_http_connection)
116
+ end
117
+
118
+ end
119
+ end
120
+
121
+ context "unexpected exception sending problems" do
122
+ should "be logged" do
123
+ sender = build_sender
124
+ sender.stubs(:setup_http_connection).raises(RuntimeError.new)
125
+
126
+ sender.expects(:log)
127
+ send_exception(:sender => sender)
128
+ end
129
+
130
+ should "return nil no matter what" do
131
+ sender = build_sender
132
+ sender.stubs(:setup_http_connection).raises(LocalJumpError)
133
+
134
+ assert_nothing_thrown do
135
+ assert_nil sender.send_to_airbrake(build_notice_data)
136
+ end
137
+ end
138
+ end
139
+
140
+ should "return nil on failed posting" do
141
+ http = stub_http
142
+ http.stubs(:post).raises(Errno::ECONNREFUSED)
143
+ assert_equal nil, send_exception(:secure => false)
144
+ end
145
+
146
+ should "not fail when posting and a timeout exception occurs" do
147
+ http = stub_http
148
+ http.stubs(:post).raises(TimeoutError)
149
+ assert_nothing_thrown do
150
+ send_exception(:secure => false)
151
+ end
152
+ end
153
+
154
+ should "not fail when posting and a connection refused exception occurs" do
155
+ http = stub_http
156
+ http.stubs(:post).raises(Errno::ECONNREFUSED)
157
+ assert_nothing_thrown do
158
+ send_exception(:secure => false)
159
+ end
160
+ end
161
+
162
+ should "not fail when posting any http exception occurs" do
163
+ http = stub_http
164
+ HydraulicBrake::Sender::HTTP_ERRORS.each do |error|
165
+ http.stubs(:post).raises(error)
166
+ assert_nothing_thrown do
167
+ send_exception(:secure => false)
168
+ end
169
+ end
170
+ end
171
+ end
172
+
173
+ context "SSL" do
174
+ should "post to the right url for non-ssl" do
175
+ http = stub_http
176
+ url = "http://api.airbrake.io:80#{HydraulicBrake::Sender::NOTICES_URI}"
177
+ uri = URI.parse(url)
178
+ send_exception(:secure => false)
179
+ assert_received(http, :post) {|expect| expect.with(uri.path, anything, HydraulicBrake::HEADERS) }
180
+ end
181
+
182
+ should "post to the right path for ssl" do
183
+ http = stub_http
184
+ send_exception(:secure => true)
185
+ assert_received(http, :post) {|expect| expect.with(HydraulicBrake::Sender::NOTICES_URI, anything, HydraulicBrake::HEADERS) }
186
+ end
187
+
188
+ should "verify the SSL peer when the use_ssl option is set to true" do
189
+ url = "https://api.airbrake.io#{HydraulicBrake::Sender::NOTICES_URI}"
190
+ uri = URI.parse(url)
191
+
192
+ real_http = Net::HTTP.new(uri.host, uri.port)
193
+ real_http.stubs(:post => nil)
194
+ proxy = stub(:new => real_http)
195
+ Net::HTTP.stubs(:Proxy => proxy)
196
+ File.stubs(:exist?).with(OpenSSL::X509::DEFAULT_CERT_FILE).returns(false)
197
+
198
+ send_exception(:secure => true)
199
+ assert(real_http.use_ssl?)
200
+ assert_equal(OpenSSL::SSL::VERIFY_PEER, real_http.verify_mode)
201
+ assert_equal(HydraulicBrake.configuration.local_cert_path, real_http.ca_file)
202
+ end
203
+
204
+ should "use the default DEFAULT_CERT_FILE if asked to" do
205
+ config = HydraulicBrake::Configuration.new
206
+ config.use_system_ssl_cert_chain = true
207
+ sender = HydraulicBrake::Sender.new(config)
208
+
209
+ assert(sender.use_system_ssl_cert_chain?)
210
+
211
+ http = sender.send(:setup_http_connection)
212
+ assert_not_equal http.ca_file, config.local_cert_path
213
+ end
214
+
215
+ should "verify the connection when the use_ssl option is set (VERIFY_PEER)" do
216
+ sender = build_sender(:secure => true)
217
+ http = sender.send(:setup_http_connection)
218
+ assert_equal(OpenSSL::SSL::VERIFY_PEER, http.verify_mode)
219
+ end
220
+
221
+ should "use the default cert (OpenSSL::X509::DEFAULT_CERT_FILE) only if explicitly told to" do
222
+ sender = build_sender(:secure => true)
223
+ http = sender.send(:setup_http_connection)
224
+
225
+ assert_equal(HydraulicBrake.configuration.local_cert_path, http.ca_file)
226
+
227
+ File.stubs(:exist?).with(OpenSSL::X509::DEFAULT_CERT_FILE).returns(true)
228
+ sender = build_sender(:secure => true, :use_system_ssl_cert_chain => true)
229
+ http = sender.send(:setup_http_connection)
230
+
231
+ assert_not_equal(HydraulicBrake.configuration.local_cert_path, http.ca_file)
232
+ assert_equal(OpenSSL::X509::DEFAULT_CERT_FILE, http.ca_file)
233
+ end
234
+
235
+ should "connect to the right port for ssl" do
236
+ stub_http
237
+ send_exception(:secure => true)
238
+ assert_received(Net::HTTP, :new) {|expect| expect.with("api.airbrake.io", 443) }
239
+ end
240
+
241
+ should "connect to the right port for non-ssl" do
242
+ stub_http
243
+ send_exception(:secure => false)
244
+ assert_received(Net::HTTP, :new) {|expect| expect.with("api.airbrake.io", 80) }
245
+ end
246
+
247
+ should "use ssl if secure" do
248
+ stub_http
249
+ send_exception(:secure => true, :host => 'example.org')
250
+ assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 443) }
251
+ end
252
+
253
+ should "not use ssl if not secure" do
254
+ stub_http
255
+ send_exception(:secure => false, :host => 'example.org')
256
+ assert_received(Net::HTTP, :new) {|expect| expect.with('example.org', 80) }
257
+ end
258
+
259
+
260
+ end
261
+
262
+ context "network timeouts" do
263
+ should "default the open timeout to 2 seconds" do
264
+ http = stub_http
265
+ send_exception
266
+ assert_received(http, :open_timeout=) {|expect| expect.with(2) }
267
+ end
268
+
269
+ should "default the read timeout to 5 seconds" do
270
+ http = stub_http
271
+ send_exception
272
+ assert_received(http, :read_timeout=) {|expect| expect.with(5) }
273
+ end
274
+
275
+ should "allow override of the open timeout" do
276
+ http = stub_http
277
+ send_exception(:http_open_timeout => 4)
278
+ assert_received(http, :open_timeout=) {|expect| expect.with(4) }
279
+ end
280
+
281
+ should "allow override of the read timeout" do
282
+ http = stub_http
283
+ send_exception(:http_read_timeout => 10)
284
+ assert_received(http, :read_timeout=) {|expect| expect.with(10) }
285
+ end
286
+ end
287
+
288
+ end
metadata ADDED
@@ -0,0 +1,259 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hydraulic_brake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephen Crosby
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: builder
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bourne
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: cucumber
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.10.6
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.6
62
+ - !ruby/object:Gem::Dependency
63
+ name: fakeweb
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.3.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: nokogiri
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.4.3.1
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.4.3.1
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 2.6.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 2.6.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: shoulda
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 2.11.3
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 2.11.3
158
+ - !ruby/object:Gem::Dependency
159
+ name: simplecov
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: yard
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ description: Sends notifications to an Airbrake server
191
+ email: stevecrozz@gmail.com
192
+ executables: []
193
+ extensions: []
194
+ extra_rdoc_files:
195
+ - README.md
196
+ files:
197
+ - .rbenv-version
198
+ - .rvmrc
199
+ - .yardopts
200
+ - Gemfile
201
+ - INSTALL
202
+ - MIT-LICENSE
203
+ - README.md
204
+ - Rakefile
205
+ - VERSION
206
+ - features/rake.feature
207
+ - features/step_definitions/rake_steps.rb
208
+ - features/support/matchers.rb
209
+ - features/support/rake/Rakefile
210
+ - features/support/terminal.rb
211
+ - hydraulic_brake.gemspec
212
+ - lib/hydraulic_brake.rb
213
+ - lib/hydraulic_brake/backtrace.rb
214
+ - lib/hydraulic_brake/configuration.rb
215
+ - lib/hydraulic_brake/notice.rb
216
+ - lib/hydraulic_brake/sender.rb
217
+ - lib/hydraulic_brake/version.rb
218
+ - lib/hydraulic_brake_tasks.rb
219
+ - resources/README.md
220
+ - resources/ca-bundle.crt
221
+ - script/integration_test.rb
222
+ - test/airbrake_2_3.xsd
223
+ - test/backtrace_test.rb
224
+ - test/configuration_test.rb
225
+ - test/helper.rb
226
+ - test/logger_test.rb
227
+ - test/notice_test.rb
228
+ - test/notifier_test.rb
229
+ - test/recursion_test.rb
230
+ - test/sender_test.rb
231
+ homepage: http://github.com/stevecrozz/hydraulic_brake
232
+ licenses:
233
+ - MIT
234
+ post_install_message:
235
+ rdoc_options: []
236
+ require_paths:
237
+ - lib
238
+ required_ruby_version: !ruby/object:Gem::Requirement
239
+ none: false
240
+ requirements:
241
+ - - ! '>='
242
+ - !ruby/object:Gem::Version
243
+ version: '0'
244
+ segments:
245
+ - 0
246
+ hash: 1855083520706619311
247
+ required_rubygems_version: !ruby/object:Gem::Requirement
248
+ none: false
249
+ requirements:
250
+ - - ! '>='
251
+ - !ruby/object:Gem::Version
252
+ version: '0'
253
+ requirements: []
254
+ rubyforge_project:
255
+ rubygems_version: 1.8.23
256
+ signing_key:
257
+ specification_version: 3
258
+ summary: Simple Airbrake client
259
+ test_files: []