Skip to content

Commit 72445fc

Browse files
committed
Update intercept request and response and improve headers and cookies
1 parent 92be65e commit 72445fc

File tree

7 files changed

+336
-29
lines changed

7 files changed

+336
-29
lines changed

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: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ module WebDriver
2525
class BiDi
2626
class InterceptedRequest < InterceptedItem
2727
attr_accessor :method, :url
28-
attr_reader :body
28+
attr_reader :body, :headers, :cookies
2929

3030
def initialize(network, request)
3131
super
3232
@method = nil
3333
@url = nil
3434
@body = nil
35+
@headers = nil
36+
@cookies = nil
3537
end
3638

3739
def continue
3840
network.continue_request(
3941
id: id,
4042
body: body,
41-
cookies: cookies.as_json,
42-
headers: headers.as_json,
43+
cookies: cookies&.as_json,
44+
headers: headers&.as_json,
4345
method: method,
4446
url: url
4547
)
@@ -56,12 +58,12 @@ def body=(value)
5658
}
5759
end
5860

59-
def headers
60-
@headers ||= Headers.new
61+
def headers=(headers = {})
62+
@headers = Headers.new(headers)
6163
end
6264

63-
def cookies(cookies = {})
64-
@cookies ||= Cookies.new(cookies)
65+
def cookies=(cookies = {})
66+
@cookies = Cookies.new(cookies)
6567
end
6668
end
6769
end # BiDi

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,22 @@ module WebDriver
2626
class BiDi
2727
class InterceptedResponse < InterceptedItem
2828
attr_accessor :reason, :status
29-
attr_reader :body
29+
attr_reader :body, :headers, :cookies
3030

3131
def initialize(network, request)
3232
super
3333
@reason = nil
3434
@status = nil
3535
@body = nil
36+
@headers = nil
37+
@cookies = nil
3638
end
3739

3840
def continue
3941
network.continue_response(
4042
id: id,
41-
cookies: cookies.as_json,
42-
headers: headers.as_json,
43+
cookies: cookies&.as_json,
44+
headers: headers&.as_json,
4345
credentials: credentials.as_json,
4446
reason: reason,
4547
status: status
@@ -49,8 +51,8 @@ def continue
4951
def provide_response
5052
network.provide_response(
5153
id: id,
52-
cookies: cookies.as_json,
53-
headers: headers.as_json,
54+
cookies: cookies&.as_json,
55+
headers: headers&.as_json,
5456
body: body,
5557
reason: reason,
5658
status: status
@@ -61,11 +63,11 @@ def credentials(username: nil, password: nil)
6163
@credentials ||= Credentials.new(username: username, password: password)
6264
end
6365

64-
def headers
65-
@headers ||= Headers.new
66+
def headers=(*headers)
67+
@headers = Headers.new(headers)
6668
end
6769

68-
def cookies(cookies = {})
70+
def cookies=(cookies = {})
6971
@cookies ||= Cookies.new(cookies)
7072
end
7173

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)