Module: Goliath::TestHelper
- Defined in:
- lib/goliath/test_helper.rb
Overview
Methods to help with testing Goliath APIs
Instance Method Summary (collapse)
-
- (Object) build_app(klass)
Builds the rack middleware chain for the given API.
-
- (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) build_app(klass)
Builds the rack middleware chain for the given API
34 35 36 37 38 39 40 41 |
# File 'lib/goliath/test_helper.rb', line 34 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 |
- (Object) get_request(request_data = {}, errback = nil, &blk)
Make a GET request the currently launched API.
107 108 109 110 111 |
# File 'lib/goliath/test_helper.rb', line 107 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.
94 95 96 97 98 99 100 |
# File 'lib/goliath/test_helper.rb', line 94 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.
118 119 120 121 122 |
# File 'lib/goliath/test_helper.rb', line 118 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/goliath/test_helper.rb', line 50 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 = build_app(api) s.api.(op, ) s. = s.port = port s.start s end |
- (Nil) stop
Stops the launched API
67 68 69 |
# File 'lib/goliath/test_helper.rb', line 67 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.
78 79 80 81 82 83 |
# File 'lib/goliath/test_helper.rb', line 78 def with_api(api, = {}, &blk) EM.synchrony do @api_server = server(api, 9000, ) blk.call end end |