Class: Selenium::WebDriver::Remote::Http::Common
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Inherits: | Object |
| Defined in: | rb/lib/selenium/webdriver/remote/http/common.rb |
Constant Summary
-
BINARY_ENCODINGS =
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 31[Encoding::BINARY, Encoding::ASCII_8BIT].freeze
-
CONTENT_TYPE =
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 26'application/json' -
DEFAULT_HEADERS =
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 27{ 'Accept' => CONTENT_TYPE, 'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8" }.freeze -
MAX_REDIRECTS =
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 25
same as chromium/gecko
20
Class Attribute Summary
Class Method Summary
- .new(client_config: nil) ⇒ Common constructor
Instance Attribute Summary
- #client_config readonly
- #server_url=(url) rw
- #server_url rw private
Instance Method Summary
-
#call(verb, url, command_hash)
steep:ignore:start.
- #close
- #quit_errors
- #common_headers private
- #create_response(code, body, content_type) private
- #encode_string_to_utf8(str) private
- #ensure_utf8_encoding(obj) private
- #request private
Constructor Details
.new(client_config: nil) ⇒ Common
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 53
def initialize(client_config: nil) @client_config = client_config || ClientConfig.new end
Class Attribute Details
.extra_headers (rw)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 42
def extra_headers ClientConfig.default_extra_headers end
.extra_headers=(value) (rw)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 46
def extra_headers=(value) ClientConfig.default_extra_headers = value end
.user_agent (rw)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 34
def user_agent ClientConfig.default_user_agent end
.user_agent=(value) (rw)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 38
def user_agent=(value) ClientConfig.default_user_agent = value end
Instance Attribute Details
#client_config (readonly)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 51
attr_reader :client_config
#server_url (rw, private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 101
def server_url client_config.server_url || raise(Error::WebDriverError, 'server_url not set') end
#server_url=(url) (rw)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 57
def server_url=(url) client_config.server_url = url end
Instance Method Details
#call(verb, url, command_hash)
steep:ignore:start
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 70
def call(verb, url, command_hash) url = server_url.merge(url) unless url.is_a?(URI) headers = common_headers.dup headers['Cache-Control'] = 'no-cache' if verb == :get if command_hash command_hash = ensure_utf8_encoding(command_hash) payload = JSON.generate(command_hash) headers['Content-Length'] = payload.bytesize.to_s if %i[post put].include?(verb) WebDriver.logger.debug(" >>> #{url} | #{payload}", id: :command) WebDriver.logger.debug(" > #{headers.inspect}", id: :header) elsif verb == :post payload = '{}' headers['Content-Length'] = '2' end request verb, url, headers, payload end
#close
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 65
def close # hook for subclasses - will be called on Driver#quit end
#common_headers (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 93
def common_headers @common_headers ||= begin headers = DEFAULT_HEADERS.dup headers['User-Agent'] = client_config.user_agent headers.merge(client_config.extra_headers || {}) end end
#create_response(code, body, content_type) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 139
def create_response(code, body, content_type) code = code.to_i body = body.to_s.strip content_type = content_type.to_s WebDriver.logger.debug("<- #{body}", id: :command) if content_type.include? CONTENT_TYPE raise Error::WebDriverError, "empty body: #{content_type.inspect} (#{code})\n#{body}" if body.empty? Response.new(code, JSON.parse(body)) elsif code == 204 Response.new(code) else msg = if body.empty? "unexpected response, code=#{code}, content-type=#{content_type.inspect}" else "unexpected response, code=#{code}, content-type=#{content_type.inspect}\n#{body}" end raise Error::WebDriverError, msg end end
#encode_string_to_utf8(str) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 124
def encode_string_to_utf8(str) return str if str.encoding == Encoding::UTF_8 && str.valid_encoding? if BINARY_ENCODINGS.include?(str.encoding) result = str.dup.force_encoding(Encoding::UTF_8) return result if result.valid_encoding? end str.encode(Encoding::UTF_8) rescue EncodingError => e raise Error::WebDriverError, "Unable to encode string to UTF-8: #{e.}. " \ "String encoding: #{str.encoding}, content: #{str.inspect}" end
#ensure_utf8_encoding(obj) (private)
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 109
def ensure_utf8_encoding(obj) case obj when String encode_string_to_utf8(obj) when Array obj.map { |item| ensure_utf8_encoding(item) } when Hash obj.each_with_object({}) do |(key, value), result| result[ensure_utf8_encoding(key)] = ensure_utf8_encoding(value) end else obj end end
#quit_errors
[ GitHub ]# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 61
def quit_errors [IOError] end
#request (private)
# File 'rb/lib/selenium/webdriver/remote/http/common.rb', line 105
def request(*) raise NotImplementedError, 'subclass responsibility' end