From e67531cb1e6a26f7f074a8856c8d90ac3392deb6 Mon Sep 17 00:00:00 2001 From: Jim Ryan Date: Tue, 8 Dec 2020 18:35:15 -0500 Subject: [PATCH 1/2] Fix spec failing due to ff7b3089 changing the with_js view and thus its size --- spec/integration/driver_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index 87bb31e..3c461a5 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -783,7 +783,7 @@ def create_screenshot(file, *args) %r{/cuprite/jquery.min.js$} => File.size(CUPRITE_ROOT + "/spec/support/public/jquery-1.11.3.min.js"), %r{/cuprite/jquery-ui.min.js$} => File.size(CUPRITE_ROOT + "/spec/support/public/jquery-ui-1.11.4.min.js"), %r{/cuprite/test.js$} => File.size(CUPRITE_ROOT + "/spec/support/public/test.js"), - %r{/cuprite/with_js$} => 2329 + %r{/cuprite/with_js$} => 2405 } resources_size.each do |resource, size| From 4e91fa937cd06cefdd36bf7398aa9aca7a449076 Mon Sep 17 00:00:00 2001 From: Jim Ryan Date: Tue, 8 Dec 2020 18:39:46 -0500 Subject: [PATCH 2/2] Only continue intercepted request if it passes black & white lists and the black/whitelist callback is the last callback Previously, if the request didn't match the blacklist, or matched the whitelist, it'd be continued, without regard for if there were other callbacks. This prohibited those callbacks from aborting the request or responding to it. This also permits blacklists and whitelists to be combined. Before, if both a white and blacklist were configured, the whitelist would be ignored. Now, if both are configured, only requests that do not match the blacklist and match the whitelist are permitted. --- lib/capybara/cuprite/page.rb | 25 +++++--------- spec/integration/driver_spec.rb | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/lib/capybara/cuprite/page.rb b/lib/capybara/cuprite/page.rb index f36a722..eb03615 100644 --- a/lib/capybara/cuprite/page.rb +++ b/lib/capybara/cuprite/page.rb @@ -135,23 +135,14 @@ def prepare_page !Array(@browser.url_blacklist).empty? on(:request) do |request, index, total| - if @browser.url_blacklist && !@browser.url_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 && !@browser.url_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 && !@browser.url_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 diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index 3c461a5..85532a0 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -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"] @@ -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"] @@ -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") }