Skip to content

Commit c7552aa

Browse files
committed
Serialize request values
1 parent 51e8f84 commit c7552aa

File tree

16 files changed

+826
-104
lines changed

16 files changed

+826
-104
lines changed

rb/lib/selenium/webdriver/bidi/network/cookies.rb

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,45 @@
2020
module Selenium
2121
module WebDriver
2222
class BiDi
23-
module Cookies
23+
class Cookies
24+
def initialize
25+
@cookies = {}
26+
end
27+
28+
def all
29+
@cookies
30+
end
31+
2432
def add_cookie(name, value)
25-
cookies.push(
26-
name: name,
27-
value: {
28-
type: 'string',
29-
value: value
30-
}
31-
)
33+
@cookies[name] = value
3234
end
3335

3436
def remove_cookie(name)
35-
cookies.delete_if { |cookie| cookie[:name] == name }
36-
end
37-
38-
def set_cookie_header(**args)
39-
cookies.push(
40-
name: args[:name],
41-
value: {
42-
type: 'string',
43-
value: 'input'
44-
},
45-
domain: args[:domain],
46-
httpOnly: args[:http_only],
47-
expiry: args[:expiry],
48-
maxAge: args[:max_age],
49-
path: args[:path],
50-
sameSite: args[:same_site],
51-
secure: args[:secure]
52-
)
37+
@cookies.delete(name)
38+
end
39+
40+
def []=(key, value)
41+
add_cookie(key, value)
42+
end
43+
44+
def [](key)
45+
@cookies[key]
46+
end
47+
48+
def delete(key)
49+
remove_cookie(key)
50+
end
51+
52+
def serialize
53+
@cookies.map do |name, value|
54+
{
55+
name: name.to_s,
56+
value: {
57+
type: 'string',
58+
value: value.to_s
59+
}
60+
}
61+
end
5362
end
5463
end
5564
end # BiDi
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
class BiDi
23+
class Credentials
24+
attr_accessor :username, :password
25+
26+
def initialize(username: nil, password: nil)
27+
@username = username
28+
@password = password
29+
end
30+
31+
def serialize
32+
return nil unless username && password
33+
34+
{
35+
type: 'password',
36+
username: username,
37+
password: password
38+
}
39+
end
40+
end
41+
end # BiDi
42+
end # WebDriver
43+
end # Selenium

rb/lib/selenium/webdriver/bidi/network/headers.rb

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,45 @@
2020
module Selenium
2121
module WebDriver
2222
class BiDi
23-
module Headers
23+
class Headers
24+
def initialize
25+
@headers = {}
26+
end
27+
28+
def all
29+
@headers
30+
end
31+
2432
def add_header(name, value)
25-
headers.push(
26-
name: name,
27-
value: {
28-
type: 'string',
29-
value: value
30-
}
31-
)
33+
@headers[name] = value
3234
end
3335

3436
def remove_header(name)
35-
headers.delete_if { |header| header[:name] == name }
37+
@headers.delete(name)
38+
end
39+
40+
def []=(key, value)
41+
add_header(key, value)
42+
end
43+
44+
def [](key)
45+
@headers[key]
46+
end
47+
48+
def delete(key)
49+
remove_header(key)
50+
end
51+
52+
def serialize
53+
@headers.map do |name, val|
54+
{
55+
name: name.to_s,
56+
value: {
57+
type: 'string',
58+
value: val.to_s
59+
}
60+
}
61+
end
3662
end
3763
end
3864
end # BiDi

rb/lib/selenium/webdriver/bidi/network/intercepted_request.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,26 @@ module Selenium
2424
module WebDriver
2525
class BiDi
2626
class InterceptedRequest < InterceptedItem
27-
include Cookies
28-
include Headers
29-
30-
attr_accessor :cookies, :headers, :method, :url
27+
attr_accessor :method, :url
3128
attr_reader :body
3229

3330
def initialize(network, request)
3431
super
35-
@body = nil
36-
@cookies = []
37-
@headers = []
32+
# We now rely on the modules above to initialize @headers and @cookies as Hashes
3833
@method = nil
3934
@url = nil
35+
@body = nil
4036
end
4137

4238
def continue
43-
network.continue_request(id:, body:, cookies:, headers:, method:, url:)
39+
network.continue_request(
40+
id: id,
41+
body: body,
42+
cookies: cookies.serialize,
43+
headers: headers.serialize,
44+
method: method,
45+
url: url
46+
)
4447
end
4548

4649
def fail
@@ -53,6 +56,14 @@ def body=(value)
5356
value: value.to_json
5457
}
5558
end
59+
60+
def headers
61+
@headers ||= Headers.new
62+
end
63+
64+
def cookies
65+
@cookies ||= Cookies.new
66+
end
5667
end
5768
end # BiDi
5869
end # WebDriver

rb/lib/selenium/webdriver/bidi/network/intercepted_response.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,41 @@
1717
# specific language governing permissions and limitations
1818
# under the License.
1919

20-
require_relative 'cookies'
20+
require_relative 'credentials'
2121
require_relative 'headers'
22+
require_relative 'set_cookie_headers'
2223

2324
module Selenium
2425
module WebDriver
2526
class BiDi
2627
class InterceptedResponse < InterceptedItem
27-
include Cookies
28-
include Headers
29-
30-
attr_accessor :cookies, :headers, :reason
31-
attr_reader :credentials
28+
attr_accessor :reason
3229

3330
def initialize(network, request)
3431
super
35-
@cookies = []
36-
@headers = []
37-
@credentials = nil
3832
@reason = nil
3933
end
4034

4135
def continue
42-
network.continue_response(id:, cookies:, headers:, credentials:, reason:)
36+
network.continue_response(
37+
id: id,
38+
cookies: set_cookie_headers.serialize,
39+
headers: headers.serialize,
40+
credentials: credentials.serialize,
41+
reason: reason
42+
)
43+
end
44+
45+
def credentials(username: nil, password: nil)
46+
@credentials ||= Credentials.new(username: username, password: password)
47+
end
48+
49+
def headers
50+
@headers ||= Headers.new
4351
end
4452

45-
def add_credentials(username, password)
46-
@credentials = {
47-
type: 'password',
48-
username: username,
49-
password: password
50-
}
53+
def set_cookie_headers(set_cookie_headers = nil)
54+
@set_cookie_headers ||= SetCookieHeaders.new(set_cookie_headers)
5155
end
5256
end
5357
end # BiDi
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
class BiDi
23+
class SetCookieHeaders
24+
def initialize(set_cookie_headers)
25+
@set_cookie_headers = set_cookie_headers
26+
end
27+
28+
def all
29+
@set_cookie_headers
30+
end
31+
32+
def add_set_cookie_header(name, value)
33+
@set_cookie_headers[name] = {value: value}
34+
end
35+
36+
def remove_set_cookie_header(name)
37+
@set_cookie_headers.delete(name)
38+
end
39+
40+
def set_cookie_header(**args)
41+
set_cookie_header = args[:name]
42+
43+
set_cookie_header_data = {
44+
value: 'input',
45+
domain: args[:domain],
46+
httpOnly: args[:http_only],
47+
expiry: args[:expiry],
48+
maxAge: args[:max_age],
49+
path: args[:path],
50+
sameSite: args[:same_site],
51+
secure: args[:secure]
52+
}
53+
54+
@set_cookie_headers[set_cookie_header] = set_cookie_header_data
55+
end
56+
57+
def []=(key, value)
58+
add_set_cookie_header(key, value)
59+
end
60+
61+
def [](key)
62+
@set_cookie_headers[key]
63+
end
64+
65+
def delete(key)
66+
remove_set_cookie_header(key)
67+
end
68+
69+
def serialize
70+
return [] unless @set_cookie_headers
71+
72+
@set_cookie_headers.map do |name, data|
73+
data = {value: data} unless data.is_a?(Hash)
74+
75+
{
76+
name: name.to_s,
77+
value: {
78+
type: 'string',
79+
value: data[:value].to_s
80+
},
81+
domain: data[:domain],
82+
httpOnly: data[:httpOnly],
83+
expiry: data[:expiry],
84+
maxAge: data[:maxAge],
85+
path: data[:path],
86+
sameSite: data[:sameSite],
87+
secure: data[:secure]
88+
}.compact
89+
end
90+
end
91+
end
92+
end # BiDi
93+
end # WebDriver
94+
end # Selenium

0 commit comments

Comments
 (0)