test/plezi_tests.rb in plezi-0.8.4 vs test/plezi_tests.rb in plezi-0.8.5

- old
+ new

@@ -2,28 +2,53 @@ lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'open-uri' require 'plezi' +def report_before_filter(result= true) + return true if $before_tested + puts(" * Before filter test: #{PleziTestTasks::RESULTS[result]}") + $before_tested = true + true +end +def report_after_filter(result= true) + return true if $after_tested + puts(" * After filter test: #{PleziTestTasks::RESULTS[result]}") + $after_tested = true + true +end + class TestCtrl + + # this will be called before every request. def before + report_before_filter end # this will be called after every request. def after + report_after_filter end # shouldn't be available (return 404). def _hidden "do you see me?" end def index "test" end + def headers + "HTTP request: #{request[:method]} #{request[:query]} - version: #{request[:version]}\n" + (request.headers.map {|k, v| "#{k}: #{v}"} .join "\n") + end + # returns the url used to access this method + def my_url + dest = params.dup + url_for dest + end # should return a 500 internal server error message. def fail raise "Hell!" end @@ -101,108 +126,154 @@ end module PleziTestTasks module_function - RESULTS = {true => "passed", false => 'FAILED!'} + RESULTS = {true => "\e[32mpassed\e[0m"} + RESULTS.default = "\e[31mFAILED!\e[0m" + def run_tests (public_methods(false)).each {|m| method(m).call if m.to_s.match /^test_/} + report_before_filter false + report_after_filter false true end def test_sleep Plezi.run_async do begin - puts "Sleeper test: #{RESULTS[URI.parse("http://localhost:3000/sleeper").read == 'slept']}" - puts "ASync tasks test: #{RESULTS[true]}" + puts " * Sleeper test: #{RESULTS[URI.parse("http://localhost:3000/sleeper").read == 'slept']}" + puts " * ASync tasks test: #{RESULTS[true]}" rescue => e - puts "Sleeper test FAILED TO RUN!!!" + puts " **** Sleeper test FAILED TO RUN!!!" puts e end end end def test_index begin - puts "index test: #{RESULTS[URI.parse("http://localhost:3000/").read == 'test']}" + puts " * Index test: #{RESULTS[URI.parse("http://localhost:3000/").read == 'test']}" rescue => e - puts "Index test FAILED TO RUN!!!" + puts " **** Index test FAILED TO RUN!!!" puts e end end def test_ssl - puts "Connection to non-ssl and unique route test: #{RESULTS[URI.parse("http://localhost:3000/ssl").read == 'false']}" + puts " * Connection to non-ssl and unique route test: #{RESULTS[URI.parse("http://localhost:3000/ssl").read == 'false']}" uri = URI.parse("https://localhost:3030/ssl") Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https"), verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http| - puts "Connection to ssl and unique ssl route test: #{RESULTS[ http.request(Net::HTTP::Get.new(uri)).body == 'true' ]}" + puts " * Connection to ssl and unique ssl route test: #{RESULTS[ http.request(Net::HTTP::Get.new(uri)).body == 'true' ]}" end rescue => e - puts "SSL Tests FAILED to complete!!!" + puts " **** SSL Tests FAILED to complete!!!" puts e end def test_new - puts "New RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/new").read == 'new']}" + puts " * New RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/new").read == 'new']}" rescue => e - puts "New RESTful path test FAILED TO RUN!!!" + puts " **** New RESTful path test FAILED TO RUN!!!" puts e end def test_show - puts "Show RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/3").read == 'show 3']}" + puts " * Show RESTful path test: #{RESULTS[URI.parse("http://localhost:3000/3").read == 'show 3']}" rescue => e - puts "Show RESTful path test FAILED TO RUN!!!" + puts " **** Show RESTful path test FAILED TO RUN!!!" puts e end def test_update - puts "Update RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3).body == 'update 3']}" + puts " * Update RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3).body == 'update 3']}" rescue => e - puts "Update RESTful path test FAILED TO RUN!!!" + puts " **** Update RESTful path test FAILED TO RUN!!!" puts e end def test_delete - puts "Delete RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3, _method: :delete).body == 'delete 3']}" + puts " * Delete RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/"), id: 3, _method: :delete).body == 'delete 3']}" rescue => e - puts "Delete RESTful path test FAILED TO RUN!!!" + puts " **** Delete RESTful path test FAILED TO RUN!!!" puts e end def test_save - puts "Save RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/new"), data: "passed").body == 'passed']}" + puts " * Save RESTful path test: #{RESULTS[Net::HTTP.post_form( URI.parse("http://localhost:3000/new"), data: "passed").body == 'passed']}" rescue => e - puts "Save RESTful path test FAILED TO RUN!!!" + puts " **** Save RESTful path test FAILED TO RUN!!!" puts e end def test_streamed begin - puts "Streaming test: #{RESULTS[URI.parse("http://localhost:3000/streamer").read == 'streamed']}" + puts " * Streaming test: #{RESULTS[URI.parse("http://localhost:3000/streamer").read == 'streamed']}" rescue => e - puts "Streaming test FAILED TO RUN!!!" + puts " **** Streaming test FAILED TO RUN!!!" puts e end end + def test_url_for + test_url = "/some/path/test/my_url/ask/" + puts " * simple #url_for test: #{RESULTS[URI.parse("http://localhost:3000" + test_url).read == test_url]}" + test_url = "/some/another_path/my_url/ask/" + puts " * missing arguments #url_for test: #{RESULTS[URI.parse("http://localhost:3000" + test_url).read == test_url]}" + + rescue => e + puts " **** #url_for test FAILED TO RUN!!!" + puts e + end def test_404 - puts "404 not found and router continuity tests: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/get404" ).code == '404' ]}" + puts " * 404 not found and router continuity tests: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/get404" ).code == '404' ]}" rescue => e - puts "404 not found test FAILED TO RUN!!!" + puts " **** 404 not found test FAILED TO RUN!!!" puts e end def test_500 workers = Plezi::EventMachine.count_living_workers - puts "500 internal error test: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code == '500' ]}" + puts " * 500 internal error test: #{RESULTS[ Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code == '500' ]}" # cause 10 more exceptions to be raised... testing thread survival. 10.times { Net::HTTP.get_response(URI.parse "http://localhost:3000/fail" ).code } workers_after_test = Plezi::EventMachine.count_living_workers - puts "Worker survival test: #{RESULTS[workers_after_test == workers]} (#{workers_after_test} out of #{workers})" + puts " * Worker survival test: #{RESULTS[workers_after_test == workers]} (#{workers_after_test} out of #{workers})" rescue => e - puts "404 not found test FAILED TO RUN!!!" + puts " **** 500 internal error test FAILED TO RUN!!!" puts e end + def test_websocket + connection_test = broadcast_test = echo_test = false + begin + ws2 = Plezi::WebsocketClient.connect_to("wss://localhost:3030") do |msg| + break unless @is_connected || !(@is_connected = true) + puts " * Websocket broadcast message test: #{RESULTS[broadcast_test = (msg == 'echo test')]}" + response.close + go_test = false + end + puts " * Websocket SSL client test: #{RESULTS[ws2 && true]}" + ws1 = Plezi::WebsocketClient.connect_to("ws://localhost:3000") do |msg| + unless @connected + puts " * Websocket connection message test: #{RESULTS[connection_test = (msg == 'connected')]}" + @connected = true + response << "echo test" + break + end + puts " * Websocket echo message test: #{RESULTS[echo_test = (msg == 'echo test')]}" + response.close + end + + rescue => e + puts " **** Websocket tests FAILED TO RUN!!!" + puts e.message + end + remote = Plezi::WebsocketClient.connect_to("wss://echo.websocket.org/") {|msg| puts " * Extra Websocket Remote test (SSL: echo.websocket.org): #{RESULTS[msg == 'Hello websockets!']}"; response.close} + remote << "Hello websockets!" + sleep 0.3 + PL.on_shutdown {puts " * Websocket connection message test: #{RESULTS[connection_test]}" unless connection_test} + PL.on_shutdown {puts " * Websocket echo message test: #{RESULTS[echo_test]}" unless echo_test} + PL.on_shutdown {puts " * Websocket broadcast message test: #{RESULTS[broadcast_test]}" unless broadcast_test} + end end NO_PLEZI_AUTO_START = true PL.create_logger '/dev/null' @@ -211,25 +282,31 @@ route("/ssl") {|req, res| res << "false" } listen port: 3030, ssl: true route("/ssl") {|req, res| res << "true" } +shared_route '/some/:multi{path|another_path}/(:option){route|test}/(:id)/(:optional)', TestCtrl shared_route '/', TestCtrl Plezi::EventMachine.start Plezi.max_threads shoutdown_test = false Plezi.on_shutdown { shoutdown_test = true } +puts " --- Starting tests" +puts " --- Failed tests should read: #{PleziTestTasks::RESULTS[false]}" PleziTestTasks.run_tests + Plezi::EventMachine.clear_timers +sleep PLEZI_TEST_TIME if defined? PLEZI_TEST_TIME + Plezi::DSL.stop_services Plezi::EventMachine.shutdown -puts "Shutdown test: #{ PleziTestTasks::RESULTS[shoutdown_test] }" +puts " * Shutdown test: #{ PleziTestTasks::RESULTS[shoutdown_test] }"