From 0491e79c4e5f16d38cf87e53290394e1eccfa2e9 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 30 Jun 2016 11:23:45 +0200 Subject: Trying to replace EM base server with reel some tests are still broken. But at least they are running now. --- test/integration/nickserver_test.rb | 80 ++++++++++++++----------------------- 1 file changed, 31 insertions(+), 49 deletions(-) (limited to 'test/integration/nickserver_test.rb') 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 -- cgit v1.2.3 From 7802da3d9684c5c78d34e0154f827221803b207b Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 30 Jun 2016 13:17:08 +0200 Subject: fix host header handling and tests --- test/integration/nickserver_test.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'test/integration/nickserver_test.rb') diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb index 00b2642..f30d2ba 100644 --- a/test/integration/nickserver_test.rb +++ b/test/integration/nickserver_test.rb @@ -55,7 +55,7 @@ class NickserverTest < Minitest::Test uid = "bananas@" + domain stub_couch_response(uid, status: 404) do start do - params = {query: {"address" => uid}, head: {host: domain}} + params = {query: {"address" => uid}, head: {"Host" => domain}} get(params) do |response| assert_equal 404, response.code end @@ -81,7 +81,7 @@ class NickserverTest < Minitest::Test uid = "blue@" + domain stub_couch_response(uid, body: file_content(:blue_couchdb_result)) do start do - params = {query: {"address" => uid}, head: {host: domain}} + params = {query: {"address" => uid}, head: {"Host" => domain}} get(params) do |response| assert_equal file_content(:blue_nickserver_result), response.to_s end @@ -112,15 +112,15 @@ class NickserverTest < Minitest::Test # # http GET requests to nickserver # - def get(params, &block) - request(:get, params[:query], &block) + def get(options = {}, &block) + request(:get, params: options[:query], head: options[:head], &block) end # # http POST requests to nickserver # - def post(params, &block) - request(:post, params[:body], &block) + def post(options, &block) + request(:post, params: options[:body], head: options[:head], &block) end # @@ -128,10 +128,11 @@ class NickserverTest < Minitest::Test # # this works because http requests to 127.0.0.1 are not stubbed, but requests to other domains are. # - def request(method, params) - request = HTTP.request method, "http://127.0.0.1:#{config.port}/", - params: params - yield request + def request(method, options = {}) + response = HTTP. + headers(options.delete(:head)). + request method, "http://127.0.0.1:#{config.port}/", options + yield response end end -- cgit v1.2.3 From 8a664a39bc3dd77a9c53fa5931f81c2b2b8b7295 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 2 Jul 2016 10:49:48 +0200 Subject: minor: silence warnings and cleanup --- test/integration/nickserver_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'test/integration/nickserver_test.rb') diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb index f30d2ba..d179d7e 100644 --- a/test/integration/nickserver_test.rb +++ b/test/integration/nickserver_test.rb @@ -1,6 +1,5 @@ require 'test_helper' require 'json' -require 'celluloid/test' # # Some important notes to understanding these tests: -- cgit v1.2.3 From fb2d7e6f8f1fceefbc8964d34369a867eb8f25bb Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 2 Jul 2016 12:03:46 +0200 Subject: refactor: replace blocks/yields with returns This became possible because we now use celluloid. Celluloid handles asynchronity without the need for callbacks or blocks. --- test/integration/nickserver_test.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'test/integration/nickserver_test.rb') diff --git a/test/integration/nickserver_test.rb b/test/integration/nickserver_test.rb index d179d7e..710c3a1 100644 --- a/test/integration/nickserver_test.rb +++ b/test/integration/nickserver_test.rb @@ -18,7 +18,12 @@ class NickserverTest < Minitest::Test def setup super - Celluloid.shutdown; Celluloid.boot + Celluloid.boot + end + + def teardown + Celluloid.shutdown + super end def test_GET_served_via_SKS -- cgit v1.2.3