Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/http/common.rb
1990 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module Remote
23
module Http
24
class Common
25
MAX_REDIRECTS = 20 # same as chromium/gecko
26
CONTENT_TYPE = 'application/json'
27
DEFAULT_HEADERS = {
28
'Accept' => CONTENT_TYPE,
29
'Content-Type' => "#{CONTENT_TYPE}; charset=UTF-8"
30
}.freeze
31
32
class << self
33
attr_accessor :extra_headers
34
attr_writer :user_agent
35
36
def user_agent
37
@user_agent ||= "selenium/#{WebDriver::VERSION} (ruby #{Platform.os})"
38
end
39
end
40
41
attr_writer :server_url
42
43
def quit_errors
44
[IOError]
45
end
46
47
def close
48
# hook for subclasses - will be called on Driver#quit
49
end
50
51
# steep:ignore:start
52
def call(verb, url, command_hash)
53
url = server_url.merge(url) unless url.is_a?(URI)
54
headers = common_headers.dup
55
headers['Cache-Control'] = 'no-cache' if verb == :get
56
57
if command_hash
58
payload = JSON.generate(command_hash)
59
headers['Content-Length'] = payload.bytesize.to_s if %i[post put].include?(verb)
60
61
WebDriver.logger.debug(" >>> #{url} | #{payload}", id: :command)
62
WebDriver.logger.debug(" > #{headers.inspect}", id: :header)
63
elsif verb == :post
64
payload = '{}'
65
headers['Content-Length'] = '2'
66
end
67
68
request verb, url, headers, payload
69
end
70
# steep:ignore:end
71
72
private
73
74
def common_headers
75
@common_headers ||= begin
76
headers = DEFAULT_HEADERS.dup
77
headers['User-Agent'] = Common.user_agent
78
headers = headers.merge(Common.extra_headers || {})
79
80
headers
81
end
82
end
83
84
def server_url
85
return @server_url if @server_url
86
87
raise Error::WebDriverError, 'server_url not set'
88
end
89
90
def request(*)
91
raise NotImplementedError, 'subclass responsibility'
92
end
93
94
def create_response(code, body, content_type)
95
code = code.to_i
96
body = body.to_s.strip
97
content_type = content_type.to_s
98
WebDriver.logger.debug("<- #{body}", id: :command)
99
100
if content_type.include? CONTENT_TYPE
101
raise Error::WebDriverError, "empty body: #{content_type.inspect} (#{code})\n#{body}" if body.empty?
102
103
Response.new(code, JSON.parse(body))
104
elsif code == 204
105
Response.new(code)
106
else
107
msg = if body.empty?
108
"unexpected response, code=#{code}, content-type=#{content_type.inspect}"
109
else
110
"unexpected response, code=#{code}, content-type=#{content_type.inspect}\n#{body}"
111
end
112
113
raise Error::WebDriverError, msg
114
end
115
end
116
end # Common
117
end # Http
118
end # Remote
119
end # WebDriver
120
end # Selenium
121
122