diff options
author | Azul <azul@riseup.net> | 2016-09-22 11:07:47 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-09-22 11:07:47 +0200 |
commit | 48cdd4b1ee0685674aa998d4daa295656d80ead3 (patch) | |
tree | 7303cb6490c75e1836cc790c62a58f58e820a703 | |
parent | e1a78d46298d556b09bfcac0fa797b8e32b965d4 (diff) |
feature: 502 on ConnectionErrors
If one source raises a 502 and no other handler has any result we'll
respond with a 502 - bad gateway.
-rw-r--r-- | lib/nickserver/dispatcher.rb | 17 | ||||
-rw-r--r-- | test/integration/dispatcher_test.rb | 20 |
2 files changed, 36 insertions, 1 deletions
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb index 75f6083..869f721 100644 --- a/lib/nickserver/dispatcher.rb +++ b/lib/nickserver/dispatcher.rb @@ -24,6 +24,7 @@ require 'nickserver/request_handlers/fingerprint_handler' module Nickserver class Dispatcher + def initialize(responder) @responder = responder end @@ -45,12 +46,26 @@ module Nickserver end def handler_chain - HandlerChain.new RequestHandlers::InvalidEmailHandler, + @handler_chain ||= init_handler_chain + end + + def init_handler_chain + chain = HandlerChain.new RequestHandlers::InvalidEmailHandler, RequestHandlers::LocalEmailHandler, RequestHandlers::LeapEmailHandler, RequestHandlers::HkpEmailHandler, RequestHandlers::FingerprintHandler, + Proc.new {|_req| proxy_error_response }, Proc.new { Nickserver::Response.new(404, "404 Not Found\n") } + chain.continue_on HTTP::ConnectionError + return chain + end + + def proxy_error_response + exception = handler_chain.rescued_exceptions.first + if exception + Nickserver::Response.new(502, exception.to_s) + end end def send_response(response) diff --git a/test/integration/dispatcher_test.rb b/test/integration/dispatcher_test.rb index b3a50a0..4f13e6b 100644 --- a/test/integration/dispatcher_test.rb +++ b/test/integration/dispatcher_test.rb @@ -48,6 +48,20 @@ class Nickserver::DispatcherTest < Minitest::Test assert_response success end + def test_email_via_hkp_nicknym_unreachable + handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" } + stub_nicknym_raises + hkp_source.expect :query, success, [Nickserver::EmailAddress] + assert_response success + end + + def test_email_via_hkp_nicknym_unreachable + handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" } + stub_nicknym_raises + hkp_source.expect :query, nil, [Nickserver::EmailAddress] + assert_response response(status: 502, content: "HTTP::ConnectionError") + end + def test_email_via_nicknym handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" } nicknym_source.expect :available_for?, true, [String] @@ -90,6 +104,12 @@ class Nickserver::DispatcherTest < Minitest::Test end end + def stub_nicknym_raises + def nicknym_source.available_for?(*_args) + raise HTTP::ConnectionError + end + end + def nicknym_source @nicknym_source ||= Minitest::Mock.new end |