Skip to content

Commit 76c303b

Browse files
aguspetitusfortner
andauthored
[rb] Fix Network issue by removing nil values on network requests (#16442)
* Remove nil values for network requests * Update intercept request and response and improve headers and cookies * request cookie and header setters now take hashes * put the unit tests back * All tests fixed --------- Co-authored-by: titusfortner <[email protected]>
1 parent 912f35e commit 76c303b

File tree

11 files changed

+490
-35
lines changed

11 files changed

+490
-35
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,16 @@ def cancel_auth(request_id)
8282
end
8383

8484
def continue_request(**args)
85-
@bidi.send_cmd(
86-
'network.continueRequest',
85+
args = {
8786
request: args[:id],
8887
body: args[:body],
8988
cookies: args[:cookies],
9089
headers: args[:headers],
9190
method: args[:method],
9291
url: args[:url]
93-
)
92+
}.compact
93+
94+
@bidi.send_cmd('network.continueRequest', **args)
9495
end
9596

9697
def fail_request(request_id)
@@ -101,27 +102,29 @@ def fail_request(request_id)
101102
end
102103

103104
def continue_response(**args)
104-
@bidi.send_cmd(
105-
'network.continueResponse',
105+
args = {
106106
request: args[:id],
107107
cookies: args[:cookies],
108108
credentials: args[:credentials],
109109
headers: args[:headers],
110110
reasonPhrase: args[:reason],
111111
statusCode: args[:status]
112-
)
112+
}.compact
113+
114+
@bidi.send_cmd('network.continueResponse', **args)
113115
end
114116

115117
def provide_response(**args)
116-
@bidi.send_cmd(
117-
'network.provideResponse',
118+
args = {
118119
request: args[:id],
119120
body: args[:body],
120121
cookies: args[:cookies],
121122
headers: args[:headers],
122123
reasonPhrase: args[:reason],
123124
statusCode: args[:status]
124-
)
125+
}.compact
126+
127+
@bidi.send_cmd('network.provideResponse', **args)
125128
end
126129

127130
def set_cache_behavior(behavior, *contexts)

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class Cookies < Hash
24-
def initialize(cookies = {})
25-
super()
26-
merge!(cookies)
27-
end
28-
2924
def as_json
30-
self[:name] = self[:name].to_s
31-
self[:value] = {type: 'string', value: self[:value].to_s}
25+
map do |name, val|
26+
self[:name] = name.to_s
27+
self[:value] = {type: 'string', value: val.to_s}
3228

33-
[compact]
29+
[compact]
30+
end
3431
end
3532
end
3633
end # BiDi

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ def initialize(network, request)
3232
@method = nil
3333
@url = nil
3434
@body = nil
35+
@headers = nil
36+
@cookies = nil
3537
end
3638

3739
def continue
40+
cookies = @cookies&.as_json
41+
headers = @headers&.as_json
3842
network.continue_request(
3943
id: id,
4044
body: body,
41-
cookies: cookies.as_json,
42-
headers: headers.as_json,
45+
cookies: cookies,
46+
headers: headers,
4347
method: method,
4448
url: url
4549
)
@@ -56,13 +60,21 @@ def body=(value)
5660
}
5761
end
5862

59-
def headers
60-
@headers ||= Headers.new
63+
def headers=(headers = {})
64+
@headers = Headers.new(headers)
65+
end
66+
67+
def headers(headers = {})
68+
@headers ||= Headers.new(headers)
6169
end
6270

6371
def cookies(cookies = {})
6472
@cookies ||= Cookies.new(cookies)
6573
end
74+
75+
def cookies=(cookies = {})
76+
@cookies = Cookies.new(cookies)
77+
end
6678
end
6779
end # BiDi
6880
end # WebDriver

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,30 @@ def initialize(network, request)
3333
@reason = nil
3434
@status = nil
3535
@body = nil
36+
@headers = nil
37+
@cookies = nil
3638
end
3739

3840
def continue
41+
cookies = @cookies&.as_json
42+
headers = @headers&.as_json
3943
network.continue_response(
4044
id: id,
41-
cookies: cookies.as_json,
42-
headers: headers.as_json,
45+
cookies: cookies,
46+
headers: headers,
4347
credentials: credentials.as_json,
4448
reason: reason,
4549
status: status
4650
)
4751
end
4852

4953
def provide_response
54+
cookies = @cookies&.as_json
55+
headers = @headers&.as_json
5056
network.provide_response(
5157
id: id,
52-
cookies: cookies.as_json,
53-
headers: headers.as_json,
58+
cookies: cookies,
59+
headers: headers,
5460
body: body,
5561
reason: reason,
5662
status: status
@@ -61,14 +67,22 @@ def credentials(username: nil, password: nil)
6167
@credentials ||= Credentials.new(username: username, password: password)
6268
end
6369

64-
def headers
65-
@headers ||= Headers.new
70+
def headers(headers = {})
71+
@headers ||= Headers.new(headers)
72+
end
73+
74+
def headers=(*headers)
75+
@headers = Headers.new(headers)
6676
end
6777

6878
def cookies(cookies = {})
6979
@cookies ||= Cookies.new(cookies)
7080
end
7181

82+
def cookies=(cookies = {})
83+
@cookies ||= Cookies.new(cookies)
84+
end
85+
7286
def body=(value)
7387
@body = {
7488
type: 'string',

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

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

2525
def remove_intercept: (String intercept) -> Hash[nil, nil]
2626

27-
def continue_with_auth: (String request_id, String username, String password) -> Hash[nil, nil]
27+
def continue_with_auth: (Integer request_id, String username, String password) -> Hash[nil, nil]
2828

2929
def provide_response: -> Hash[nil, nil]
3030

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class Cookies
5-
def initialize: (Hash[Symbol, String | Integer | bool] cookies) -> void
5+
def initialize: (Hash[Symbol, String | Integer | bool]? cookies) -> void
66

77
def as_json: () -> Array[Hash[Symbol, untyped]]
88
end

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class InterceptedRequest < InterceptedItem
5-
@method: String
5+
@method: String?
66

7-
@url: String
7+
@url: String?
88

9-
@body: Hash[untyped, untyped]
9+
@body: Hash[untyped, untyped]?
1010

11-
@headers: Hash[untyped, untyped]
11+
@headers: Hash[untyped, untyped]?
1212

13-
@cookies: Hash[untyped, untyped]
13+
@cookies: Hash[untyped, untyped]?
1414

1515
attr_accessor method: String
1616

@@ -22,13 +22,17 @@ module Selenium
2222

2323
def continue: () -> Hash[nil, nil]
2424

25+
def cookies=: -> Hash[nil, nil]
26+
2527
def fail: () -> Hash[nil, nil]
2628

2729
def body=: (Hash[untyped, untyped] value) -> Hash[untyped, untyped]
2830

2931
def headers: () -> Hash[untyped, untyped]
3032

3133
def cookies: () -> Hash[untyped, untyped]
34+
35+
def headers=: -> Hash[untyped, untyped]
3236
end
3337
end
3438
end

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Selenium
55
@body: untyped
66
@cookies: Hash[Symbol, String | Integer]?
77

8-
@reason: String
8+
@reason: String?
99

1010
@credentials: untyped
1111

@@ -24,10 +24,14 @@ module Selenium
2424

2525
def cookies:(?Hash[Symbol, String | Integer]? set_cookie_headers) -> untyped
2626

27+
def cookies=: -> Hash[Symbol, String | Integer]
28+
2729
def credentials: (?username: untyped?, ?password: untyped?) -> untyped
2830

2931
def headers: () -> untyped
3032

33+
def headers=: -> Hash[untyped, untyped]
34+
3135
def provide_response: -> untyped
3236

3337
def set_cookie_headers: (?untyped? set_cookie_headers) -> untyped
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
require File.expand_path('../spec_helper', __dir__)
21+
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/cookies', __dir__)
22+
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/headers', __dir__)
23+
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/intercepted_request', __dir__)
24+
25+
module Selenium
26+
module WebDriver
27+
class BiDi
28+
describe InterceptedRequest do
29+
let(:mock_network) { instance_double(Network) }
30+
let(:mock_request) { {'request' => 'req-123'} }
31+
let(:request_id) { 'req-123' }
32+
let(:intercepted_request) { described_class.new(mock_network, mock_request) }
33+
let(:mock_headers) { [{name: 'Auth', value: 'token'}] }
34+
let(:mock_cookies) { [{name: 'session', value: {type: 'string', value: '123'}}] }
35+
36+
describe '#continue' do
37+
before do
38+
allow(mock_network).to receive(:continue_request)
39+
end
40+
41+
it 'sends only the mandatory ID when no optional fields are set' do
42+
expected_payload = {id: request_id, body: nil, cookies: nil, headers: nil, method: nil, url: nil}
43+
intercepted_request.continue
44+
45+
expect(mock_network).to have_received(:continue_request).with(expected_payload)
46+
end
47+
48+
it 'sends headers payload when headers are explicitly set' do
49+
intercepted_request.headers = mock_headers
50+
51+
expected_payload = {
52+
id: request_id,
53+
body: nil,
54+
cookies: nil,
55+
headers: Headers.new(mock_headers).as_json,
56+
method: nil,
57+
url: nil
58+
}
59+
60+
intercepted_request.continue
61+
62+
expect(mock_network).to have_received(:continue_request).with(expected_payload)
63+
end
64+
65+
it 'sends cookies payload when cookies are explicitly set' do
66+
intercepted_request.cookies = mock_cookies
67+
68+
expected_payload = {
69+
id: request_id,
70+
body: nil,
71+
cookies: Cookies.new(mock_cookies).as_json,
72+
headers: nil,
73+
method: nil,
74+
url: nil
75+
}
76+
77+
intercepted_request.continue
78+
79+
expect(mock_network).to have_received(:continue_request).with(expected_payload)
80+
end
81+
82+
it 'sends full custom payload when all fields are set' do
83+
intercepted_request.headers = mock_headers
84+
intercepted_request.cookies = mock_cookies
85+
intercepted_request.method = 'POST'
86+
87+
expected_payload = {
88+
id: request_id,
89+
body: nil,
90+
cookies: Cookies.new(mock_cookies).as_json,
91+
headers: Headers.new(mock_headers).as_json,
92+
method: 'POST',
93+
url: nil
94+
}
95+
96+
intercepted_request.continue
97+
98+
expect(mock_network).to have_received(:continue_request).with(expected_payload)
99+
end
100+
end
101+
end
102+
end
103+
end
104+
end

0 commit comments

Comments
 (0)