diff --git a/rb/lib/selenium/webdriver/bidi/network.rb b/rb/lib/selenium/webdriver/bidi/network.rb index 0042ac8578d1d..50c5d5c9be54c 100644 --- a/rb/lib/selenium/webdriver/bidi/network.rb +++ b/rb/lib/selenium/webdriver/bidi/network.rb @@ -40,12 +40,11 @@ def initialize(bidi) @bidi = bidi end - def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :string) - url_patterns = url_patterns && pattern_type ? UrlPattern.format_pattern(url_patterns, pattern_type) : nil + def add_intercept(phases: [], contexts: nil, filters: nil) @bidi.send_cmd('network.addIntercept', phases: phases, contexts: contexts, - urlPatterns: url_patterns) + urlPatterns: UrlPattern.format_pattern(filters)) end def remove_intercept(intercept) diff --git a/rb/lib/selenium/webdriver/bidi/network/url_pattern.rb b/rb/lib/selenium/webdriver/bidi/network/url_pattern.rb index 659b4be4a82b5..878a576812051 100644 --- a/rb/lib/selenium/webdriver/bidi/network/url_pattern.rb +++ b/rb/lib/selenium/webdriver/bidi/network/url_pattern.rb @@ -25,39 +25,39 @@ class BiDi module UrlPattern module_function - def format_pattern(url_patterns, pattern_type) - case pattern_type - when :string - to_url_string_pattern(url_patterns) - when :url - to_url_pattern(url_patterns) - else - raise ArgumentError, "Unknown pattern type: #{pattern_type}" - end - end + ALLOWED_KEYS = %i[protocol hostname port pathname search].freeze + WEB = [{ type: 'pattern', protocol: 'https' }.freeze, { type: 'pattern', protocol: 'http' }.freeze].freeze - def to_url_pattern(*url_patterns) - url_patterns.flatten.map do |url_pattern| - uri = URI.parse(url_pattern) + def format_pattern(filters) + patterns = Array(filters).flatten.compact + return WEB.dup if patterns.empty? - { - type: 'pattern', - protocol: uri.scheme || '', - hostname: uri.host || '', - port: uri.port.to_s, - pathname: uri.path || '', - search: uri.query || '' - } + patterns.each_with_object([]) do |pattern, array| + case pattern + when String, ::URI::Generic + array << { type: 'string', pattern: pattern.to_s } + when Hash + url_pattern = to_url_pattern(pattern) + if url_pattern.key?(:protocol) + array << url_pattern + else + array << url_pattern.merge(protocol: 'http') + array << url_pattern.merge(protocol: 'https') + end + else + raise TypeError, "pattern must be a String, URI or a Hash of keys: #{ALLOWED_KEYS.join(", ")}" + end end + end - def to_url_string_pattern(*url_patterns) - url_patterns.flatten.map do |url_pattern| - { - type: 'string', - pattern: url_pattern - } - end + private + + def to_url_pattern(pattern) + unknown = pattern.keys - ALLOWED_KEYS + raise ArgumentError, "Unknown keys in pattern hash: #{unknown.inspect}" unless unknown.empty? + + pattern.slice(*ALLOWED_KEYS).merge(type: 'pattern') end end end # BiDi diff --git a/rb/lib/selenium/webdriver/common/network.rb b/rb/lib/selenium/webdriver/common/network.rb index 765ba88097a19..660b9dd01d786 100644 --- a/rb/lib/selenium/webdriver/common/network.rb +++ b/rb/lib/selenium/webdriver/common/network.rb @@ -44,7 +44,7 @@ def clear_handlers callbacks.each_key { |id| remove_handler(id) } end - def add_authentication_handler(username = nil, password = nil, *filter, pattern_type: nil, &block) + def add_authentication_handler(username = nil, password = nil, *filters, &block) selected_block = if username && password proc { |auth| auth.authenticate(username, password) } @@ -56,38 +56,35 @@ def add_authentication_handler(username = nil, password = nil, *filter, pattern_ :auth_required, BiDi::Network::PHASES[:auth_required], BiDi::InterceptedAuth, - filter, - pattern_type: pattern_type, + Array(filters).flatten, &selected_block ) end - def add_request_handler(*filter, pattern_type: nil, &block) + def add_request_handler(*filters, &block) add_handler( :before_request, BiDi::Network::PHASES[:before_request], BiDi::InterceptedRequest, - filter, - pattern_type: pattern_type, + Array(filters).flatten, &block ) end - def add_response_handler(*filter, pattern_type: nil, &block) + def add_response_handler(*filters, &block) add_handler( :response_started, BiDi::Network::PHASES[:response_started], BiDi::InterceptedResponse, - filter, - pattern_type: pattern_type, + Array(filters).flatten, &block ) end private - def add_handler(event_type, phase, intercept_type, filter, pattern_type: nil) - intercept = network.add_intercept(phases: [phase], url_patterns: [filter].flatten, pattern_type: pattern_type) + def add_handler(event_type, phase, intercept_type, filters) + intercept = network.add_intercept(phases: [phase], filters: filters) callback_id = network.on(event_type) do |event| request = event['request'] intercepted_item = intercept_type.new(network, request) diff --git a/rb/sig/lib/selenium/webdriver/bidi/network.rbs b/rb/sig/lib/selenium/webdriver/bidi/network.rbs index bc286a14b245d..3dd8463de0b1d 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network.rbs @@ -10,7 +10,7 @@ module Selenium def initialize: (BiDi bidi) -> void - def add_intercept: (?phases: Array[String], ?contexts: BrowsingContext?, ?url_patterns: String | Array[String]?) -> Hash[String, String] + def add_intercept: (?phases: Array[String], ?contexts: BrowsingContext?, ?filters: Array[String | Hash[Symbol, String]]?) -> Hash[String, String] def cancel_auth: -> Hash[nil, nil] @@ -22,7 +22,7 @@ module Selenium def fail_request: -> Hash[nil, nil] - def remove_intercept: (String intercept) -> Hash[nil, nil] + def remove_intercept: (String? intercept) -> Hash[nil, nil] def continue_with_auth: (Integer request_id, String username, String password) -> Hash[nil, nil] diff --git a/rb/sig/lib/selenium/webdriver/bidi/network/url_pattern.rbs b/rb/sig/lib/selenium/webdriver/bidi/network/url_pattern.rbs index 57353479f69f7..f294b7eaabf6f 100644 --- a/rb/sig/lib/selenium/webdriver/bidi/network/url_pattern.rbs +++ b/rb/sig/lib/selenium/webdriver/bidi/network/url_pattern.rbs @@ -2,11 +2,13 @@ module Selenium module WebDriver class BiDi module UrlPattern - def self?.format_pattern: (Array[String] | String url_patterns, Symbol pattern_type) -> Array[Hash[Symbol, String]] + ALLOWED_KEYS: Array[Symbol] - def self?.to_url_pattern: (*String url_patterns) -> Array[Hash[Symbol, String]] + WEB: Array[Hash[Symbol, String]] - def self?.to_url_string_pattern: (*String url_patterns) -> Array[Hash[Symbol, String]] + def self?.format_pattern: (nil | Array[String] | Array[URI] | Array[Hash[Symbol, String]]) -> Array[Hash[Symbol, String]] + + def self?.to_url_pattern: (Hash[Symbol, String]) -> Hash[Symbol, String] end end end diff --git a/rb/sig/lib/selenium/webdriver/common/network.rbs b/rb/sig/lib/selenium/webdriver/common/network.rbs index 193e7385439af..58a09d445fa71 100644 --- a/rb/sig/lib/selenium/webdriver/common/network.rbs +++ b/rb/sig/lib/selenium/webdriver/common/network.rbs @@ -11,21 +11,21 @@ module Selenium alias bidi network - def initialize: (Remote::Bridge bridge) -> void + def initialize: (Remote::BiDiBridge bridge) -> void def remove_handler: (Numeric id) -> untyped def clear_handlers: () -> untyped - def add_authentication_handler: (?String? username, ?String? password, *String filter, ?pattern_type: Symbol?) { (?) -> untyped } -> untyped + def add_authentication_handler: (?String? username, ?String? password, *(String | Hash[Symbol, String])?) { (BiDi::InterceptedAuth) -> void } -> Hash[String, String] - def add_request_handler: (*String filter, ?pattern_type: Symbol?) -> Hash[String, String] + def add_request_handler: (*(String | Hash[Symbol, String])?) { (BiDi::InterceptedRequest) -> void } -> Hash[String, String] - def add_response_handler: (*String filter, ?pattern_type: Symbol?) -> Hash[String, String] + def add_response_handler: (*(String | Hash[Symbol, String])?) { (BiDi::InterceptedResponse) -> void } -> Hash[String, String] private - def add_handler: (Symbol event_type, String phase, BiDi::InterceptedRequest | BiDi::InterceptedAuth | BiDi::InterceptedResponse intercept_type, Array[String] filter, ?pattern_type: Symbol?) { (untyped) -> untyped } -> untyped + def add_handler: (Symbol event_type, String phase, Class intercept_type, Array[String | Hash[Symbol, String]] filters) { (untyped) -> untyped } -> untyped end end end diff --git a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb index 8cc7fe27a2b67..4082cb1d02590 100644 --- a/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/bidi/network_spec.rb @@ -35,16 +35,14 @@ class BiDi it 'adds an intercept with a default pattern type' do network = described_class.new(driver.bidi) pattern = 'http://localhost:4444/formPage.html' - intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], url_patterns: pattern) + intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], filters: [pattern]) expect(intercept).not_to be_nil end it 'adds an intercept with a url pattern' do network = described_class.new(driver.bidi) pattern = 'http://localhost:4444/formPage.html' - intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], - url_patterns: pattern, - pattern_type: :url) + intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], filters: [pattern]) expect(intercept).not_to be_nil end diff --git a/rb/spec/integration/selenium/webdriver/network_spec.rb b/rb/spec/integration/selenium/webdriver/network_spec.rb index ae1b2bfa347b6..f55725307e83d 100644 --- a/rb/spec/integration/selenium/webdriver/network_spec.rb +++ b/rb/spec/integration/selenium/webdriver/network_spec.rb @@ -60,7 +60,7 @@ module WebDriver it 'adds an auth handler with a pattern type' do reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) - network.add_authentication_handler(username, password, url_for('basicAuth'), pattern_type: :url) + network.add_authentication_handler(username, password, url_for('basicAuth')) driver.navigate.to url_for('basicAuth') expect(driver.find_element(tag_name: 'h1').text).to eq('authorized') expect(network.callbacks.count).to be 1 @@ -135,7 +135,7 @@ module WebDriver it 'adds a request handler with a pattern type' do reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) - network.add_request_handler(url_for('formPage.html'), pattern_type: :url, &:continue) + network.add_request_handler(url_for('formPage.html'), &:continue) driver.navigate.to url_for('formPage.html') expect(driver.current_url).to eq(url_for('formPage.html')) expect(network.callbacks.count).to be 1 @@ -230,7 +230,7 @@ module WebDriver it 'adds a response handler with a pattern type' do reset_driver!(web_socket_url: true) do |driver| network = described_class.new(driver) - network.add_response_handler(url_for('formPage.html'), pattern_type: :url, &:continue) + network.add_response_handler(url_for('formPage.html'), &:continue) driver.navigate.to url_for('formPage.html') expect(driver.current_url).to eq(url_for('formPage.html')) expect(network.callbacks.count).to be 1