Module: Goliath::TestHelper
- Defined in:
- lib/goliath/test_helper.rb,
lib/goliath/test_helper_ws.rb,
lib/goliath/test_helper_sse.rb,
lib/goliath/test_helper_streaming.rb
Overview
Methods to help with testing Goliath APIs
Defined Under Namespace
Classes: SSEHelper, StreamingHelper, WSHelper
Constant Summary collapse
- DEFAULT_ERROR =
Proc.new { fail "API request failed" }
Instance Method Summary collapse
- #create_test_request(request_data) ⇒ Object
-
#delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a DELETE request against the currently launched API.
-
#get_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a GET request against the currently launched API.
-
#head_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a HEAD request against the currently launched API.
-
#hookup_request_callbacks(req, errback, &blk) ⇒ Nil
private
Helper method to setup common callbacks for various request methods.
-
#options_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make an OPTIONS request against the currently launched API.
-
#patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a PATCH request against the currently launched API.
-
#post_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a POST request against the currently launched API.
-
#put_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a PUT request the currently launched API.
-
#server(api, port, options = {}, &blk) ⇒ Goliath::Server
Launches an instance of a given API server.
- #setup_logger(opts) ⇒ Object
- #sse_client_connect(path, &blk) ⇒ Object
-
#stop ⇒ Nil
Stops the launched API.
- #streaming_client_connect(path, &blk) ⇒ Object
-
#with_api(api, options = {}, &blk) ⇒ Object
Wrapper for launching API and executing given code block.
- #ws_client_connect(path, &blk) ⇒ Object
Instance Method Details
#create_test_request(request_data) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/goliath/test_helper.rb', line 184 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 |
#delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a DELETE request against the currently launched API.
169 170 171 172 |
# File 'lib/goliath/test_helper.rb', line 169 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 |
#get_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a GET request against the currently launched API.
129 130 131 132 |
# File 'lib/goliath/test_helper.rb', line 129 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 |
#head_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a HEAD request against the currently launched API.
119 120 121 122 |
# File 'lib/goliath/test_helper.rb', line 119 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 |
#hookup_request_callbacks(req, errback, &blk) ⇒ Nil
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.
106 107 108 109 110 111 112 |
# File 'lib/goliath/test_helper.rb', line 106 def hookup_request_callbacks(req, errback, &blk) req.callback &blk req.callback { stop } req.errback &errback if errback req.errback { stop } end |
#options_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make an OPTIONS request against the currently launched API.
179 180 181 182 |
# File 'lib/goliath/test_helper.rb', line 179 def (request_data = {}, errback = DEFAULT_ERROR, &blk) req = create_test_request(request_data).(request_data) hookup_request_callbacks(req, errback, &blk) end |
#patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a PATCH request against the currently launched API.
159 160 161 162 |
# File 'lib/goliath/test_helper.rb', line 159 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 |
#post_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a POST request against the currently launched API.
139 140 141 142 |
# File 'lib/goliath/test_helper.rb', line 139 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 |
#put_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object
Make a PUT request the currently launched API.
149 150 151 152 |
# File 'lib/goliath/test_helper.rb', line 149 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 |
#server(api, port, options = {}, &blk) ⇒ Goliath::Server
Launches an instance of a given API server. The server will launch on the specified port.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/goliath/test_helper.rb', line 43 def server(api, port, = {}, &blk) op = OptionParser.new s = Goliath::Server.new s.logger = setup_logger() s.api = api.new s.app = Goliath::Rack::Builder.build(api, s.api) s.api.(op, ) s. = s.port = port s.plugins = api.plugins @test_server_port = s.port if blk s.start(&blk) s end |
#setup_logger(opts) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/goliath/test_helper.rb', line 59 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 |
#sse_client_connect(path, &blk) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/goliath/test_helper_sse.rb', line 67 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 |
#stop ⇒ Nil
Stops the launched API
82 83 84 |
# File 'lib/goliath/test_helper.rb', line 82 def stop EM.stop_event_loop end |
#streaming_client_connect(path, &blk) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/goliath/test_helper_streaming.rb', line 35 def streaming_client_connect(path, &blk) url = "http://localhost:#{@test_server_port}#{path}" client = StreamingHelper.new(url) blk.call(client) if blk stop end |
#with_api(api, options = {}, &blk) ⇒ Object
This will not return until stop is called.
Wrapper for launching API and executing given code block. This will start the EventMachine reactor running.
93 94 95 |
# File 'lib/goliath/test_helper.rb', line 93 def with_api(api, = {}, &blk) server(api, .delete(:port) || 9900, , &blk) end |