Skip to content

Commit ea1f4f0

Browse files
authored
request cookie and header setters now take hashes
1 parent 247863b commit ea1f4f0

File tree

1 file changed

+254
-78
lines changed

1 file changed

+254
-78
lines changed

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

Lines changed: 254 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,105 +17,281 @@
1717
# specific language governing permissions and limitations
1818
# under the License.
1919

20-
require File.expand_path('../spec_helper', __dir__)
21-
# Adjust the require path as necessary for your project structure
22-
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network', __dir__)
20+
require_relative 'spec_helper'
2321

2422
module Selenium
2523
module WebDriver
26-
class BiDi
27-
describe Network do
28-
let(:mock_bidi) { instance_double(BiDi, 'Bidi') }
29-
let(:network) { described_class.new(mock_bidi) }
30-
let(:request_id) { '12345-request-id' }
24+
describe Network, exclude: {version: 'beta'},
25+
exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
26+
only: {browser: %i[chrome edge firefox]} do
27+
let(:username) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first }
28+
let(:password) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last }
3129

32-
before { allow(mock_bidi).to receive(:send_cmd).and_return({}) }
30+
it 'adds an auth handler' do
31+
reset_driver!(web_socket_url: true) do |driver|
32+
network = described_class.new(driver)
33+
network.add_authentication_handler(username, password)
34+
driver.navigate.to url_for('basicAuth')
35+
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
36+
expect(network.callbacks.count).to be 1
37+
end
38+
end
3339

34-
describe '#continue_request' do
35-
it 'sends only the mandatory request ID when all optional args are nil' do
36-
expected_payload = {request: request_id}
40+
it 'adds an auth handler with a filter' do
41+
reset_driver!(web_socket_url: true) do |driver|
42+
network = described_class.new(driver)
43+
network.add_authentication_handler(username, password, url_for('basicAuth'))
44+
driver.navigate.to url_for('basicAuth')
45+
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
46+
expect(network.callbacks.count).to be 1
47+
end
48+
end
3749

38-
expect(mock_bidi).to have_received(:send_cmd).with('network.continueRequest', expected_payload)
50+
it 'adds an auth handler with multiple filters' do
51+
reset_driver!(web_socket_url: true) do |driver|
52+
network = described_class.new(driver)
53+
network.add_authentication_handler(username, password, url_for('basicAuth'), url_for('formPage.html'))
54+
driver.navigate.to url_for('basicAuth')
55+
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
56+
expect(network.callbacks.count).to be 1
57+
end
58+
end
3959

40-
network.continue_request(id: request_id)
41-
end
60+
it 'adds an auth handler with a pattern type' do
61+
reset_driver!(web_socket_url: true) do |driver|
62+
network = described_class.new(driver)
63+
network.add_authentication_handler(username, password, url_for('basicAuth'), pattern_type: :url)
64+
driver.navigate.to url_for('basicAuth')
65+
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
66+
expect(network.callbacks.count).to be 1
67+
end
68+
end
4269

43-
it 'sends only provided optional args' do
44-
expected_payload = {
45-
request: request_id,
46-
body: {type: 'string', value: 'new body'},
47-
method: 'POST'
48-
}
49-
50-
expect(mock_bidi).to have_received(:send_cmd).with('network.continueRequest', expected_payload)
51-
52-
network.continue_request(
53-
id: request_id,
54-
body: {type: 'string', value: 'new body'},
55-
cookies: nil,
56-
headers: nil,
57-
method: 'POST'
58-
)
59-
end
70+
it 'removes an auth handler' do
71+
reset_driver!(web_socket_url: true) do |driver|
72+
network = described_class.new(driver)
73+
id = network.add_authentication_handler(username, password)
74+
network.remove_handler(id)
75+
expect(network.callbacks.count).to be 0
6076
end
77+
end
6178

62-
describe '#continue_response' do
63-
it 'sends only the mandatory request ID when all optional args are nil' do
64-
expected_payload = {request: request_id}
79+
it 'clears all auth handlers' do
80+
reset_driver!(web_socket_url: true) do |driver|
81+
network = described_class.new(driver)
82+
2.times { network.add_authentication_handler(username, password) }
83+
network.clear_handlers
84+
expect(network.callbacks.count).to be 0
85+
end
86+
end
6587

66-
expect(mock_bidi).to have_received(:send_cmd).with('network.continueResponse', expected_payload)
88+
it 'continues without auth' do
89+
reset_driver!(web_socket_url: true) do |driver|
90+
network = described_class.new(driver)
91+
network.add_authentication_handler(&:skip)
92+
expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)
93+
end
94+
end
6795

68-
network.continue_response(id: request_id)
69-
end
96+
it 'cancels auth' do
97+
reset_driver!(web_socket_url: true) do |driver|
98+
network = described_class.new(driver)
99+
network.add_authentication_handler(&:cancel)
100+
driver.navigate.to url_for('basicAuth')
101+
expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')
102+
end
103+
end
104+
105+
it 'adds a request handler' do
106+
reset_driver!(web_socket_url: true) do |driver|
107+
network = described_class.new(driver)
108+
network.add_request_handler(&:continue)
109+
driver.navigate.to url_for('formPage.html')
110+
expect(driver.current_url).to eq(url_for('formPage.html'))
111+
expect(network.callbacks.count).to be 1
112+
end
113+
end
114+
115+
it 'adds a request handler with a filter' do
116+
reset_driver!(web_socket_url: true) do |driver|
117+
network = described_class.new(driver)
118+
network.add_request_handler(url_for('formPage.html'), &:continue)
119+
driver.navigate.to url_for('formPage.html')
120+
expect(driver.current_url).to eq(url_for('formPage.html'))
121+
expect(network.callbacks.count).to be 1
122+
end
123+
end
124+
125+
it 'adds a request handler with multiple filters' do
126+
reset_driver!(web_socket_url: true) do |driver|
127+
network = described_class.new(driver)
128+
network.add_request_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)
129+
driver.navigate.to url_for('formPage.html')
130+
expect(driver.current_url).to eq(url_for('formPage.html'))
131+
expect(network.callbacks.count).to be 1
132+
end
133+
end
134+
135+
it 'adds a request handler with a pattern type' do
136+
reset_driver!(web_socket_url: true) do |driver|
137+
network = described_class.new(driver)
138+
network.add_request_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
139+
driver.navigate.to url_for('formPage.html')
140+
expect(driver.current_url).to eq(url_for('formPage.html'))
141+
expect(network.callbacks.count).to be 1
142+
end
143+
end
70144

71-
it 'sends only provided optional args' do
72-
expected_headers = [{name: 'Auth', value: {type: 'string', value: 'Token'}}]
73-
expected_payload = {
74-
request: request_id,
75-
headers: expected_headers,
76-
statusCode: 202
77-
}
78-
79-
expect(mock_bidi).to have_received(:send_cmd).with('network.continueResponse', expected_payload)
80-
81-
network.continue_response(
82-
id: request_id,
83-
cookies: nil,
84-
credentials: nil,
85-
headers: expected_headers,
86-
reason: nil,
87-
status: 202
88-
)
145+
it 'adds a request handler with attributes' do
146+
reset_driver!(web_socket_url: true) do |driver|
147+
network = described_class.new(driver)
148+
network.add_request_handler do |request|
149+
request.method = 'GET'
150+
request.url = url_for('formPage.html')
151+
request.headers = {foo: 'bar', baz: 'qux'}
152+
request.cookies = {name: 'test',
153+
value: 'value4',
154+
domain: 'example.com',
155+
path: '/path',
156+
size: 1234,
157+
httpOnly: true,
158+
secure: true,
159+
sameSite: 'Strict',
160+
expiry: 1234}
161+
request.body = ({test: 'example'})
162+
request.continue
89163
end
164+
driver.navigate.to url_for('formPage.html')
165+
expect(driver.current_url).to eq(url_for('formPage.html'))
166+
expect(network.callbacks.count).to be 1
167+
end
168+
end
169+
170+
it 'fails a request' do
171+
reset_driver!(web_socket_url: true) do |driver|
172+
network = described_class.new(driver)
173+
network.add_request_handler(&:fail)
174+
expect(network.callbacks.count).to be 1
175+
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)
90176
end
177+
end
91178

92-
describe '#provide_response' do
93-
it 'sends only the mandatory request ID when all optional args are nil' do
94-
expected_payload = {request: request_id}
179+
it 'removes a request handler' do
180+
reset_driver!(web_socket_url: true) do |driver|
181+
network = described_class.new(driver)
182+
id = network.add_request_handler(&:continue)
183+
network.remove_handler(id)
184+
expect(network.callbacks.count).to be 0
185+
end
186+
end
95187

96-
expect(mock_bidi).to have_received(:send_cmd).with('network.provideResponse', expected_payload)
188+
it 'clears all request handlers' do
189+
reset_driver!(web_socket_url: true) do |driver|
190+
network = described_class.new(driver)
191+
2.times { network.add_request_handler(&:continue) }
192+
network.clear_handlers
193+
expect(network.callbacks.count).to be 0
194+
end
195+
end
196+
197+
it 'adds a response handler' do
198+
reset_driver!(web_socket_url: true) do |driver|
199+
network = described_class.new(driver)
200+
network.add_response_handler(&:continue)
201+
driver.navigate.to url_for('formPage.html')
202+
expect(driver.current_url).to eq(url_for('formPage.html'))
203+
expect(network.callbacks.count).to be 1
204+
end
205+
end
206+
207+
it 'adds a response handler with a filter' do
208+
reset_driver!(web_socket_url: true) do |driver|
209+
network = described_class.new(driver)
210+
network.add_response_handler(url_for('formPage.html'), &:continue)
211+
driver.navigate.to url_for('formPage.html')
212+
expect(driver.find_element(name: 'login')).to be_displayed
213+
expect(network.callbacks.count).to be 1
214+
end
215+
end
97216

98-
network.provide_response(id: request_id)
217+
it 'adds a response handler with multiple filters' do
218+
reset_driver!(web_socket_url: true) do |driver|
219+
network = described_class.new(driver)
220+
network.add_response_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)
221+
driver.navigate.to url_for('formPage.html')
222+
expect(driver.find_element(name: 'login')).to be_displayed
223+
expect(network.callbacks.count).to be 1
224+
end
225+
end
226+
227+
it 'adds a response handler with a pattern type' do
228+
reset_driver!(web_socket_url: true) do |driver|
229+
network = described_class.new(driver)
230+
network.add_response_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
231+
driver.navigate.to url_for('formPage.html')
232+
expect(driver.current_url).to eq(url_for('formPage.html'))
233+
expect(network.callbacks.count).to be 1
234+
end
235+
end
236+
237+
it 'adds a response handler with attributes' do
238+
reset_driver!(web_socket_url: true) do |driver|
239+
network = described_class.new(driver)
240+
network.add_response_handler do |response|
241+
response.reason = 'OK'
242+
response.headers = {foo: 'bar'}
243+
response.credentials.username = 'foo'
244+
response.credentials.password = 'bar'
245+
response.cookies = {name: 'foo',
246+
domain: 'localhost',
247+
httpOnly: true,
248+
expiry: '1_000_000',
249+
maxAge: 1_000,
250+
path: '/',
251+
sameSite: 'none',
252+
secure: false}
253+
response.continue
99254
end
255+
driver.navigate.to url_for('formPage.html')
256+
expect(driver.current_url).to eq(url_for('formPage.html'))
257+
expect(network.callbacks.count).to be 1
258+
end
259+
end
100260

101-
it 'sends only provided optional args' do
102-
expected_payload = {
103-
request: request_id,
104-
body: {type: 'string', value: 'Hello'},
105-
reasonPhrase: 'OK-Custom'
106-
}
107-
108-
expect(mock_bidi).to have_received(:send_cmd).with('network.provideResponse', expected_payload)
109-
110-
network.provide_response(
111-
id: request_id,
112-
body: {type: 'string', value: 'Hello'},
113-
cookies: nil,
114-
headers: nil,
115-
reason: 'OK-Custom',
116-
status: nil
117-
)
261+
it 'adds a response handler that provides a response',
262+
except: {browser: :firefox,
263+
reason: 'https://github.com/w3c/webdriver-bidi/issues/747'} do
264+
reset_driver!(web_socket_url: true) do |driver|
265+
network = described_class.new(driver)
266+
network.add_response_handler do |response|
267+
response.status = 200
268+
response.headers = {foo: 'bar'}
269+
response.body = '<html><head><title>Hello World!</title></head><body/></html>'
270+
response.provide_response
118271
end
272+
driver.navigate.to url_for('formPage.html')
273+
source = driver.page_source
274+
expect(source).not_to include('There should be a form here:')
275+
expect(source).to include('Hello World!')
276+
end
277+
end
278+
279+
it 'removes a response handler' do
280+
reset_driver!(web_socket_url: true) do |driver|
281+
network = described_class.new(driver)
282+
id = network.add_response_handler(&:continue)
283+
network.remove_handler(id)
284+
network.clear_handlers
285+
expect(network.callbacks.count).to be 0
286+
end
287+
end
288+
289+
it 'clears all response handlers' do
290+
reset_driver!(web_socket_url: true) do |driver|
291+
network = described_class.new(driver)
292+
2.times { network.add_response_handler(&:continue) }
293+
network.clear_handlers
294+
expect(network.callbacks.count).to be 0
119295
end
120296
end
121297
end

0 commit comments

Comments
 (0)