Skip to content

Commit 797558d

Browse files
committed
fix: intercept should raise error on unknown resource type and request stage
fixes #313
1 parent 5992001 commit 797558d

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

lib/ferrum/network.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ module Ferrum
1111
class Network
1212
CLEAR_TYPE = %i[traffic cache].freeze
1313
AUTHORIZE_TYPE = %i[server proxy].freeze
14-
RESOURCE_TYPES = %w[Document Stylesheet Image Media Font Script TextTrack
15-
XHR Fetch EventSource WebSocket Manifest
16-
SignedExchange Ping CSPViolationReport Other].freeze
14+
REQUEST_STAGES = %i[Request Response].freeze
15+
RESOURCE_TYPES = %i[Document Stylesheet Image Media Font Script TextTrack
16+
XHR Fetch Prefetch EventSource WebSocket Manifest
17+
SignedExchange Ping CSPViolationReport Preflight Other].freeze
1718
AUTHORIZE_BLOCK_MISSING = "Block is missing, call `authorize(...) { |r| r.continue } " \
1819
"or subscribe to `on(:request)` events before calling it"
1920
AUTHORIZE_TYPE_WRONG = ":type should be in #{AUTHORIZE_TYPE}"
@@ -187,11 +188,20 @@ def whitelist=(patterns)
187188
# end
188189
# browser.go_to("https://google.com")
189190
#
190-
def intercept(pattern: "*", resource_type: nil)
191+
def intercept(pattern: "*", resource_type: nil, request_stage: nil, handle_auth_requests: true)
191192
pattern = { urlPattern: pattern }
192-
pattern[:resourceType] = resource_type if resource_type && RESOURCE_TYPES.include?(resource_type.to_s)
193193

194-
@page.command("Fetch.enable", handleAuthRequests: true, patterns: [pattern])
194+
if resource_type && RESOURCE_TYPES.none?(resource_type.to_sym)
195+
raise ArgumentError, "Unknown resource type '#{resource_type}' must be #{RESOURCE_TYPES.join(' | ')}"
196+
end
197+
198+
if request_stage && REQUEST_STAGES.none?(request_stage.to_sym)
199+
raise ArgumentError, "Unknown request stage '#{request_stage}' must be #{REQUEST_STAGES.join(' | ')}"
200+
end
201+
202+
pattern[:resourceType] = resource_type if resource_type
203+
pattern[:requestStage] = request_stage if request_stage
204+
@page.command("Fetch.enable", patterns: [pattern], handleAuthRequests: handle_auth_requests)
195205
end
196206

197207
#

spec/network_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,55 @@
302302
end
303303

304304
describe "#intercept" do
305+
it "supports :pattern argument" do
306+
network.intercept(pattern: "*/ferrum/frame_child")
307+
page.on(:request) do |request|
308+
request.respond(body: "<h1>hello</h1>")
309+
end
310+
311+
page.go_to("/ferrum/frame_parent")
312+
313+
expect(network.status).to eq(200)
314+
frame = page.at_xpath("//iframe").frame
315+
expect(frame.body).to include("hello")
316+
end
317+
318+
context "with :resource_type argument" do
319+
it "raises an error with wrong type" do
320+
expect { network.intercept(resource_type: :BlaBla) }.to raise_error(ArgumentError)
321+
end
322+
323+
it "intercepts only given type" do
324+
network.intercept(resource_type: :Document)
325+
page.on(:request) do |request|
326+
request.respond(body: "<h1>hello</h1>")
327+
end
328+
329+
page.go_to("/ferrum/non_existing")
330+
331+
expect(network.status).to eq(200)
332+
expect(page.body).to include("hello")
333+
end
334+
end
335+
336+
context "with :request_stage argument" do
337+
it "raises an error with wrong stage" do
338+
expect { network.intercept(request_stage: :BlaBla) }.to raise_error(ArgumentError)
339+
end
340+
341+
it "intercepts only given stage" do
342+
network.intercept(request_stage: :Response)
343+
page.on(:request) do |request|
344+
request.respond(body: "<h1>hello</h1>")
345+
end
346+
347+
page.go_to("/ferrum/index")
348+
349+
expect(network.status).to eq(200)
350+
expect(page.body).to include("hello")
351+
end
352+
end
353+
305354
it "supports custom responses" do
306355
network.intercept
307356
page.on(:request) do |request|

0 commit comments

Comments
 (0)