lib/goliath/test_helper.rb in goliath-0.9.1 vs lib/goliath/test_helper.rb in goliath-0.9.2
- old
+ new
@@ -1,9 +1,10 @@
require 'em-synchrony'
require 'em-synchrony/em-http'
require 'goliath/server'
+require 'goliath/rack/builder'
require 'rack'
module Goliath
# Methods to help with testing Goliath APIs
#
@@ -22,44 +23,31 @@
# end
# end
#
module TestHelper
def self.included(mod)
- Goliath.env = 'test'
+ Goliath.env = :test
end
- # Builds the rack middleware chain for the given API
- #
- # @param klass [Class] The API class to build the middlewares for
- # @return [Object] The Rack middleware chain
- def build_app(klass)
- ::Rack::Builder.new do
- klass.middlewares.each do |mw|
- use(*(mw[0..1].compact), &mw[2])
- end
- run klass.new
- end
- end
-
# Launches an instance of a given API server. The server
# will launch on the default settings of localhost port 9000.
#
# @param api [Class] The API class to launch
# @param port [Integer] The port to run the server on
# @param options [Hash] The options hash to provide to the server
# @return [Goliath::Server] The executed server
- def server(api, port = 9000, options = {})
+ def server(api, port = 9000, options = {}, &blk)
op = OptionParser.new
s = Goliath::Server.new
s.logger = mock('log').as_null_object
s.api = api.new
- s.app = build_app(api)
+ s.app = Goliath::Rack::Builder.build(api, s.api)
s.api.options_parser(op, options)
s.options = options
s.port = port
- s.start
+ s.start(&blk)
s
end
# Stops the launched API
#
@@ -74,14 +62,11 @@
# @param api [Class] The API class to launch
# @param options [Hash] The options to pass to the server
# @param blk [Proc] The code to execute after the server is launched.
# @note This will not return until stop is called.
def with_api(api, options = {}, &blk)
- EM.synchrony do
- @api_server = server(api, 9000, options)
- blk.call
- end
+ server(api, 9000, options, &blk)
end
# Helper method to setup common callbacks for various request methods.
# The given err and callback handlers will be attached and a callback
# to stop the reactor will be added.
@@ -97,10 +82,21 @@
req.errback &errback if errback
req.errback { stop }
end
+ # Make a HEAD request the currently launched API.
+ #
+ # @param request_data [Hash] Any data to pass to the HEAD request.
+ # @param errback [Proc] An error handler to attach
+ # @param blk [Proc] The callback block to execute
+ def head_request(request_data = {}, errback = nil, &blk)
+ path = request_data.delete(:path) || ''
+ req = EM::HttpRequest.new("http://localhost:9000#{path}").head(request_data)
+ hookup_request_callbacks(req, errback, &blk)
+ end
+
# Make a GET request the currently launched API.
#
# @param request_data [Hash] Any data to pass to the GET request.
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
@@ -116,9 +112,31 @@
# @param errback [Proc] An error handler to attach
# @param blk [Proc] The callback block to execute
def post_request(request_data = {}, errback = nil, &blk)
path = request_data.delete(:path) || ''
req = EM::HttpRequest.new("http://localhost:9000#{path}").post(request_data)
+ hookup_request_callbacks(req, errback, &blk)
+ end
+
+ # Make a PUT request the currently launched API.
+ #
+ # @param request_data [Hash] Any data to pass to the PUT request.
+ # @param errback [Proc] An error handler to attach
+ # @param blk [Proc] The callback block to execute
+ def put_request(request_data = {}, errback = nil, &blk)
+ path = request_data.delete(:path) || ''
+ req = EM::HttpRequest.new("http://localhost:9000#{path}").put(request_data)
+ hookup_request_callbacks(req, errback, &blk)
+ end
+
+ # Make a DELETE request the currently launched API.
+ #
+ # @param request_data [Hash] Any data to pass to the DELETE request.
+ # @param errback [Proc] An error handler to attach
+ # @param blk [Proc] The callback block to execute
+ def delete_request(request_data = {}, errback = nil, &blk)
+ path = request_data.delete(:path) || ''
+ req = EM::HttpRequest.new("http://localhost:9000#{path}").delete(request_data)
hookup_request_callbacks(req, errback, &blk)
end
end
end