sinatra/test/integration_test.rb

53 lines
1.2 KiB
Ruby
Raw Normal View History

2011-10-31 21:40:22 +00:00
require File.expand_path('../helper', __FILE__)
require 'rbconfig'
require 'open-uri'
require 'timeout'
2011-10-31 21:40:22 +00:00
class IntegrationTest < Test::Unit::TestCase
2011-10-31 21:59:09 +00:00
def app_file
File.expand_path('../integration/app.rb', __FILE__)
end
def port
2011-10-31 23:19:22 +00:00
5000 + (Process.pid % 1000)
end
2011-10-31 21:59:09 +00:00
def command
2011-10-31 21:40:22 +00:00
cmd = []
if RbConfig.respond_to? :ruby
cmd << RbConfig.ruby.inspect
else
file, dir = RbConfig::CONFIG.values_at('ruby_install_name', 'bindir')
cmd << File.expand_path(file, dir).inspect
end
cmd << "-I" << File.expand_path('../../lib', __FILE__).inspect
cmd << app_file.inspect << '-p' << port << '2>&1'
2011-10-31 21:59:09 +00:00
cmd.join(" ")
end
def with_server
pipe = IO.popen(command)
error = nil
Timeout.timeout(10) do
begin
2011-10-31 21:59:09 +00:00
yield
rescue Errno::ECONNREFUSED => e
error = e
sleep 0.1
retry
end
end
rescue Timeout::Error => e
$stderr.puts command, pipe.read if pipe
raise error || e
ensure
Process.kill("TERM", pipe.pid) if pipe
2011-10-31 21:40:22 +00:00
end
2011-10-31 21:59:09 +00:00
it 'starts a top level application' do
with_server do
assert_equal open("http://127.0.0.1:#{port}/app_file").read, app_file
2011-10-31 21:59:09 +00:00
end
end
2011-10-31 21:40:22 +00:00
end