Path: blob/trunk/rb/lib/selenium/webdriver/common/network.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require 'forwardable'2021module Selenium22module WebDriver23class Network24extend Forwardable2526attr_reader :callbacks, :network27alias bidi network2829def_delegators :network, :continue_with_auth, :continue_with_request, :continue_with_response3031def initialize(bridge)32@network = BiDi::Network.new(bridge.bidi)33@callbacks = {}34end3536def remove_handler(id)37intercept = callbacks[id]38network.remove_intercept(intercept['intercept'])39callbacks.delete(id)40end4142def clear_handlers43callbacks.each_key { |id| remove_handler(id) }44end4546def add_authentication_handler(username = nil, password = nil, *filter, pattern_type: nil, &block)47selected_block =48if username && password49proc { |auth| auth.authenticate(username, password) }50else51block52end5354add_handler(55:auth_required,56BiDi::Network::PHASES[:auth_required],57BiDi::InterceptedAuth,58filter,59pattern_type: pattern_type,60&selected_block61)62end6364def add_request_handler(*filter, pattern_type: nil, &block)65add_handler(66:before_request,67BiDi::Network::PHASES[:before_request],68BiDi::InterceptedRequest,69filter,70pattern_type: pattern_type,71&block72)73end7475def add_response_handler(*filter, pattern_type: nil, &block)76add_handler(77:response_started,78BiDi::Network::PHASES[:response_started],79BiDi::InterceptedResponse,80filter,81pattern_type: pattern_type,82&block83)84end8586private8788def add_handler(event_type, phase, intercept_type, filter, pattern_type: nil)89intercept = network.add_intercept(phases: [phase], url_patterns: [filter].flatten, pattern_type: pattern_type)90callback_id = network.on(event_type) do |event|91request = event['request']92intercepted_item = intercept_type.new(network, request)93yield(intercepted_item)94end9596callbacks[callback_id] = intercept97callback_id98end99end # Network100end # WebDriver101end # Selenium102103104