Skip to content

Commit 6f37b95

Browse files
committed
[rb] implement client config class
1 parent b3b23f5 commit 6f37b95

File tree

11 files changed

+206
-24
lines changed

11 files changed

+206
-24
lines changed

rb/lib/selenium/webdriver/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# under the License.
1919

2020
require 'selenium/webdriver/common/error'
21+
require 'selenium/webdriver/common/client_config'
2122
require 'selenium/webdriver/common/local_driver'
2223
require 'selenium/webdriver/common/driver_finder'
2324
require 'selenium/webdriver/common/platform'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#
23+
# Immutable Configuration for HTTP clients.
24+
#
25+
26+
ClientConfig = Data.define(
27+
:open_timeout,
28+
:read_timeout,
29+
:proxy,
30+
:extra_headers,
31+
:user_agent,
32+
:server_url
33+
) do
34+
def initialize(
35+
open_timeout: nil,
36+
read_timeout: nil,
37+
proxy: nil,
38+
extra_headers: nil,
39+
user_agent: nil,
40+
server_url: nil
41+
)
42+
super
43+
end
44+
end
45+
end
46+
end

rb/lib/selenium/webdriver/common/driver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ def ref
319319

320320
attr_reader :bridge
321321

322-
def create_bridge(caps:, url:, http_client: nil)
322+
def create_bridge(caps:, url: nil, http_client: nil, client_config: nil)
323323
klass = caps['webSocketUrl'] ? Remote::BiDiBridge : Remote::Bridge
324-
klass.new(http_client: http_client, url: url).tap do |bridge|
324+
klass.new(http_client: http_client, url: url, client_config: client_config).tap do |bridge|
325325
bridge.create_session(caps)
326326
end
327327
end

rb/lib/selenium/webdriver/remote/bridge.rb

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ def element_class
5454
# Initializes the bridge with the given server URL
5555
# @param [String, URI] url url for the remote server
5656
# @param [Object] http_client an HTTP client instance that implements the same protocol as Http::Default
57+
# @param [ClientConfig] client_config configuration for the HTTP client
5758
# @api private
5859
#
5960

60-
def initialize(url:, http_client: nil)
61-
uri = url.is_a?(URI) ? url : URI.parse(url)
62-
uri.path += '/' unless uri.path.end_with?('/')
61+
def initialize(url: nil, http_client: nil, client_config: nil)
62+
if http_client && client_config
63+
raise Error::WebDriverError, 'Cannot specify both http_client and client_config'
64+
end
6365

64-
@http = http_client || Http::Default.new
65-
@http.server_url = uri
66-
@file_detector = nil
66+
@http = http_client || create_http_client(client_config, url: url)
6767

68+
@file_detector = nil
6869
@locator_converter = self.class.locator_converter
6970
end
7071

@@ -93,6 +94,8 @@ def create_session(capabilities)
9394
extend(WebDriver::Safari::Features)
9495
when 'internet explorer'
9596
extend(WebDriver::IE::Features)
97+
else
98+
raise Error::WebDriverError, "Unknown browser name: #{@capabilities[:browser_name]}"
9699
end
97100
end
98101

@@ -662,6 +665,41 @@ def prepare_capabilities_payload(capabilities)
662665
capabilities = {alwaysMatch: capabilities} if !capabilities['alwaysMatch'] && !capabilities['firstMatch']
663666
{capabilities: capabilities}
664667
end
668+
669+
def create_http_client(client_config, url: nil)
670+
http = build_http_client(client_config)
671+
validate_server_url_args(url, client_config)
672+
http.server_url = normalize_url(url || client_config&.server_url)
673+
http
674+
end
675+
676+
def build_http_client(client_config)
677+
if client_config
678+
http = Http::Default.new(open_timeout: client_config.open_timeout,
679+
read_timeout: client_config.read_timeout)
680+
http.proxy = client_config.proxy if client_config.proxy
681+
682+
Http::Common.extra_headers = client_config.extra_headers if client_config.extra_headers
683+
Http::Common.user_agent = client_config.user_agent if client_config.user_agent
684+
else
685+
http = Http::Default.new
686+
end
687+
http
688+
end
689+
690+
def validate_server_url_args(url, client_config)
691+
if url && client_config&.server_url
692+
raise Error::WebDriverError, 'Cannot specify url in both keyword and http_client'
693+
elsif url.nil? && !client_config&.server_url
694+
raise Error::WebDriverError, 'No server URL provided'
695+
end
696+
end
697+
698+
def normalize_url(url)
699+
url = URI.parse(url) if url && !url.is_a?(URI)
700+
url&.path += '/' unless url&.path&.end_with?('/')
701+
url
702+
end
665703
end # Bridge
666704
end # Remote
667705
end # WebDriver

rb/lib/selenium/webdriver/remote/http/common.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def user_agent
4040

4141
attr_writer :server_url
4242

43+
def server_url?
44+
!@server_url.nil?
45+
end
46+
4347
def quit_errors
4448
[IOError]
4549
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Selenium
2+
module WebDriver
3+
4+
ClientConfig: Class
5+
6+
class ClientConfig
7+
attr_reader open_timeout: Integer?
8+
attr_reader read_timeout: Integer?
9+
attr_reader proxy: Selenium::WebDriver::Proxy?
10+
attr_reader extra_headers: Hash[String, String]?
11+
attr_reader user_agent: String?
12+
attr_reader server_url: (String | URI)?
13+
14+
def initialize: (
15+
?open_timeout: Integer?,
16+
?read_timeout: Integer?,
17+
?proxy: Selenium::WebDriver::Proxy?,
18+
?extra_headers: Hash[String, String]?,
19+
?user_agent: String?,
20+
?server_url: (String | URI)?
21+
) -> void
22+
23+
def with: (**untyped) -> ClientConfig
24+
def to_h: -> Hash[Symbol, untyped]
25+
def deconstruct_keys: (Array[Symbol]?) -> Hash[Symbol, untyped]
26+
end
27+
end
28+
end

rb/sig/lib/selenium/webdriver/common/driver.rbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module Selenium
6969

7070
attr_reader bridge: untyped
7171

72-
def create_bridge: (caps: untyped, url: untyped, ?http_client: untyped) -> untyped
72+
def create_bridge: (caps: Remote::Capabilities, url: (String | URI)?, ?http_client: Remote::Http::Common?, ?client_config: ClientConfig?) -> Remote::Bridge
7373

7474
def service_url: (untyped service) -> untyped
7575

rb/sig/lib/selenium/webdriver/remote/bridge.rbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Selenium
3030

3131
attr_reader capabilities: untyped
3232

33-
def initialize: (url: String | URI, ?http_client: untyped?) -> void
33+
def initialize: (?url: (String | URI)?, ?http_client: untyped?, ?client_config: ClientConfig?) -> void
3434

3535
def bidi: -> WebDriver::Error::WebDriverError
3636

@@ -248,6 +248,14 @@ module Selenium
248248

249249
def convert_locator: (untyped how, untyped what) -> ::Array[untyped]
250250

251+
def create_http_client: (?ClientConfig? client_config, ?url: (String | URI)?) -> Http::Common
252+
253+
def build_http_client: (ClientConfig? client_config) -> Http::Common
254+
255+
def validate_server_url_args: ((String | URI)? url, ClientConfig? client_config) -> void
256+
257+
def normalize_url: ((String | URI)? url) -> URI?
258+
251259
ESCAPE_CSS_REGEXP: ::Regexp
252260

253261
UNICODE_CODE_POINT: 30

rb/sig/lib/selenium/webdriver/remote/http/common.rbs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@ module Selenium
1111

1212
@common_headers: Hash[String, untyped]
1313

14-
attr_accessor self.extra_headers: Hash[String, untyped]
14+
attr_accessor self.extra_headers: Hash[String, untyped]?
1515

16-
attr_writer self.user_agent: String
16+
attr_writer self.user_agent: String?
1717

1818
def self.user_agent: -> String
1919

20-
attr_writer server_url: String
20+
attr_writer server_url?: URI
2121

2222
def quit_errors: () -> Array[untyped]
2323

2424
def close: () -> nil
2525

2626
def call: (untyped verb, untyped url, untyped command_hash) -> untyped
2727

28+
def server_url?: -> bool
29+
2830
private
2931

3032
def common_headers: -> Hash[String, untyped]
3133

32-
def server_url: () -> String
34+
def server_url: () -> URI
3335

3436
def request: (*untyped) -> untyped
3537

rb/sig/lib/selenium/webdriver/remote/http/default.rbs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ module Selenium
44
module Http
55
# @api private
66
class Default < Common
7-
@open_timeout: untyped
7+
@open_timeout: Integer
88

9-
@read_timeout: untyped
9+
@read_timeout: Integer
1010

1111
@http: untyped
1212

13-
@proxy: untyped
13+
@proxy: Proxy
1414

15-
attr_writer proxy: untyped
15+
attr_writer proxy: Proxy?
1616

17-
attr_accessor open_timeout: untyped
17+
attr_accessor open_timeout: Integer
1818

19-
attr_accessor read_timeout: untyped
19+
attr_accessor read_timeout: Integer
2020

21-
def initialize: (?open_timeout: untyped?, ?read_timeout: untyped?) -> void
21+
def initialize: (?open_timeout: Integer?, ?read_timeout: Integer?) -> void
2222

2323
def close: () -> untyped
2424

@@ -40,7 +40,7 @@ module Selenium
4040

4141
def proxy: () -> untyped
4242

43-
def use_proxy?: () -> untyped
43+
def use_proxy?: () -> bool
4444
end
4545
end
4646
end

0 commit comments

Comments
 (0)