Class: Ferrum::Network::InterceptedRequest

Inherits:
Object
  • Object
show all
Includes:
RequestParams
Defined in:
lib/ferrum/network/intercepted_request.rb

Overview

Represents a request paused by request interception (Fetch.enable), allowing it to be continued as is or with overrides, fulfilled with a fake response, or aborted before it reaches the network.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestParams

#headers, #method, #post_data, #url, #url_fragment

Constructor Details

#initialize(client, params) ⇒ InterceptedRequest

Returns a new instance of InterceptedRequest.



18
19
20
21
22
23
24
25
26
27
# File 'lib/ferrum/network/intercepted_request.rb', line 18

def initialize(client, params)
  @status = nil
  @client = client
  @params = params
  @request_id = params["requestId"]
  @frame_id = params["frameId"]
  @resource_type = params["resourceType"]
  @request = params["request"]
  @network_id = params["networkId"]
end

Instance Attribute Details

#frame_id ⇒ Object

Returns the value of attribute frame_id.



16
17
18
# File 'lib/ferrum/network/intercepted_request.rb', line 16

def frame_id
  @frame_id
end

#network_id ⇒ Object

Returns the value of attribute network_id.



16
17
18
# File 'lib/ferrum/network/intercepted_request.rb', line 16

def network_id
  @network_id
end

#request_id ⇒ Object

Returns the value of attribute request_id.



16
17
18
# File 'lib/ferrum/network/intercepted_request.rb', line 16

def request_id
  @request_id
end

#resource_type ⇒ Object

Returns the value of attribute resource_type.



16
17
18
# File 'lib/ferrum/network/intercepted_request.rb', line 16

def resource_type
  @resource_type
end

#status ⇒ Object

Returns the value of attribute status.



16
17
18
# File 'lib/ferrum/network/intercepted_request.rb', line 16

def status
  @status
end

Instance Method Details

#abort ⇒ Object

Aborts the intercepted request.

Examples:

request.abort


131
132
133
134
# File 'lib/ferrum/network/intercepted_request.rb', line 131

def abort
  @status = :aborted
  @client.command("Fetch.failRequest", async: true, requestId: request_id, errorReason: "BlockedByClient")
end

#continue(**options) ⇒ Object

Continues the intercepted request, letting it reach the network as is, or with the given overrides.

Examples:

request.continue

Parameters:

  • options (Hash)

Options Hash (**options):

  • :url (String) —

    Overrides the request's URL.

  • :method (String) —

    Overrides the request's method.

  • :postData (String) —

    Overrides the request's post data.

  • :headers (Hash) —

    Overrides the request's headers.



119
120
121
122
123
# File 'lib/ferrum/network/intercepted_request.rb', line 119

def continue(**options)
  options = options.merge(requestId: request_id)
  @status = :continued
  @client.command("Fetch.continueRequest", async: true, **options)
end

#initial_priority ⇒ String

The request's initial priority, one of "VeryLow", "Low", "Medium", "High", or "VeryHigh".

Returns:

  • (String)


142
143
144
# File 'lib/ferrum/network/intercepted_request.rb', line 142

def initial_priority
  @request["initialPriority"]
end

#inspect ⇒ String

Inspects the intercepted request.

Returns:

  • (String)


160
161
162
163
164
165
166
# File 'lib/ferrum/network/intercepted_request.rb', line 160

def inspect
  "#<#{self.class} " \
    "@request_id=#{@request_id.inspect} " \
    "@frame_id=#{@frame_id.inspect} " \
    "@resource_type=#{@resource_type.inspect} " \
    "@request=#{@request.inspect}>"
end

#match?(pattern) ⇒ Boolean

Whether the request's URL matches the given pattern.

Parameters:

  • pattern (String, Regexp)

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/ferrum/network/intercepted_request.rb', line 58

def match?(pattern)
  case url
  when pattern
    true
  else
    false
  end
end

Whether this request is for the navigation of a frame, as opposed to a subresource (script, image, XHR, etc).

Returns:

  • (Boolean)


47
48
49
# File 'lib/ferrum/network/intercepted_request.rb', line 47

def navigation_request?
  @params["isNavigationRequest"]
end

#referrer_policy ⇒ String

The request's referrer policy.

Returns:

  • (String)


151
152
153
# File 'lib/ferrum/network/intercepted_request.rb', line 151

def referrer_policy
  @request["referrerPolicy"]
end

#respond(**options) ⇒ Object

Fulfills the intercepted request with a fake response instead of letting it reach the network.

Examples:

request.respond(body: "Lorem ipsum")

Parameters:

  • options (Hash)

Options Hash (**options):

  • :responseCode (Integer) —

    HTTP status code to respond with, 200 by default.

  • :body (String) —

    Response body.

  • :responseHeaders (Hash) —

    Response headers.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ferrum/network/intercepted_request.rb', line 85

def respond(**options)
  has_body = options.key?(:body)
  headers = has_body ? { "content-length" => options.fetch(:body, "").length } : {}
  headers = headers.merge(options.fetch(:responseHeaders, {}))

  options = { responseCode: 200 }.merge(options)
  options = options.merge(requestId: request_id, responseHeaders: header_array(headers))
  options = options.merge(body: Base64.strict_encode64(options.fetch(:body, ""))) if has_body

  @status = :responded
  @client.command("Fetch.fulfillRequest", async: true, **options)
end

#status?(value) ⇒ Boolean

Whether the request's current status matches the given value.

Parameters:

  • value (String, Symbol) —

    One of :responded, :continued, :aborted.

Returns:

  • (Boolean)


37
38
39
# File 'lib/ferrum/network/intercepted_request.rb', line 37

def status?(value)
  @status == value.to_sym
end