diff options
author | Azul <azul@riseup.net> | 2016-08-29 12:51:00 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-08-30 08:25:16 +0200 |
commit | cd1bbe970ca17034b0e380ff2996542e3af81e31 (patch) | |
tree | 6090adda14bee5443d164a13f8f499ae92c3ed73 | |
parent | 6e2d31e3f7c515f65d92533bcdb035438461a00c (diff) |
feature: keep trying if no Host header given
So far we would error out if no host was specified in the config or
the request. It's true that we can't do local lookup if we don't
know our own domain. However we can still use HKP.
In the future we will query leaps own API for other providers. If the
host was not set in the initial request we might even proxy a request to
ourselves. Providing the Host header will prevent an infinite loop in
that case.
-rw-r--r-- | lib/nickserver/request_handlers/local_email_handler.rb | 7 | ||||
-rw-r--r-- | test/integration/dispatcher_test.rb | 16 | ||||
-rw-r--r-- | test/support/request_handler_test_helper.rb | 39 | ||||
-rw-r--r-- | test/unit/request_handlers/local_email_handler_test.rb | 45 |
4 files changed, 57 insertions, 50 deletions
diff --git a/lib/nickserver/request_handlers/local_email_handler.rb b/lib/nickserver/request_handlers/local_email_handler.rb index 1f2abc2..9e8ed48 100644 --- a/lib/nickserver/request_handlers/local_email_handler.rb +++ b/lib/nickserver/request_handlers/local_email_handler.rb @@ -9,7 +9,6 @@ module Nickserver def call(request) return nil unless request.email domain = Config.domain || request.domain - return missing_domain_response if domain.nil? || domain == '' email = EmailAddress.new(request.email) return nil unless email.domain?(domain) source.query email @@ -17,16 +16,10 @@ module Nickserver protected - attr_reader :domain - def source Nickserver::CouchDB::Source.new end - def missing_domain_response - ErrorResponse.new "HTTP request must include a Host header." - end - end end end diff --git a/test/integration/dispatcher_test.rb b/test/integration/dispatcher_test.rb index 60d252b..4ec823b 100644 --- a/test/integration/dispatcher_test.rb +++ b/test/integration/dispatcher_test.rb @@ -15,16 +15,12 @@ class Nickserver::DispatcherTest < Minitest::Test def test_missing_domain handle address: ['valid@email.tld'] - assert_response status: 500, content: "500 HTTP request must include a Host header.\n" + assert_response_from_hkp end def test_email_from_hkp handle address: ['valid@email.tld'], headers: { "Host" => "http://nickserver.me" } - source = Minitest::Mock.new - source.expect :query, Nickserver::Response.new(200, "fake content"), [Nickserver::EmailAddress] - Nickserver::Hkp::Source.stub :new, source do - assert_response status: 200, content: "200 fake content" - end + assert_response_from_hkp end def test_fingerprint_to_short @@ -62,6 +58,14 @@ class Nickserver::DispatcherTest < Minitest::Test responder.verify end + def assert_response_from_hkp + source = Minitest::Mock.new + source.expect :query, Nickserver::Response.new(200, "fake content"), [Nickserver::EmailAddress] + Nickserver::Hkp::Source.stub :new, source do + assert_response status: 200, content: "200 fake content" + end + end + def dispatcher Nickserver::Dispatcher.new responder end diff --git a/test/support/request_handler_test_helper.rb b/test/support/request_handler_test_helper.rb new file mode 100644 index 0000000..3ca89ba --- /dev/null +++ b/test/support/request_handler_test_helper.rb @@ -0,0 +1,39 @@ +module RequestHandlerTestHelper + + protected + + def assert_refuses(opts = {}) + assert_nil handle(request(opts)) + end + + def assert_handles(opts = {}) + assert handle(request(opts)) + end + + def assert_queries_for(*query_args, &block) + source_class.stub :new, source_expecting_query_for(*query_args), &block + end + + def source_expecting_query_for(*query_args) + source = Minitest::Mock.new + source.expect :query, 'response', query_args + source + end + + def assert_responds_with_error(msg, opts) + response = handle(request(opts)) + assert_equal 500, response.status + assert_equal "500 #{msg}\n", response.content + end + + def handle(request) + handler.call(request) + end + + def request(opts = {}) + params = {'address' => [opts[:email]]} + headers = {'Host' => opts[:domain]} + Nickserver::Request.new params, headers + end + +end diff --git a/test/unit/request_handlers/local_email_handler_test.rb b/test/unit/request_handlers/local_email_handler_test.rb index 8f303ec..1bfe264 100644 --- a/test/unit/request_handlers/local_email_handler_test.rb +++ b/test/unit/request_handlers/local_email_handler_test.rb @@ -1,7 +1,9 @@ require 'test_helper' +require 'support/request_handler_test_helper' require 'nickserver/request_handlers/local_email_handler' class LocalEmailHandlerTest < MiniTest::Test + include RequestHandlerTestHelper def test_no_email assert_refuses @@ -12,14 +14,13 @@ class LocalEmailHandlerTest < MiniTest::Test end def test_local_email - assert_handles email: 'me@local.tld', domain: 'local.tld' + assert_queries_for Nickserver::EmailAddress do + assert_handles email: 'me@local.tld', domain: 'local.tld' + end end def test_missing_host_header - Nickserver::Config.stub :domain, nil do - assert_responds_with_error "HTTP request must include a Host header.", - email: 'me@local.tld' - end + assert_refuses email: 'me@local.tld' end protected @@ -28,38 +29,8 @@ class LocalEmailHandlerTest < MiniTest::Test Nickserver::RequestHandlers::LocalEmailHandler.new end - def source - source = Minitest::Mock.new - source.expect :query, - 'response', - [Nickserver::EmailAddress] - source - end - - def assert_handles(opts) - Nickserver::CouchDB::Source.stub :new, source do - assert_equal 'response', handle(request(opts)) - end - end - - def assert_responds_with_error(msg, opts) - response = handle(request(opts)) - assert_equal 500, response.status - assert_equal "500 #{msg}\n", response.content - end - - def assert_refuses(opts = {}) - assert_nil handle(request(opts)) - end - - def handle(request) - handler.call(request) - end - - def request(opts = {}) - params = {'address' => [opts[:email]]} - headers = {'Host' => opts[:domain]} - Nickserver::Request.new params, headers + def source_class + Nickserver::CouchDB::Source end end |