Sha256: f904cc61899831cbb01c76691c8dd01d21dcc362dc56c38edbf29a95a3880a02

Contents?: true

Size: 1.73 KB

Versions: 4

Compression:

Stored size: 1.73 KB

Contents

require 'rspec/core'
require 'uri'
require 'net/http'

module WatirWebdriverRails

  class Identify
    def initialize(app)
      @app = app
    end

    def call(env)
      if env["PATH_INFO"] == "/__identify__"
        [200, {}, [@app.object_id.to_s]]
      else
        @app.call(env)
      end
    end
  end

  class << self
    attr_accessor :host, :port, :server_boot_timeout, :app, :server_running
    
    
    
    def run_server
      return if @server_running == true
      
      @app = Rack::Builder.new {
        map "/" do
          if Rails.version.to_f >= 3.0
            run Rails.application  
          else # Rails 2
            use Rails::Rack::Static
            run ActionController::Dispatcher.new
          end
        end
      }.to_app
      
      Thread.new do
        begin
          require 'rack/handler/thin'
          Thin::Logging.silent = true
          Rack::Handler::Thin.run(Identify.new(app), :Port => @port)
        rescue LoadError
          require 'rack/handler/webrick'
          Rack::Handler::WEBrick.run(Identify.new(app), :Port => @port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))
        end
      end

      timeout(@server_boot_timeout) do
        if responsive? 
          true 
        else 
          sleep(0.5)
          false 
        end
      end
      
      @server_running = true
    end
    
    
    def responsive?
      res = Net::HTTP.start("127.0.0.1", @port) { |http| http.get('/__identify__') }
    
      if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
        return res.body == @app.object_id.to_s
      end
    rescue Errno::ECONNREFUSED, Errno::EBADF
      return false
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
watir-webdriver-rails-0.0.6 lib/watir-webdriver-rails/rails.rb
watir-webdriver-rails-0.0.5 lib/watir-webdriver-rails/rails.rb
watir-webdriver-rails-0.0.4 lib/watir-webdriver-rails/rails.rb
watir-webdriver-rails-0.0.3 lib/watir-webdriver-rails/rails.rb