Sha256: 02c0ec13701fe1da33127e02379791e47408df7c6d77ad891ed44e5a47b9cb4a

Contents?: true

Size: 1.98 KB

Versions: 12

Compression:

Stored size: 1.98 KB

Contents

require "net/http"

module Selenium
  module WebDriver
    module Remote
      class DefaultHttpClient
        CONTENT_TYPE = "application/json"
        DEBUG        = $VERBOSE == true

        class RetryException < StandardError; end

        def initialize(url)
          @server_url = url
        end

        def call(verb, url, *args)
          response = nil
          url      = @server_url.merge(url) unless url.kind_of?(URI)
          headers  = { "Accept" => CONTENT_TYPE }

          if args.any?
            headers.merge!("Content-Type" => "#{CONTENT_TYPE}; charset=utf-8")
            payload = args.to_json
            puts "   >>> #{payload}" if DEBUG
          end

          begin
            request = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)

            # TODO: should be checking against a maximum redirect count
            http.request(request, payload) do |res|
              if res.kind_of? Net::HTTPRedirection
                verb, payload = :get, nil
                url           = URI.parse(res["Location"])
                raise RetryException
              else
                response = create_response(res)
              end
            end

            response
          rescue RetryException
            retry
          end
        end

        private

        def http
          # ignoring SSL for now
          @http ||= Net::HTTP.new @server_url.host, @server_url.port
        end

        def create_response(res)
          puts "<- #{res.body}\n" if DEBUG
          if res.content_type == CONTENT_TYPE
            Response.new do |r|
              r.code         = res.code.to_i
              r.payload      = JSON.parse(res.body)
            end
          elsif res.code == '204'
            Response.new { |r| r.code = res.code.to_i }
          else
            raise "Unexpected content type: #{res.content_type.inspect} (#{res.code})\n#{res.body}"
          end
        end

      end # DefaultHttpClient
    end # Remote
  end # WebDriver
end # Selenium

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
selenium-webdriver-0.0.12 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.11 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.10 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.9 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.8 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.7 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.6 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.5 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.4 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.3 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.2 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb
selenium-webdriver-0.0.1 remote/client/src/rb/lib/selenium/webdriver/remote/default_http_client.rb