Class: Sinatra::Runner
- Inherits:
-
Object
- Object
- Sinatra::Runner
- Defined in:
- lib/sinatra/runner.rb
Overview
NOTE: This feature is experimental, and missing tests!
Helps you spinning up and shutting down your own sinatra app. This is especially helpful for running real network tests against a sinatra backend.
The backend server could look like the following (in test/server.rb).
require "sinatra"
get "/" do
"Cheers from test server"
end
get "/ping" do
"1"
end
Note that you need to implement a ping action for internal use.
Next, you need to write your runner.
require 'sinatra/runner'
class Runner < Sinatra::Runner
def app_file
File.("server.rb", __dir__)
end
end
Override Runner#app_file, #command, #port, #protocol and #ping_path for customization.
**Don’t forget to override #app_file specific to your application!**
Wherever you need this test backend, here’s how you manage it. The following example assumes you have a test in your app that needs to be run against your test backend.
runner = ServerRunner.new
runner.run
# ..tests against localhost:4567 here..
runner.kill
For an example, check github.com/apotonick/roar/blob/master/test/integration/runner.rb
Instance Method Summary collapse
- #app_file ⇒ Object
- #get(url) ⇒ Object
- #get_response(url) ⇒ Object
- #get_stream(url = '/stream', &block) ⇒ Object
- #kill ⇒ Object
- #log ⇒ Object
- #run ⇒ Object
Instance Method Details
#app_file ⇒ Object
53 54 55 |
# File 'lib/sinatra/runner.rb', line 53 def app_file File.('server.rb', __dir__) end |
#get(url) ⇒ Object
72 73 74 |
# File 'lib/sinatra/runner.rb', line 72 def get(url) Timeout.timeout(1) { get_url("#{protocol}://127.0.0.1:#{port}#{url}") } end |
#get_response(url) ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/sinatra/runner.rb', line 85 def get_response(url) Net::HTTP.start '127.0.0.1', port do |http| request = Net::HTTP::Get.new url http.request request do |response| response end end end |
#get_stream(url = '/stream', &block) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/sinatra/runner.rb', line 76 def get_stream(url = '/stream', &block) Net::HTTP.start '127.0.0.1', port do |http| request = Net::HTTP::Get.new url http.request request do |response| response.read_body(&block) end end end |
#kill ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/sinatra/runner.rb', line 63 def kill return unless pipe Process.kill('KILL', pipe.pid) rescue NotImplementedError system "kill -9 #{pipe.pid}" rescue Errno::ESRCH end |
#log ⇒ Object
94 95 96 97 98 99 |
# File 'lib/sinatra/runner.rb', line 94 def log @log ||= +'' loop { @log << pipe.read_nonblock(1) } rescue Exception @log end |
#run ⇒ Object
57 58 59 60 61 |
# File 'lib/sinatra/runner.rb', line 57 def run @pipe = start @started = Time.now warn "#{server} up and running on port #{port}" if ping end |