Module: Goliath::TestHelper
- Defined in:
- lib/goliath/test_helper.rb
Overview
Methods to help with testing Goliath APIs
Instance Method Summary (collapse)
-
- (Object) get_request(request_data = {}, errback = nil, &blk)
Make a GET request the currently launched API.
-
- (Nil) hookup_request_callbacks(req, errback, &blk)
Private
Helper method to setup common callbacks for various request methods.
-
- (Object) post_request(request_data = {}, errback = nil, &blk)
Make a POST request the currently launched API.
-
- (Goliath::Server) server(api, port = 9000, options = {})
Launches an instance of a given API server.
-
- (Nil) stop
Stops the launched API.
-
- (Object) with_api(api, options = {}, &blk)
Wrapper for launching API and executing given code block.
Instance Method Details
- (Object) get_request(request_data = {}, errback = nil, &blk)
Make a GET request the currently launched API.
95 96 97 98 99 |
# File 'lib/goliath/test_helper.rb', line 95 def get_request(request_data = {}, errback = nil, &blk) path = request_data.delete(:path) || '' req = EM::HttpRequest.new("http://localhost:9000#{path}").get(request_data) hookup_request_callbacks(req, errback, &blk) end |
- (Nil) hookup_request_callbacks(req, errback, &blk)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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.
82 83 84 85 86 87 88 |
# File 'lib/goliath/test_helper.rb', line 82 def hookup_request_callbacks(req, errback, &blk) req.callback &blk req.callback { stop } req.errback &errback if errback req.errback { stop } end |
- (Object) post_request(request_data = {}, errback = nil, &blk)
Make a POST request the currently launched API.
106 107 108 109 110 |
# File 'lib/goliath/test_helper.rb', line 106 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 |
- (Goliath::Server) server(api, port = 9000, options = {})
Launches an instance of a given API server. The server will launch on the default settings of localhost port 9000.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/goliath/test_helper.rb', line 38 def server(api, port = 9000, = {}) op = OptionParser.new s = Goliath::Server.new s.logger = mock('log').as_null_object s.api = api.new s.app = Goliath::Rack::Builder.build(api, s.api) s.api.(op, ) s. = s.port = port s.start s end |
- (Nil) stop
Stops the launched API
55 56 57 |
# File 'lib/goliath/test_helper.rb', line 55 def stop EM.stop end |
- (Object) with_api(api, options = {}, &blk)
This will not return until stop is called.
Wrapper for launching API and executing given code block. This will start the EventMachine reactor running.
66 67 68 69 70 71 |
# File 'lib/goliath/test_helper.rb', line 66 def with_api(api, = {}, &blk) EM.synchrony do @api_server = server(api, 9000, ) blk.call end end |