Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Added proxy support and configuration of timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Romney committed Nov 25, 2008
1 parent 3805660 commit 86e78b1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
26 changes: 22 additions & 4 deletions lib/hoptoad_notifier.rb
Expand Up @@ -16,7 +16,8 @@ module HoptoadNotifier
IGNORE_DEFAULT.freeze

class << self
attr_accessor :host, :port, :secure, :api_key
attr_accessor :host, :port, :secure, :api_key, :http_open_timeout, :http_read_timeout,
:proxy_host, :proxy_port, :proxy_user, :proxy_pass
attr_reader :backtrace_filters

# Takes a block and adds it to the list of backtrace filters. When the filters
Expand All @@ -37,6 +38,16 @@ def host
@host ||= 'hoptoadapp.com'
end

# The HTTP open timeout (defaults to 2 seconds).
def http_open_timeout
@http_open_timeout ||= 2
end

# The HTTP read timeout (defaults to 5 seconds).
def http_read_timeout
@http_read_timeout ||= 5
end

# Returns the list of errors that are being ignored. The array can be appended to.
def ignore
@ignore ||= (HoptoadNotifier::IGNORE_DEFAULT.dup)
Expand Down Expand Up @@ -222,13 +233,20 @@ def clean_notice(notice) #:nodoc:

def send_to_hoptoad data #:nodoc:
url = HoptoadNotifier.url
Net::HTTP.start(url.host, url.port) do |http|

Net::HTTP::Proxy(
HoptoadNotifier.proxy_host,
HoptoadNotifier.proxy_port,
HoptoadNotifier.proxy_user,
HoptoadNotifier.proxy_pass).start(url.host, url.port) do |http|

headers = {
'Content-type' => 'application/x-yaml',
'Accept' => 'text/xml, application/xml'
}
http.read_timeout = 5 # seconds
http.open_timeout = 2 # seconds

http.read_timeout = HoptoadNotifier.http_read_timeout
http.open_timeout = HoptoadNotifier.http_open_timeout
http.use_ssl = !!HoptoadNotifier.secure
response = begin
http.post(url.path, stringify_keys(data).to_yaml, headers)
Expand Down
62 changes: 60 additions & 2 deletions test/hoptoad_notifier_test.rb
Expand Up @@ -84,12 +84,24 @@ def rescue_action e
config.secure = true
config.api_key = "1234567890abcdef"
config.ignore << [ RuntimeError ]
config.proxy_host = 'proxyhost1'
config.proxy_port = '80'
config.proxy_user = 'user'
config.proxy_pass = 'secret'
config.http_open_timeout = 2
config.http_read_timeout = 5
end

assert_equal "host", HoptoadNotifier.host
assert_equal 3333, HoptoadNotifier.port
assert_equal true, HoptoadNotifier.secure
assert_equal "1234567890abcdef", HoptoadNotifier.api_key
assert_equal 'proxyhost1', HoptoadNotifier.proxy_host
assert_equal '80', HoptoadNotifier.proxy_port
assert_equal 'user', HoptoadNotifier.proxy_user
assert_equal 'secret', HoptoadNotifier.proxy_pass
assert_equal 2, HoptoadNotifier.http_open_timeout
assert_equal 5, HoptoadNotifier.http_read_timeout
assert_equal (HoptoadNotifier::IGNORE_DEFAULT + [RuntimeError]), HoptoadNotifier.ignore
end

Expand Down Expand Up @@ -352,6 +364,39 @@ def rescue_action e
HoptoadNotifier::Sender.expects(:new).returns(@sender)
@sender.stubs(:public_environment?).returns(true)
end

context "when using an HTTP Proxy" do
setup do
@body = 'body'
@response = stub(:body => @body)
@http = stub(:post => @response, :read_timeout= => nil, :open_timeout= => nil, :use_ssl= => nil)
@sender.stubs(:logger).returns(stub(:error => nil, :info => nil))
@proxy = stub
@proxy.stubs(:start).yields(@http)

HoptoadNotifier.port = nil
HoptoadNotifier.host = nil
HoptoadNotifier.secure = false

Net::HTTP.expects(:Proxy).with(
HoptoadNotifier.proxy_host,
HoptoadNotifier.proxy_port,
HoptoadNotifier.proxy_user,
HoptoadNotifier.proxy_pass
).returns(@proxy)
end

context "on notify" do
setup { HoptoadNotifier.notify(@exception) }

before_should "post to Hoptoad" do
url = "http://hoptoadapp.com:80/notices/"
uri = URI.parse(url)
URI.expects(:parse).with(url).returns(uri)
@http.expects(:post).with(uri.path, anything, anything).returns(@response)
end
end
end

context "when stubbing out Net::HTTP" do
setup do
Expand All @@ -362,6 +407,7 @@ def rescue_action e
Net::HTTP.stubs(:start).yields(@http)
HoptoadNotifier.port = nil
HoptoadNotifier.host = nil
HoptoadNotifier.proxy_host = nil
end

context "on notify" do
Expand All @@ -383,13 +429,25 @@ def rescue_action e
@sender.expects(:send_to_hoptoad)
end

before_should "set the open timeout to 2 seconds" do
before_should "default the open timeout to 2 seconds" do
HoptoadNotifier.http_open_timeout = nil
@http.expects(:open_timeout=).with(2)
end

before_should "set the read timeout to 5 seconds" do
before_should "default the read timeout to 5 seconds" do
HoptoadNotifier.http_read_timeout = nil
@http.expects(:read_timeout=).with(5)
end

before_should "allow override of the open timeout" do
HoptoadNotifier.http_open_timeout = 4
@http.expects(:open_timeout=).with(4)
end

before_should "allow override of the read timeout" do
HoptoadNotifier.http_read_timeout = 10
@http.expects(:read_timeout=).with(10)
end

before_should "connect to the right port for ssl" do
HoptoadNotifier.secure = true
Expand Down

0 comments on commit 86e78b1

Please sign in to comment.