Skip to content

Commit ab0f4c2

Browse files
committed
Refactor cookies, headers and set cookie headers
1 parent c7552aa commit ab0f4c2

File tree

11 files changed

+91
-169
lines changed

11 files changed

+91
-169
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,29 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class Cookies
24-
def initialize
25-
@cookies = {}
24+
def initialize(cookies = {})
25+
@cookies = cookies
2626
end
2727

2828
def all
2929
@cookies
3030
end
3131

32-
def add_cookie(name, value)
33-
@cookies[name] = value
34-
end
35-
36-
def remove_cookie(name)
37-
@cookies.delete(name)
38-
end
39-
4032
def []=(key, value)
41-
add_cookie(key, value)
33+
@cookies[key] = value
4234
end
4335

4436
def [](key)
4537
@cookies[key]
4638
end
4739

4840
def delete(key)
49-
remove_cookie(key)
41+
@cookies.delete(key)
5042
end
5143

5244
def serialize
45+
return [] unless @cookies
46+
5347
@cookies.map do |name, value|
5448
{
5549
name: name.to_s,
@@ -61,6 +55,8 @@ def serialize
6155
end
6256
end
6357
end
64-
end # BiDi
58+
end
59+
60+
# BiDi
6561
end # WebDriver
6662
end # Selenium

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,16 @@ def all
2929
@headers
3030
end
3131

32-
def add_header(name, value)
33-
@headers[name] = value
34-
end
35-
36-
def remove_header(name)
37-
@headers.delete(name)
38-
end
39-
4032
def []=(key, value)
41-
add_header(key, value)
33+
@headers[key] = value
4234
end
4335

4436
def [](key)
4537
@headers[key]
4638
end
4739

4840
def delete(key)
49-
remove_header(key)
41+
@headers.delete(key)
5042
end
5143

5244
def serialize

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def headers
6161
@headers ||= Headers.new
6262
end
6363

64-
def cookies
65-
@cookies ||= Cookies.new
64+
def cookies(cookies = {})
65+
@cookies ||= Cookies.new(cookies)
6666
end
6767
end
6868
end # BiDi

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def headers
5050
@headers ||= Headers.new
5151
end
5252

53-
def set_cookie_headers(set_cookie_headers = nil)
53+
def set_cookie_headers(set_cookie_headers = {})
5454
@set_cookie_headers ||= SetCookieHeaders.new(set_cookie_headers)
5555
end
5656
end

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

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,24 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class SetCookieHeaders
24-
def initialize(set_cookie_headers)
24+
def initialize(set_cookie_headers = {})
2525
@set_cookie_headers = set_cookie_headers
2626
end
2727

2828
def all
2929
@set_cookie_headers
3030
end
3131

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-
5732
def []=(key, value)
58-
add_set_cookie_header(key, value)
33+
@set_cookie_headers[key] = {value: value}
5934
end
6035

6136
def [](key)
6237
@set_cookie_headers[key]
6338
end
6439

6540
def delete(key)
66-
remove_set_cookie_header(key)
41+
@set_cookie_headers.delete(key)
6742
end
6843

6944
def serialize

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ module Selenium
44
class Cookies
55
@cookies: untyped
66

7-
def initialize: () -> void
7+
def initialize: (untyped cookies) -> void
88

99
def all: () -> untyped
1010

11-
def add_cookie: (untyped name, untyped value) -> untyped
12-
13-
def remove_cookie: (untyped name) -> untyped
14-
1511
def []=: (untyped key, untyped value) -> untyped
1612

1713
def []: (untyped key) -> untyped

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ module Selenium
88

99
def all: () -> untyped
1010

11-
def add_header: (untyped name, untyped value) -> untyped
12-
13-
def remove_header: (untyped name) -> untyped
14-
1511
def []=: (untyped key, untyped value) -> untyped
1612

1713
def []: (untyped key) -> untyped
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Selenium
2+
module WebDriver
3+
class BiDi
4+
class SetCookieHeaders
5+
@set_cookie_headers: untyped
6+
7+
def initialize: (untyped set_cookie_headers) -> void
8+
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)
20+
end
21+
end
22+
end
23+
end

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,46 @@ class BiDi
2727
let(:cookies) { described_class.new }
2828

2929
describe '#initialize' do
30-
it 'initializes an empty cookies hash' do
30+
it 'initializes with an empty hash by default' do
3131
expect(cookies.all).to eq({})
3232
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
3339
end
3440

3541
describe '#all' do
3642
it 'returns the underlying cookies hash' do
37-
cookies.add_cookie('session_id', 'abc123')
43+
cookies['session_id'] = 'abc123'
3844
expect(cookies.all).to eq({'session_id' => 'abc123'})
3945
end
4046
end
4147

4248
describe '#add_cookie' do
4349
it 'adds a cookie to the internal store' do
44-
cookies.add_cookie('foo', 'bar')
50+
cookies['foo'] = 'bar'
4551
expect(cookies['foo']).to eq('bar')
4652
end
4753

4854
it 'updates an existing cookie if the name already exists' do
49-
cookies.add_cookie('foo', 'bar')
50-
cookies.add_cookie('foo', 'baz')
55+
cookies['foo'] = 'bar'
56+
cookies['foo'] = 'baz'
5157
expect(cookies['foo']).to eq('baz')
5258
end
5359
end
5460

5561
describe '#remove_cookie' do
5662
it 'removes a cookie by name' do
57-
cookies.add_cookie('foo', 'bar')
58-
cookies.remove_cookie('foo')
63+
cookies['foo'] = 'bar'
64+
cookies.delete('foo')
5965
expect(cookies['foo']).to be_nil
6066
end
6167

6268
it 'does not raise an error if cookie does not exist' do
63-
expect { cookies.remove_cookie('non_existent') }.not_to raise_error
69+
expect { cookies.delete('non_existent') }.not_to raise_error
6470
end
6571
end
6672

@@ -93,7 +99,7 @@ class BiDi
9399
describe '#serialize' do
94100
it 'returns an array of cookie hashes in the minimal format' do
95101
cookies['key4'] = 'value4'
96-
cookies.add_cookie('session_id', 'xyz123')
102+
cookies['session_id'] = 'xyz123'
97103

98104
serialized = cookies.serialize
99105
expect(serialized).to be_an(Array)

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,33 @@ class BiDi
3434

3535
describe '#all' do
3636
it 'returns the underlying headers hash' do
37-
headers.add_header('Authorization', 'Bearer abc123')
37+
headers['Authorization'] = 'Bearer abc123'
3838
expect(headers.all).to eq({'Authorization' => 'Bearer abc123'})
3939
end
4040
end
4141

4242
describe '#add_header' do
4343
it 'adds a header to the internal store' do
44-
headers.add_header('Content-Type', 'application/json')
44+
headers['Content-Type'] = 'application/json'
4545
expect(headers['Content-Type']).to eq('application/json')
4646
end
4747

4848
it 'updates an existing header if the name already exists' do
49-
headers.add_header('Content-Type', 'text/html')
50-
headers.add_header('Content-Type', 'application/json')
49+
headers['Content-Type'] = 'text/html'
50+
headers['Content-Type'] = 'application/json'
5151
expect(headers['Content-Type']).to eq('application/json')
5252
end
5353
end
5454

5555
describe '#remove_header' do
5656
it 'removes a header by name' do
57-
headers.add_header('X-Custom-Header', 'foo')
58-
headers.remove_header('X-Custom-Header')
57+
headers['X-Custom-Header'] = 'foo'
58+
headers.delete('X-Custom-Header')
5959
expect(headers['X-Custom-Header']).to be_nil
6060
end
6161

6262
it 'does not raise an error if header does not exist' do
63-
expect { headers.remove_header('Non-Existent') }.not_to raise_error
63+
expect { headers.delete('Non-Existent') }.not_to raise_error
6464
end
6565
end
6666

@@ -96,7 +96,7 @@ class BiDi
9696
describe '#serialize' do
9797
it 'returns an array of header hashes in the correct format' do
9898
headers['Accept'] = 'application/json'
99-
headers.add_header('User-Agent', 'MyAgent/1.0')
99+
headers['User-Agent'] = 'MyAgent/1.0'
100100

101101
serialized = headers.serialize
102102
expect(serialized).to be_an(Array)

0 commit comments

Comments
 (0)