# encoding: utf-8 module WatirSpec class Server < Sinatra::Base class << self attr_accessor :autorun def run_async case WatirSpec.platform when :java Thread.new { run! } sleep 0.1 until WatirSpec::Server.running? when :windows require "win32/process" pid = Process.create( :app_name => "#{WatirSpec.ruby} #{File.dirname(__FILE__)}/../spec_helper.rb", :process_inherit => true, :thread_inherit => true, :inherit => true ).process_id else pid = fork { run! } sleep 1 end if pid # is this really necessary? at_exit { begin Process.kill 0, pid alive = true rescue Errno::ESRCH alive = false end Process.kill(9, pid) if alive } end end def run! handler = detect_rack_handler handler.run(self, :Host => host, :Port => port) { @running = true } end def autorun @autorun ||= true end def should_run? autorun && !running? end def running? defined?(@running) && @running end end # class << Server set :public, WatirSpec.html set :static, true set :run, false set :environment, :production set :host, "localhost" if WatirSpec.platform == :windows set :port, 2000 set :server, %w[mongrel webrick] get '/' do self.class.name end post '/post_to_me' do "You posted the following content:\n#{ env['rack.input'].read }" end get '/plain_text' do content_type 'text/plain' 'This is text/plain' end get '/ajax' do sleep 10 "A slooow ajax response" end get '/charset_mismatch' do content_type 'text/html; charset=UTF-8' <<-HTML