Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 289 lines (232 sloc) 9.0 kb
0400c67 Michael Grosser make all tests runnable via ruby test_file.rb by loading the helper abso...
grosser authored
1 require File.expand_path '../helper', __FILE__
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
2
3 class SenderTest < Test::Unit::TestCase
4
5 def setup
6 reset_config
7 end
8
96a3db7 Moved most configuration into a Configuration class
Joe Ferris authored
9 def build_sender(opts = {})
845ff38 David Pulls in ideas from https://github.com/kidsalsa/airbrake/commit/54982ba8...
dvdplm authored
10 Airbrake.configure do |conf|
11 opts.each {|opt, value| conf.send(:"#{opt}=", value) }
12 end
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
13 end
14
15 def send_exception(args = {})
16 notice = args.delete(:notice) || build_notice_data
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
17 notice.stubs(:to_xml)
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
18 sender = args.delete(:sender) || build_sender(args)
ceb0feb Jon Yurek WIP: Hoptoad -> Airbrake
jyurek authored
19 sender.send_to_airbrake(notice)
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
20 end
21
68f51a4 Jon Yurek Insert the Hoptoad Error Number into the response if possible.
jyurek authored
22 def stub_http(options = {})
23 response = stub(:body => options[:body] || 'body')
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
24 http = stub(:post => response,
25 :read_timeout= => nil,
26 :open_timeout= => nil,
2157d29 Cameron Walters (cee-dub) Always verify SSL certificates when using SSL.
cee-dub authored
27 :ca_file= => nil,
28 :verify_mode= => nil,
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
29 :use_ssl= => nil)
30 Net::HTTP.stubs(:new => http)
31 http
32 end
33
e4a2336 Zachary Anker Add the ability to pass XML directly to send_to_airbrake
zanker authored
34 should "post to Airbrake with XML passed" do
35 xml_notice = Airbrake::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, Airbrake::HEADERS)
44 end
45 end
46
47 should "post to Airbrake with a Notice instance passed" do
48 notice = Airbrake::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, Airbrake::HEADERS)
57 end
58 end
59
ceb0feb Jon Yurek WIP: Hoptoad -> Airbrake
jyurek authored
60 should "post to Airbrake when using an HTTP proxy" do
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
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
da3d5c0 Migrated to new api.airbrake.io endpoint.
Jonathan Siegel authored
69 url = "http://api.airbrake.io:80#{Airbrake::Sender::NOTICES_URI}"
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
70 uri = URI.parse(url)
e03090b Moved http properties into Sender
Joe Ferris authored
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)
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
81 assert_received(http, :post) do |expect|
ceb0feb Jon Yurek WIP: Hoptoad -> Airbrake
jyurek authored
82 expect.with(uri.path, anything, Airbrake::HEADERS)
e03090b Moved http properties into Sender
Joe Ferris authored
83 end
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
84 assert_received(Net::HTTP, :Proxy) do |expect|
e03090b Moved http properties into Sender
Joe Ferris authored
85 expect.with(proxy_host, proxy_port, proxy_user, proxy_pass)
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
86 end
87 end
88
68f51a4 Jon Yurek Insert the Hoptoad Error Number into the response if possible.
jyurek authored
89 should "return the created group's id on successful posting" do
22b4b82 Hrvoje Šimić minor test fix
shime authored
90 http = stub_http(:body => '<id type="integer">3799307</id>')
68f51a4 Jon Yurek Insert the Hoptoad Error Number into the response if possible.
jyurek authored
91 assert_equal "3799307", send_exception(:secure => false)
92 end
93
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
94 context "when encountering exceptions: " do
c8f7535 David Moves http setup code to own method
dvdplm authored
95 context "HTTP connection setup problems" do
96 should "not be rescued" do
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
97 proxy = stub()
98 proxy.stubs(:new).raises(NoMemoryError)
c8f7535 David Moves http setup code to own method
dvdplm authored
99 Net::HTTP.stubs(:Proxy => proxy)
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
100
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
101 assert_raise NoMemoryError do
c8f7535 David Moves http setup code to own method
dvdplm authored
102 build_sender.send(:setup_http_connection)
103 end
104 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
105
c8f7535 David Moves http setup code to own method
dvdplm authored
106 should "be logged" do
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
107 proxy = stub()
108 proxy.stubs(:new).raises(RuntimeError)
c8f7535 David Moves http setup code to own method
dvdplm authored
109 Net::HTTP.stubs(:Proxy => proxy)
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
110
c8f7535 David Moves http setup code to own method
dvdplm authored
111 sender = build_sender
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
112 sender.expects(:log)
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
113
114 assert_raise RuntimeError do
c8f7535 David Moves http setup code to own method
dvdplm authored
115 sender.send(:setup_http_connection)
116 end
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
117
c8f7535 David Moves http setup code to own method
dvdplm authored
118 end
119 end
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
120
c8f7535 David Moves http setup code to own method
dvdplm authored
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)
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
125
126 sender.expects(:log)
127 send_exception(:sender => sender)
c8f7535 David Moves http setup code to own method
dvdplm authored
128 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
129
c8f7535 David Moves http setup code to own method
dvdplm authored
130 should "return nil no matter what" do
131 sender = build_sender
5d645ed David Adds `pry` to the gems available in test env
dvdplm authored
132 sender.stubs(:setup_http_connection).raises(LocalJumpError)
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
133
c8f7535 David Moves http setup code to own method
dvdplm authored
134 assert_nothing_thrown do
8e55c1f Hrvoje Šimić print the failed notice details
shime authored
135 assert_nil sender.send_to_airbrake(build_notice_data)
c8f7535 David Moves http setup code to own method
dvdplm authored
136 end
137 end
138 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
139
c8f7535 David Moves http setup code to own method
dvdplm authored
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)
448b622 also handle Errno::ECONNREFUSED and other http errors
Chad Pytel authored
144 end
145
c8f7535 David Moves http setup code to own method
dvdplm authored
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
448b622 also handle Errno::ECONNREFUSED and other http errors
Chad Pytel authored
152 end
153
c8f7535 David Moves http setup code to own method
dvdplm authored
154 should "not fail when posting and a connection refused exception occurs" do
155 http = stub_http
156 http.stubs(:post).raises(Errno::ECONNREFUSED)
448b622 also handle Errno::ECONNREFUSED and other http errors
Chad Pytel authored
157 assert_nothing_thrown do
158 send_exception(:secure => false)
159 end
160 end
161
c8f7535 David Moves http setup code to own method
dvdplm authored
162 should "not fail when posting any http exception occurs" do
163 http = stub_http
164 Airbrake::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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
171 end
172
c8f7535 David Moves http setup code to own method
dvdplm authored
173 context "SSL" do
174 should "post to the right url for non-ssl" do
175 http = stub_http
da3d5c0 Migrated to new api.airbrake.io endpoint.
Jonathan Siegel authored
176 url = "http://api.airbrake.io:80#{Airbrake::Sender::NOTICES_URI}"
c8f7535 David Moves http setup code to own method
dvdplm authored
177 uri = URI.parse(url)
178 send_exception(:secure => false)
179 assert_received(http, :post) {|expect| expect.with(uri.path, anything, Airbrake::HEADERS) }
180 end
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
181
c8f7535 David Moves http setup code to own method
dvdplm authored
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(Airbrake::Sender::NOTICES_URI, anything, Airbrake::HEADERS) }
186 end
2157d29 Cameron Walters (cee-dub) Always verify SSL certificates when using SSL.
cee-dub authored
187
c8f7535 David Moves http setup code to own method
dvdplm authored
188 should "verify the SSL peer when the use_ssl option is set to true" do
da3d5c0 Migrated to new api.airbrake.io endpoint.
Jonathan Siegel authored
189 url = "https://api.airbrake.io#{Airbrake::Sender::NOTICES_URI}"
c8f7535 David Moves http setup code to own method
dvdplm authored
190 uri = URI.parse(url)
fd89187 Jon Yurek Tests the previous commits regarding SSL
jyurek authored
191
c8f7535 David Moves http setup code to own method
dvdplm authored
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)
fd89187 Jon Yurek Tests the previous commits regarding SSL
jyurek authored
197
c8f7535 David Moves http setup code to own method
dvdplm authored
198 send_exception(:secure => true)
199 assert(real_http.use_ssl?)
200 assert_equal(OpenSSL::SSL::VERIFY_PEER, real_http.verify_mode)
845ff38 David Pulls in ideas from https://github.com/kidsalsa/airbrake/commit/54982ba8...
dvdplm authored
201 assert_equal(Airbrake.configuration.local_cert_path, real_http.ca_file)
c8f7535 David Moves http setup code to own method
dvdplm authored
202 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
203
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
204 should "use the default DEFAULT_CERT_FILE if asked to" do
205 config = Airbrake::Configuration.new
206 config.use_system_ssl_cert_chain = true
93c5bca David Adds a :use_system_ssl_cert_chain configuration option to allow use of t...
dvdplm authored
207 sender = Airbrake::Sender.new(config)
208
209 assert(sender.use_system_ssl_cert_chain?)
210
211 http = sender.send(:setup_http_connection)
845ff38 David Pulls in ideas from https://github.com/kidsalsa/airbrake/commit/54982ba8...
dvdplm authored
212 assert_not_equal http.ca_file, config.local_cert_path
93c5bca David Adds a :use_system_ssl_cert_chain configuration option to allow use of t...
dvdplm authored
213 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
214
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
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
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
220
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
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)
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
224
845ff38 David Pulls in ideas from https://github.com/kidsalsa/airbrake/commit/54982ba8...
dvdplm authored
225 assert_equal(Airbrake.configuration.local_cert_path, http.ca_file)
fd89187 Jon Yurek Tests the previous commits regarding SSL
jyurek authored
226
c8f7535 David Moves http setup code to own method
dvdplm authored
227 File.stubs(:exist?).with(OpenSSL::X509::DEFAULT_CERT_FILE).returns(true)
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
228 sender = build_sender(:secure => true, :use_system_ssl_cert_chain => true)
229 http = sender.send(:setup_http_connection)
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
230
845ff38 David Pulls in ideas from https://github.com/kidsalsa/airbrake/commit/54982ba8...
dvdplm authored
231 assert_not_equal(Airbrake.configuration.local_cert_path, http.ca_file)
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
232 assert_equal(OpenSSL::X509::DEFAULT_CERT_FILE, http.ca_file)
c8f7535 David Moves http setup code to own method
dvdplm authored
233 end
5a4bfc5 David Use OpenSSL::X509::DEFAULT_CERT_FILE to connect only if configured to by...
dvdplm authored
234
c8f7535 David Moves http setup code to own method
dvdplm authored
235 should "connect to the right port for ssl" do
236 stub_http
237 send_exception(:secure => true)
da3d5c0 Migrated to new api.airbrake.io endpoint.
Jonathan Siegel authored
238 assert_received(Net::HTTP, :new) {|expect| expect.with("api.airbrake.io", 443) }
c8f7535 David Moves http setup code to own method
dvdplm authored
239 end
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
240
c8f7535 David Moves http setup code to own method
dvdplm authored
241 should "connect to the right port for non-ssl" do
242 stub_http
243 send_exception(:secure => false)
da3d5c0 Migrated to new api.airbrake.io endpoint.
Jonathan Siegel authored
244 assert_received(Net::HTTP, :new) {|expect| expect.with("api.airbrake.io", 80) }
c8f7535 David Moves http setup code to own method
dvdplm authored
245 end
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
246
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
252
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
258
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
259
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
260 end
2fb09d4 Hrvoje Šimić clean whitespace
shime authored
261
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
268
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
274
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
280
c8f7535 David Moves http setup code to own method
dvdplm authored
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
6106fad Moved HTTP responsibilities into the Sender class
Joe Ferris authored
286 end
287
288 end
Something went wrong with that request. Please try again.