Class: Puppeteer::NetworkManager

Inherits:
Object
  • Object
show all
Includes:
DebugPrint, EventCallbackable, IfPresent
Defined in:
lib/puppeteer/network_manager.rb

Defined Under Namespace

Classes: Credentials

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Methods included from EventCallbackable

#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(client, ignore_https_errors, frame_manager) ⇒ NetworkManager

Returns a new instance of NetworkManager.

Parameters:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppeteer/network_manager.rb', line 19

def initialize(client, ignore_https_errors, frame_manager)
  @client = client
  @ignore_https_errors = ignore_https_errors
  @frame_manager = frame_manager

  # @type {!Map<string, !Request>}
  @request_id_to_request = {}

  # @type {!Map<string, !Protocol.Network.requestWillBeSentPayload>}
  @request_id_to_request_with_be_sent_event = {}

  @extra_http_headers = {}

  @offline = false

  @attempted_authentications = Set.new
  @user_request_interception_enabled = false
  @protocol_request_interception_enabled = false
  @user_cache_disabled = false
  @request_id_to_interception_id = {}

  @client.on_event('Fetch.requestPaused') do |event|
    handle_request_paused(event)
  end
  @client.on_event('Fetch.authRequired') do |event|
    handle_auth_required(event)
  end
  @client.on_event('Network.requestWillBeSent') do |event|
    handle_request_will_be_sent(event)
  end
  @client.on_event('Network.requestServedFromCache') do |event|
    handle_request_served_from_cache(event)
  end
  @client.on_event('Network.responseReceived') do |event|
    handle_response_received(event)
  end
  @client.on_event('Network.loadingFinished') do |event|
    handle_loading_finished(event)
  end
  @client.on_event('Network.loadingFailed') do |event|
    handle_loading_failed(event)
  end
end

Instance Method Details

#authenticate(username:, password:) ⇒ Object

Parameters:

  • username (String|NilClass)
  • password (String|NilClass)


72
73
74
75
# File 'lib/puppeteer/network_manager.rb', line 72

def authenticate(username:, password:)
  @credentials = Credentials.new(username: username, password: password)
  update_protocol_request_interception
end

#cache_enabled=(enabled) ⇒ Object



113
114
115
116
# File 'lib/puppeteer/network_manager.rb', line 113

def cache_enabled=(enabled)
  @user_cache_disabled = !enabled
  update_protocol_cache_disabled
end

#extra_http_headers!Object<string, string>

Returns:

  • (!Object<string, string>)


91
92
93
# File 'lib/puppeteer/network_manager.rb', line 91

def extra_http_headers
  @extra_http_headers.dup
end

#extra_http_headers=(headers) ⇒ Object

Parameters:

  • extraHTTPHeaders (!Object<string, string>)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppeteer/network_manager.rb', line 78

def extra_http_headers=(headers)
  new_extra_http_headers = {}
  headers.each do |key, value|
    unless value.is_a?(String)
      raise ArgumentError.new("Expected value of header \"#{key}\" to be String, but \"#{value}\" is found.")
    end
    new_extra_http_headers[key.downcase] = value
  end
  @extra_http_headers = new_extra_http_headers
  @client.send_message('Network.setExtraHTTPHeaders', headers: new_extra_http_headers)
end

#initObject



63
64
65
66
67
68
# File 'lib/puppeteer/network_manager.rb', line 63

def init
  @client.send_message('Network.enable')
  if @ignore_https_errors
    @client.send_message('Security.setIgnoreCertificateErrors', ignore: true)
  end
end

#offline_mode=(value) ⇒ Object

Parameters:

  • value (TrueClass|FalseClass)


96
97
98
99
100
101
102
103
104
105
106
# File 'lib/puppeteer/network_manager.rb', line 96

def offline_mode=(value)
  return if @offline == value
  @offline = value
  @client.send_message('Network.emulateNetworkConditions',
    offline: @offline,
    # values of 0 remove any active throttling. crbug.com/456324#c9
    latency: 0,
    downloadThroughput: -1,
    uploadThroughput: -1,
  )
end

#request_interception=(enabled) ⇒ Object



118
119
120
121
# File 'lib/puppeteer/network_manager.rb', line 118

def request_interception=(enabled)
  @user_request_interception_enabled = enabled
  update_protocol_request_interception
end

#user_agent=(user_agent) ⇒ Object

Parameters:

  • user_agent (String)


109
110
111
# File 'lib/puppeteer/network_manager.rb', line 109

def user_agent=(user_agent)
  @client.send_message('Network.setUserAgentOverride', userAgent: user_agent)
end