module Goliath::TestHelper
def self.included(mod)
def self.included(mod) Goliath.env = :test end
def create_test_request(request_data)
def create_test_request(request_data) domain = request_data.delete(:domain) || "localhost:#{@test_server_port}" path = request_data.delete(:path) || '' opts = request_data.delete(:connection_options) || {} EM::HttpRequest.new("http://#{domain}#{path}", opts) end
def delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the DELETE request.
def delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).delete(request_data) hookup_request_callbacks(req, errback, &blk) end
def fake_logger
def fake_logger Class.new do def method_missing(name, *args, &blk) nil end end.new end
def get_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the GET request.
def get_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).get(request_data) hookup_request_callbacks(req, errback, &blk) end
def head_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the HEAD request.
def head_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).head(request_data) hookup_request_callbacks(req, errback, &blk) end
def hookup_request_callbacks(req, errback, &blk)
- Api: - private
Returns:
-
(Nil)
-
Parameters:
-
blk
(Proc
) -- The callback handler to attach -
errback
(Proc
) -- An error handler to attach -
req
(EM::HttpRequest
) -- The HTTP request to augment
def hookup_request_callbacks(req, errback, &blk) req.callback &blk req.callback { stop } req.errback &errback if errback req.errback { stop } end
def method_missing(name, *args, &blk)
def method_missing(name, *args, &blk) nil end
def options_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the OPTIONS request.
def options_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).options(request_data) hookup_request_callbacks(req, errback, &blk) end
def patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the PUT request.
def patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).patch(request_data) hookup_request_callbacks(req, errback, &blk) end
def post_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the POST request.
def post_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).post(request_data) hookup_request_callbacks(req, errback, &blk) end
def put_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
-
blk
(Proc
) -- The callback block to execute -
errback
(Proc
) -- An error handler to attach -
request_data
(Hash
) -- Any data to pass to the PUT request.
def put_request(request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).put(request_data) hookup_request_callbacks(req, errback, &blk) end
def server(api, port, options = {}, &blk)
-
(Goliath::Server)
- The executed server
Parameters:
-
options
(Hash
) -- The options hash to provide to the server -
port
(Integer
) -- The port to run the server on -
api
(Class
) -- The API class to launch
def server(api, port, options = {}, &blk) op = OptionParser.new s = Goliath::Server.new s.logger = setup_logger(options) s.api = api.new s.app = Goliath::Rack::Builder.build(api, s.api) s.api.options_parser(op, options) s.options = options s.port = port s.plugins = api.plugins @test_server_port = s.port if blk s.start(&blk) s end
def setup_logger(opts)
def setup_logger(opts) return fake_logger if opts[:log_file].nil? && opts[:log_stdout].nil? log = Log4r::Logger.new('goliath') log_format = Log4r::PatternFormatter.new(:pattern => "[#{Process.pid}:%l] %d :: %m") log.level = opts[:verbose].nil? ? Log4r::INFO : Log4r::DEBUG if opts[:log_stdout] log.add(Log4r::StdoutOutputter.new('console', :formatter => log_format)) elsif opts[:log_file] file = opts[:log_file] FileUtils.mkdir_p(File.dirname(file)) log.add(Log4r::FileOutputter.new('fileOutput', {:filename => file, :trunc => false, :formatter => log_format})) end log end
def sse_client_connect(path,&blk)
def sse_client_connect(path,&blk) url = "http://localhost:#{@test_server_port}#{path}" client = SSEHelper.new(url) client.with_async_http { client.connection.start } client.listen Fiber.new { blk.call(client) }.resume if blk stop end
def stop
-
(Nil)
-
def stop EM.stop_event_loop end
def streaming_client_connect(path, &blk)
def streaming_client_connect(path, &blk) url = "http://localhost:#{@test_server_port}#{path}" client = StreamingHelper.new(url) blk.call(client) if blk stop end
def with_api(api, options = {}, &blk)
- Note: - This will not return until stop is called.
Parameters:
-
blk
(Proc
) -- The code to execute after the server is launched. -
options
(Hash
) -- The options to pass to the server -
api
(Class
) -- The API class to launch
def with_api(api, options = {}, &blk) server(api, options.delete(:port) || 9900, options, &blk) end
def ws_client_connect(path,&blk)
def ws_client_connect(path,&blk) url = "ws://localhost:#{@test_server_port}#{path}" client = WSHelper.new(url) blk.call(client) if blk stop end