diff options
author | Azul <azul@riseup.net> | 2016-06-30 11:23:45 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2016-06-30 12:04:07 +0200 |
commit | 0491e79c4e5f16d38cf87e53290394e1eccfa2e9 (patch) | |
tree | 56d0aff1fc8ca1bbd11b09bc6929f00132467898 | |
parent | 4132d6e6db3755334ace352165991487802c6b82 (diff) |
Trying to replace EM base server with reel
some tests are still broken. But at least they are running now.
-rw-r--r-- | Gemfile | 3 | ||||
-rwxr-xr-x | bin/nickserver | 6 | ||||
-rw-r--r-- | lib/nickserver/adapters/em_http.rb | 24 | ||||
-rw-r--r-- | lib/nickserver/em_server.rb | 51 | ||||
-rw-r--r-- | lib/nickserver/hkp/source.rb | 1 | ||||
-rw-r--r-- | lib/nickserver/reel_server.rb | 24 | ||||
-rw-r--r-- | lib/nickserver/request_handler.rb | 5 | ||||
-rw-r--r-- | lib/nickserver/server.rb | 9 | ||||
-rw-r--r-- | test/integration/hkp_test.rb | 26 | ||||
-rw-r--r-- | test/integration/nickserver_test.rb | 80 | ||||
-rw-r--r-- | test/test_helper.rb | 14 | ||||
-rw-r--r-- | test/unit/adapters/celluloid_http_test.rb | 23 | ||||
-rw-r--r-- | test/unit/adapters/em_http_test.rb | 25 |
13 files changed, 97 insertions, 194 deletions
@@ -2,3 +2,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in keymachine.gemspec gemspec + +gem 'byebug' + diff --git a/bin/nickserver b/bin/nickserver index f3795bf..8cf0738 100755 --- a/bin/nickserver +++ b/bin/nickserver @@ -20,7 +20,5 @@ load_local_gem require 'nickserver' Nickserver::Daemon.run('nickserver') do - EventMachine.run do - Nickserver::Server.start - end -end
\ No newline at end of file + Nickserver::Server.start +end diff --git a/lib/nickserver/adapters/em_http.rb b/lib/nickserver/adapters/em_http.rb deleted file mode 100644 index 16db5ae..0000000 --- a/lib/nickserver/adapters/em_http.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'nickserver/adapters' -require 'em-http' - -module Nickserver::Adapters - class EmHttp - - def initialize - @timeout = 5 - end - - def get(url, options = {}) - get_request(url, options).callback {|http| - yield http.response_header.status, http.response - }.errback {|http| - yield 0, http.error - } - end - - def get_request(url, options = {}) - @request = EventMachine::HttpRequest.new(url) - @request.get timeout: @timeout, query: options[:query] - end - end -end diff --git a/lib/nickserver/em_server.rb b/lib/nickserver/em_server.rb deleted file mode 100644 index a983249..0000000 --- a/lib/nickserver/em_server.rb +++ /dev/null @@ -1,51 +0,0 @@ -require 'eventmachine' -silence_warnings do - require 'evma_httpserver' -end -require 'nickserver/request_handler' - -module Nickserver - class EmServer < EM::Connection - include EM::HttpServer - - def self.start(options = {}) - EventMachine.run do - EM.start_server options[:host], options[:port], self - end - end - - def post_init - super - no_environment_strings - end - - def process_http_request - handler.respond_to params, @http_headers - end - - def respond(status, content) - response = EM::DelegatedHttpResponse.new(self) - response.status = status - response.content_type 'text/plain' - response.content = content - silence_warnings do - response.send_response - end - end - - private - - def handler - @handler ||= RequestHandler.new(self, Nickserver::Adapters::EmHttp.new) - end - - def params - if @http_query_string - CGI.parse(@http_query_string) - elsif @http_post_content - CGI.parse(@http_post_content) - end - end - - end -end diff --git a/lib/nickserver/hkp/source.rb b/lib/nickserver/hkp/source.rb index 8b2a62b..9c79546 100644 --- a/lib/nickserver/hkp/source.rb +++ b/lib/nickserver/hkp/source.rb @@ -1,3 +1,4 @@ +require 'nickserver/source' require 'nickserver/response' require 'nickserver/hkp/response' require 'nickserver/hkp/client' diff --git a/lib/nickserver/reel_server.rb b/lib/nickserver/reel_server.rb index c12c402..abda57f 100644 --- a/lib/nickserver/reel_server.rb +++ b/lib/nickserver/reel_server.rb @@ -4,25 +4,31 @@ require 'nickserver/adapters/celluloid_http' require 'nickserver/request_handler' module Nickserver - class ReelServer + class ReelServer < Reel::Server::HTTP def self.start(options = {}) - Reel::Server::HTTP.run(options[:host], options[:port]) do |connection| - # Support multiple keep-alive requests per connection - connection.each_request do |request| - handler = handler_for(request) - handler.respond_to params(request), request.headers - end + new(options[:host], options[:port]).run + end + + def initialize(host = "127.0.0.1", port = 3000) + super(host, port, &method(:on_connection)) + end + + def on_connection(connection) + connection.each_request do |request| + handler = handler_for(request) + handler.respond_to params(request), request.headers end end + protected - def self.handler_for(request) + def handler_for(request) RequestHandler.new(request, Nickserver::Adapters::CelluloidHttp.new) end - def self.params(request) + def params(request) if request.query_string CGI.parse request.query_string else diff --git a/lib/nickserver/request_handler.rb b/lib/nickserver/request_handler.rb index a3fd57d..a54653a 100644 --- a/lib/nickserver/request_handler.rb +++ b/lib/nickserver/request_handler.rb @@ -1,3 +1,6 @@ +require 'nickserver/hkp/source' +require 'nickserver/couch_db/source' + module Nickserver class RequestHandler @@ -54,7 +57,7 @@ module Nickserver return uid_domain == Config.domain else # no domain configured, use Host header - host_header = headers.split(/\0/).grep(/^Host: /).first + host_header = headers['Host'] if host_header.nil? send_error("HTTP request must include a Host header.") else diff --git a/lib/nickserver/server.rb b/lib/nickserver/server.rb index 174d6ac..b8f5604 100644 --- a/lib/nickserver/server.rb +++ b/lib/nickserver/server.rb @@ -1,9 +1,6 @@ require 'kernel_ext' require 'json' -require 'nickserver/em_server' -require 'nickserver/couch_db/source' -require 'nickserver/hkp/source' -require 'nickserver/adapters/em_http' +require 'nickserver/reel_server' # @@ -15,7 +12,7 @@ module Nickserver class Server # - # Starts the Nickserver. Must be run inside an EM.run block. + # Starts the Nickserver. # # Available options: # @@ -33,7 +30,7 @@ module Nickserver puts "Starting nickserver #{options[:host]}:#{options[:port]}" end - Nickserver::EmServer.start(options) + Nickserver::ReelServer.start(options) end diff --git a/test/integration/hkp_test.rb b/test/integration/hkp_test.rb index a824a3f..cbb8190 100644 --- a/test/integration/hkp_test.rb +++ b/test/integration/hkp_test.rb @@ -1,6 +1,6 @@ require 'test_helper' require 'nickserver/hkp/source' -require 'nickserver/adapters/em_http' +require 'nickserver/adapters/celluloid_http' class HkpTest < Minitest::Test @@ -89,9 +89,9 @@ class HkpTest < Minitest::Test ca_file = file_path('mayfirst-ca.pem') real_network do - stub_config(:hkp_url, hkp_url) do - stub_config(:hkp_ca_file, ca_file) do - #stub_config(:hkp_ca_file, file_path('autistici-ca.pem')) do + config.stub(:hkp_url, hkp_url) do + config.stub(:hkp_ca_file, ca_file) do + #config.stub(:hkp_ca_file, file_path('autistici-ca.pem')) do assert File.exist?(Nickserver::Config.hkp_ca_file) uid = 'elijah@riseup.net' assert_key_info_for_uid uid do |keys| @@ -112,26 +112,20 @@ class HkpTest < Minitest::Test end def assert_response_for_uid(uid, &block) - EM.run do - Nickserver::Hkp::Source.new(adapter).query uid do |response| - yield response - EM.stop - end + Nickserver::Hkp::Source.new(adapter).query uid do |response| + yield response end end def assert_key_info_for_uid(uid, &block) - EM.run do - Nickserver::Hkp::Source.new(adapter).search uid do |status, keys| - assert_equal 200, status - yield keys - EM.stop - end + Nickserver::Hkp::Source.new(adapter).search uid do |status, keys| + assert_equal 200, status + yield keys end end def adapter - Nickserver::Adapters::EmHttp.new + Nickserver::Adapters::CelluloidHttp.new end def fetch_key_info(body_source, uid, &block) diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb index b4ff4da..00b2642 100644 --- a/test/integration/nickserver_test.rb +++ b/test/integration/nickserver_test.rb @@ -1,21 +1,27 @@ require 'test_helper' require 'json' +require 'celluloid/test' # # Some important notes to understanding these tests: # -# (1) Requests to localhost always bypass HTTP stub. +# (1) Requests to 127.0.0.1 always bypass HTTP stub. # -# (2) All requests to nickserver are to localhost. +# (2) All requests to nickserver are to 127.0.0.1. # # (3) the "Host" header for requests to nickserver must be set (or Config.domain set) # # (4) When stubbing requests to couchdb, the couchdb host is changed from the -# default (localhost) to a dummy value (notlocalhost). +# default (127.0.0.1) to a dummy value (notlocalhost). # class NickserverTest < Minitest::Test + def setup + super + Celluloid.shutdown; Celluloid.boot + end + def test_GET_served_via_SKS uid = 'cloudadmin@leap.se' key_id = 'E818C478D3141282F7590D29D041EB11B1647490' @@ -24,9 +30,8 @@ class NickserverTest < Minitest::Test start do params = {query: {"address" => uid}} - get(params) do |http| - assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] - stop + get(params) do |response| + assert_equal file_content(:leap_public_key), JSON.parse(response.to_s)["openpgp"] end end end @@ -39,9 +44,8 @@ class NickserverTest < Minitest::Test start do params = {body: {"address" => uid}} - post(params) do |http| - assert_equal file_content(:leap_public_key), JSON.parse(http.response)["openpgp"] - stop + post(params) do |response| + assert_equal file_content(:leap_public_key), JSON.parse(response.to_s)["openpgp"] end end end @@ -52,9 +56,8 @@ class NickserverTest < Minitest::Test stub_couch_response(uid, status: 404) do start do params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal 404, http.response_header.status - stop + get(params) do |response| + assert_equal 404, response.code end end end @@ -66,9 +69,8 @@ class NickserverTest < Minitest::Test stub_couch_response(uid, body: file_content(:empty_couchdb_result)) do start do params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal 404, http.response_header.status - stop + get(params) do |response| + assert_equal 404, response.code end end end @@ -80,9 +82,8 @@ class NickserverTest < Minitest::Test stub_couch_response(uid, body: file_content(:blue_couchdb_result)) do start do params = {query: {"address" => uid}, head: {host: domain}} - get(params) do |http| - assert_equal file_content(:blue_nickserver_result), http.response - stop + get(params) do |response| + assert_equal file_content(:blue_nickserver_result), response.to_s end end end @@ -90,9 +91,8 @@ class NickserverTest < Minitest::Test def test_GET_empty start do - get({}) do |http| - assert_equal "404 Not Found\n", http.response - stop + get({}) do |response| + assert_equal "404 Not Found\n", response.to_s end end end @@ -103,53 +103,35 @@ class NickserverTest < Minitest::Test # start nickserver # def start(timeout = 1) - Timeout::timeout(timeout) do - EM.run do - Nickserver::Server.start - EM.epoll - yield - end - end - rescue Timeout::Error - flunk 'EventMachine was not stopped before the timeout expired' + server = Nickserver::ReelServer.new '127.0.0.1', config.port + yield server + ensure + server.terminate if server && server.alive? end # # http GET requests to nickserver # def get(params, &block) - request(:get, params, &block) + request(:get, params[:query], &block) end # # http POST requests to nickserver # def post(params, &block) - request(:post, params, &block) + request(:post, params[:body], &block) end # # http request to nickserver # - # this works because http requests to localhost are not stubbed, but requests to other domains are. + # this works because http requests to 127.0.0.1 are not stubbed, but requests to other domains are. # def request(method, params) - EventMachine::HttpRequest.new("http://localhost:#{Nickserver::Config.port}/").send(method,params).callback {|http| - # p http.response_header.status - # p http.response_header - # p http.response - yield http - }.errback {|http| - flunk(http.error) if http.error - EM.stop - } - end - - # - # stop nickserver - # - def stop - EM.stop + request = HTTP.request method, "http://127.0.0.1:#{config.port}/", + params: params + yield request end end diff --git a/test/test_helper.rb b/test/test_helper.rb index afdd3f9..689928c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -48,26 +48,22 @@ class Minitest::Test def stub_sks_get_reponse(key_id, opts = {}) options = {status: 200, body: ""}.merge(opts) - stub_http_request(:get, Nickserver::Config.hkp_url).with( + stub_http_request(:get, config.hkp_url).with( query: {op: 'get', search: "0x"+key_id, exact: 'on', options: 'mr'} ).to_return(options) end def stub_couch_response(uid, opts = {}) # can't stub localhost, so set couch_host to anything else - Nickserver::Config.stub :couch_host, 'notlocalhost' do + config.stub :couch_host, 'notlocalhost' do options = {status: 200, body: ""}.merge(opts) query = "\?key=#{"%22#{uid}%22"}&reduce=false" - stub_http_request(:get, /#{Regexp.escape(Nickserver::Config.couch_url)}.*#{query}/).to_return(options) + stub_http_request(:get, /#{Regexp.escape(config.couch_url)}.*#{query}/).to_return(options) yield end end - # - # temporarily stubs the config property for the duration of the given block - # - def stub_config(property, value, &block) - Nickserver::Config.stub(property, value, &block) + def config + Nickserver::Config end - end diff --git a/test/unit/adapters/celluloid_http_test.rb b/test/unit/adapters/celluloid_http_test.rb new file mode 100644 index 0000000..64c7a5d --- /dev/null +++ b/test/unit/adapters/celluloid_http_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' +require 'nickserver/adapters/celluloid_http' +require 'byebug' + +class Nickserver::Adapters::CelluloidHttpTest < Minitest::Test + + def test_successful_request + url = 'http://url.to' + stub_http_request(:get, url) + .with(query: {key: :value}) + .to_return status: 200, body: 'body' + adapter.get(url, query: {key: :value}) do |status, body| + assert_equal 200, status + assert_equal 'body', body + end + end + + protected + + def adapter + @adapter ||= Nickserver::Adapters::CelluloidHttp.new + end +end diff --git a/test/unit/adapters/em_http_test.rb b/test/unit/adapters/em_http_test.rb deleted file mode 100644 index 659ff1b..0000000 --- a/test/unit/adapters/em_http_test.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'test_helper' -require 'nickserver/adapters/em_http' - -class Nickserver::Adapters::EmHttpTest < Minitest::Test - - def test_successful_request - url = 'http://url.to' - stub_http_request(:get, url) - .with(query: {key: :value}) - .to_return status: 200, body: 'body' - EM.run do - adapter.get(url, query: {key: :value}) do |status, body| - assert_equal 200, status - assert_equal 'body', body - EM.stop - end - end - end - - protected - - def adapter - Nickserver::Adapters::EmHttp.new - end -end |