Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request interception when using a black/whitelist #140

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions lib/capybara/cuprite/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,14 @@ def prepare_page
!Array(@browser.url_blacklist).empty?

on(:request) do |request, index, total|
if @browser.url_blacklist && [email protected]_blacklist.empty?
if @browser.url_blacklist.any? { |r| request.match?(r) }
request.abort and next
else
request.continue and next
end
elsif @browser.url_whitelist && [email protected]_whitelist.empty?
if @browser.url_whitelist.any? { |r| request.match?(r) }
request.continue and next
else
request.abort and next
end
elsif index + 1 < total
# There are other callbacks that may handle this request
next
else
# If there are no callbacks then just continue
if @browser.url_blacklist && @browser.url_blacklist.any? { |r| request.match?(r) }
request.abort and next
elsif @browser.url_whitelist && [email protected]_whitelist.empty? && @browser.url_whitelist.none? { |r| request.match?(r) }
request.abort and next
end

if index + 1 >= total
# Continue the request if there are no other callbacks
request.continue
end
end
Expand Down
58 changes: 58 additions & 0 deletions spec/features/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,23 @@ def create_screenshot(file, *args)
end
end

it "continues to other intercepted request callbacks if URL is not blacklisted" do
@driver.browser.url_blacklist = ["unwanted"]

@driver.browser.on(:request) do |request, index, total|
if request.match?(/simple/)
request.respond(body: 'Intercepted')
else
request.continue
end
end

@session.visit "/cuprite/simple"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("Intercepted")
end

it "supports wildcards" do
@driver.browser.url_blacklist = ["*wanted"]

Expand Down Expand Up @@ -1194,6 +1211,29 @@ def create_screenshot(file, *args)
end
end

it "continues to other intercepted request callbacks if URL matches whitelist" do
@driver.browser.url_whitelist = ["url_whitelist", "/wanted"]

@driver.browser.on(:request) do |request, index, total|
if request.match?(/wanted/)
request.respond(body: 'Intercepted')
else
request.continue
end
end

@session.visit "/cuprite/url_whitelist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
@session.within_frame "framename" do
expect(@session).to have_content("Intercepted")
end
@session.within_frame "unwantedframe" do
expect(@session).not_to have_content("We shouldn't see this.")
end
end

it "supports wildcards" do
@driver.browser.url_whitelist = ["url_whitelist", "/*wanted"]

Expand Down Expand Up @@ -1263,6 +1303,24 @@ def create_screenshot(file, *args)
end
end

context "combining resource requests blacklist and whitelist" do
it "permits requests that do not match blacklist and match whitelist" do
@driver.browser.url_blacklist = ["unwanted"]
@driver.browser.url_whitelist = ["url_whitelist", "unwanted"]

@session.visit "/cuprite/url_whitelist"

expect(@session.status_code).to eq(200)
expect(@session).to have_content("We are loading some wanted action here")
@session.within_frame "framename" do
expect(@session).to_not have_content("We should see this.")
end
@session.within_frame "unwantedframe" do
expect(@session).not_to have_content("We shouldn't see this.")
end
end
end

context "has ability to send keys" do
before { @session.visit("/cuprite/send_keys") }

Expand Down