Skip to content

Commit fa0c87a

Browse files
committed
Simplify serialization
1 parent ab0f4c2 commit fa0c87a

File tree

9 files changed

+56
-358
lines changed

9 files changed

+56
-358
lines changed

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

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,14 @@
2020
module Selenium
2121
module WebDriver
2222
class BiDi
23-
class Cookies
23+
class Cookies < Hash
2424
def initialize(cookies = {})
25-
@cookies = cookies
26-
end
27-
28-
def all
29-
@cookies
30-
end
31-
32-
def []=(key, value)
33-
@cookies[key] = value
34-
end
35-
36-
def [](key)
37-
@cookies[key]
38-
end
39-
40-
def delete(key)
41-
@cookies.delete(key)
25+
super()
26+
merge!(cookies)
4227
end
4328

4429
def serialize
45-
return [] unless @cookies
46-
47-
@cookies.map do |name, value|
30+
map do |name, value|
4831
{
4932
name: name.to_s,
5033
value: {
@@ -55,8 +38,6 @@ def serialize
5538
end
5639
end
5740
end
58-
end
59-
60-
# BiDi
41+
end # BiDi
6142
end # WebDriver
6243
end # Selenium

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,9 @@
2020
module Selenium
2121
module WebDriver
2222
class BiDi
23-
class Headers
24-
def initialize
25-
@headers = {}
26-
end
27-
28-
def all
29-
@headers
30-
end
31-
32-
def []=(key, value)
33-
@headers[key] = value
34-
end
35-
36-
def [](key)
37-
@headers[key]
38-
end
39-
40-
def delete(key)
41-
@headers.delete(key)
42-
end
43-
23+
class Headers < Hash
4424
def serialize
45-
@headers.map do |name, val|
25+
map do |name, val|
4626
{
4727
name: name.to_s,
4828
value: {

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,14 @@
2020
module Selenium
2121
module WebDriver
2222
class BiDi
23-
class SetCookieHeaders
23+
class SetCookieHeaders < Hash
2424
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 []=(key, value)
33-
@set_cookie_headers[key] = {value: value}
34-
end
35-
36-
def [](key)
37-
@set_cookie_headers[key]
38-
end
39-
40-
def delete(key)
41-
@set_cookie_headers.delete(key)
25+
super()
26+
merge!(set_cookie_headers)
4227
end
4328

4429
def serialize
45-
return [] unless @set_cookie_headers
46-
47-
@set_cookie_headers.map do |name, data|
30+
map do |name, data|
4831
data = {value: data} unless data.is_a?(Hash)
4932

5033
{

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,8 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class Cookies
5-
@cookies: untyped
6-
75
def initialize: (untyped cookies) -> void
86

9-
def all: () -> untyped
10-
11-
def []=: (untyped key, untyped value) -> untyped
12-
13-
def []: (untyped key) -> untyped
14-
15-
def delete: (untyped key) -> untyped
16-
177
def serialize: () -> untyped
188
end
199
end

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,8 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class Headers
5-
@headers: untyped
6-
75
def initialize: () -> void
86

9-
def all: () -> untyped
10-
11-
def []=: (untyped key, untyped value) -> untyped
12-
13-
def []: (untyped key) -> untyped
14-
15-
def delete: (untyped key) -> untyped
16-
177
def serialize: () -> untyped
188
end
199
end

rb/sig/lib/selenium/webdriver/bidi/network/set_cookie_headers.rbs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,9 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class SetCookieHeaders
5-
@set_cookie_headers: untyped
6-
75
def initialize: (untyped set_cookie_headers) -> void
86

9-
def all: () -> untyped
10-
11-
def set_cookie_header: (**untyped args) -> untyped
12-
13-
def []=: (untyped key, untyped value) -> untyped
14-
15-
def []: (untyped key) -> untyped
16-
17-
def delete: (untyped key) -> untyped
18-
19-
def serialize: () -> (::Array[untyped] | untyped)
7+
def serialize: () -> (Array[untyped] | untyped)
208
end
219
end
2210
end

rb/spec/unit/selenium/webdriver/bidi/cookies_spec.rb

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -26,98 +26,22 @@ class BiDi
2626
describe Cookies do
2727
let(:cookies) { described_class.new }
2828

29-
describe '#initialize' do
30-
it 'initializes with an empty hash by default' do
31-
expect(cookies.all).to eq({})
32-
end
33-
34-
it 'stores the passed cookie hash internally' do
35-
my_hash = {'foo' => {value: 'bar'}}
36-
cookie_headers = described_class.new(my_hash)
37-
expect(cookie_headers.all).to eq(my_hash)
38-
end
39-
end
40-
41-
describe '#all' do
42-
it 'returns the underlying cookies hash' do
43-
cookies['session_id'] = 'abc123'
44-
expect(cookies.all).to eq({'session_id' => 'abc123'})
45-
end
46-
end
47-
48-
describe '#add_cookie' do
49-
it 'adds a cookie to the internal store' do
50-
cookies['foo'] = 'bar'
51-
expect(cookies['foo']).to eq('bar')
52-
end
53-
54-
it 'updates an existing cookie if the name already exists' do
55-
cookies['foo'] = 'bar'
56-
cookies['foo'] = 'baz'
57-
expect(cookies['foo']).to eq('baz')
58-
end
59-
end
60-
61-
describe '#remove_cookie' do
62-
it 'removes a cookie by name' do
63-
cookies['foo'] = 'bar'
64-
cookies.delete('foo')
65-
expect(cookies['foo']).to be_nil
66-
end
67-
68-
it 'does not raise an error if cookie does not exist' do
69-
expect { cookies.delete('non_existent') }.not_to raise_error
70-
end
71-
end
72-
73-
describe '#[]=' do
74-
it 'adds a cookie using bracket assignment' do
75-
cookies['key1'] = 'value1'
76-
expect(cookies['key1']).to eq('value1')
77-
end
78-
end
79-
80-
describe '#[]' do
81-
it 'retrieves the value of a cookie by name' do
82-
cookies['key2'] = 'value2'
83-
expect(cookies['key2']).to eq('value2')
84-
end
85-
86-
it 'returns nil for unknown cookies' do
87-
expect(cookies['does_not_exist']).to be_nil
88-
end
89-
end
90-
91-
describe '#delete' do
92-
it 'removes a cookie via bracket-based delete' do
93-
cookies['key3'] = 'value3'
94-
cookies.delete('key3')
95-
expect(cookies['key3']).to be_nil
96-
end
97-
end
98-
99-
describe '#serialize' do
100-
it 'returns an array of cookie hashes in the minimal format' do
101-
cookies['key4'] = 'value4'
102-
cookies['session_id'] = 'xyz123'
103-
104-
serialized = cookies.serialize
105-
expect(serialized).to be_an(Array)
106-
expect(serialized.size).to eq(2)
107-
108-
key4_item = serialized.find { |h| h[:name] == 'key4' }
109-
expect(key4_item).not_to be_nil
110-
expect(key4_item[:value][:type]).to eq('string')
111-
expect(key4_item[:value][:value]).to eq('value4')
112-
113-
session_item = serialized.find { |h| h[:name] == 'session_id' }
114-
expect(session_item).not_to be_nil
115-
expect(session_item[:value][:value]).to eq('xyz123')
116-
end
117-
118-
it 'returns an empty array if no cookies are set' do
119-
expect(cookies.serialize).to eq([])
120-
end
29+
it 'returns a serialized array of cookie hashes' do
30+
cookies['key4'] = 'value4'
31+
cookies['session_id'] = 'xyz123'
32+
33+
serialized = cookies.serialize
34+
expect(serialized).to be_an(Array)
35+
expect(serialized.size).to eq(2)
36+
37+
key4_item = serialized.find { |h| h[:name] == 'key4' }
38+
expect(key4_item).not_to be_nil
39+
expect(key4_item[:value][:type]).to eq('string')
40+
expect(key4_item[:value][:value]).to eq('value4')
41+
42+
session_item = serialized.find { |h| h[:name] == 'session_id' }
43+
expect(session_item).not_to be_nil
44+
expect(session_item[:value][:value]).to eq('xyz123')
12145
end
12246
end
12347
end

rb/spec/unit/selenium/webdriver/bidi/headers_spec.rb

Lines changed: 12 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -26,94 +26,21 @@ class BiDi
2626
describe Headers do
2727
let(:headers) { described_class.new }
2828

29-
describe '#initialize' do
30-
it 'initializes an empty headers hash' do
31-
expect(headers.all).to eq({})
32-
end
33-
end
34-
35-
describe '#all' do
36-
it 'returns the underlying headers hash' do
37-
headers['Authorization'] = 'Bearer abc123'
38-
expect(headers.all).to eq({'Authorization' => 'Bearer abc123'})
39-
end
40-
end
41-
42-
describe '#add_header' do
43-
it 'adds a header to the internal store' do
44-
headers['Content-Type'] = 'application/json'
45-
expect(headers['Content-Type']).to eq('application/json')
46-
end
47-
48-
it 'updates an existing header if the name already exists' do
49-
headers['Content-Type'] = 'text/html'
50-
headers['Content-Type'] = 'application/json'
51-
expect(headers['Content-Type']).to eq('application/json')
52-
end
53-
end
54-
55-
describe '#remove_header' do
56-
it 'removes a header by name' do
57-
headers['X-Custom-Header'] = 'foo'
58-
headers.delete('X-Custom-Header')
59-
expect(headers['X-Custom-Header']).to be_nil
60-
end
61-
62-
it 'does not raise an error if header does not exist' do
63-
expect { headers.delete('Non-Existent') }.not_to raise_error
64-
end
65-
end
66-
67-
describe '#[]=' do
68-
it 'adds or updates a header using bracket assignment' do
69-
headers['Cache-Control'] = 'no-cache'
70-
expect(headers['Cache-Control']).to eq('no-cache')
71-
72-
headers['Cache-Control'] = 'private'
73-
expect(headers['Cache-Control']).to eq('private')
74-
end
75-
end
76-
77-
describe '#[]' do
78-
it 'retrieves the value of a header by name' do
79-
headers['Host'] = 'example.com'
80-
expect(headers['Host']).to eq('example.com')
81-
end
82-
83-
it 'returns nil for unknown headers' do
84-
expect(headers['Does-Not-Exist']).to be_nil
85-
end
86-
end
87-
88-
describe '#delete' do
89-
it 'removes a header via bracket-based delete' do
90-
headers['Accept'] = 'text/html'
91-
headers.delete('Accept')
92-
expect(headers['Accept']).to be_nil
93-
end
94-
end
95-
96-
describe '#serialize' do
97-
it 'returns an array of header hashes in the correct format' do
98-
headers['Accept'] = 'application/json'
99-
headers['User-Agent'] = 'MyAgent/1.0'
100-
101-
serialized = headers.serialize
102-
expect(serialized).to be_an(Array)
103-
expect(serialized.size).to eq(2)
29+
it 'returns an array of serialized array of header hashes' do
30+
headers['Accept'] = 'application/json'
31+
headers['User-Agent'] = 'MyAgent/1.0'
10432

105-
accept_item = serialized.find { |h| h[:name] == 'Accept' }
106-
expect(accept_item).not_to be_nil
107-
expect(accept_item[:value]).to eq({type: 'string', value: 'application/json'})
33+
serialized = headers.serialize
34+
expect(serialized).to be_an(Array)
35+
expect(serialized.size).to eq(2)
10836

109-
ua_item = serialized.find { |h| h[:name] == 'User-Agent' }
110-
expect(ua_item).not_to be_nil
111-
expect(ua_item[:value]).to eq({type: 'string', value: 'MyAgent/1.0'})
112-
end
37+
accept_item = serialized.find { |h| h[:name] == 'Accept' }
38+
expect(accept_item).not_to be_nil
39+
expect(accept_item[:value]).to eq({ type: 'string', value: 'application/json' })
11340

114-
it 'returns an empty array if no headers are set' do
115-
expect(headers.serialize).to eq([])
116-
end
41+
ua_item = serialized.find { |h| h[:name] == 'User-Agent' }
42+
expect(ua_item).not_to be_nil
43+
expect(ua_item[:value]).to eq({ type: 'string', value: 'MyAgent/1.0' })
11744
end
11845
end
11946
end

0 commit comments

Comments
 (0)