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/features/driver_spec.rb b/spec/features/driver_spec.rb index 1ffebd6..15771c7 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/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") }