From b77112d5ea8e56f0ec47c33a8fd1261b24526790 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Fri, 24 Jun 2016 10:59:04 -0700 Subject: [PATCH] Support wildcard matching for white/blacklists --- CHANGELOG.md | 1 + .../poltergeist/client/browser.coffee | 15 +++++--- .../poltergeist/client/compiled/browser.js | 34 ++++++++++++++++-- .../poltergeist/client/compiled/web_page.js | 8 ++--- .../poltergeist/client/web_page.coffee | 8 ++--- spec/integration/driver_spec.rb | 35 ++++++++++++++++++- 6 files changed, 85 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c57e5bd..b0fd12e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Add clear_memory_cache to the driver (Piotr Gaertig) * Allowing passing format and quality options to save_screenshot (Josef Šimánek, Thomas Walpole) * Allow configuring default url_blacklist and url_whitelist in the driver configuration (Thomas Walpole) +* Support wildcard format in url_black/whitelist (Thomas Walpole) #### Bug fixes #### * Fix within_frame when called on a frame whose src attribute is about:blank (Thomas Walpole) [Issue #772] diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index 2e94098..25c061c 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -496,12 +496,12 @@ class Poltergeist.Browser else command.sendResponse(false) - set_url_whitelist: -> - @currentPage.urlWhitelist = Array.prototype.slice.call(arguments) + set_url_whitelist: (wildcards...)-> + @currentPage.urlWhitelist = (@_wildcardToRegexp(wc) for wc in wildcards) @current_command.sendResponse(true) - set_url_blacklist: -> - @currentPage.urlBlacklist = Array.prototype.slice.call(arguments) + set_url_blacklist: (wildcards...)-> + @currentPage.urlBlacklist = (@_wildcardToRegexp(wc) for wc in wildcards) @current_command.sendResponse(true) set_confirm_process: (process) -> @@ -518,3 +518,10 @@ class Poltergeist.Browser clear_memory_cache: -> @currentPage.clearMemoryCache() @current_command.sendResponse(true) + + _wildcardToRegexp: (wildcard)-> + wildcard = wildcard.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&") + wildcard = wildcard.replace(/\*/g, ".*") + wildcard = wildcard.replace(/\?/g, ".") + new RegExp(wildcard, "i") + diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index bf653eb..ea5dd79 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -1,4 +1,5 @@ -var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; +var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, + slice = [].slice; Poltergeist.Browser = (function() { function Browser(width, height) { @@ -676,12 +677,32 @@ Poltergeist.Browser = (function() { }; Browser.prototype.set_url_whitelist = function() { - this.currentPage.urlWhitelist = Array.prototype.slice.call(arguments); + var wc, wildcards; + wildcards = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.currentPage.urlWhitelist = (function() { + var i, len, results; + results = []; + for (i = 0, len = wildcards.length; i < len; i++) { + wc = wildcards[i]; + results.push(this._wildcardToRegexp(wc)); + } + return results; + }).call(this); return this.current_command.sendResponse(true); }; Browser.prototype.set_url_blacklist = function() { - this.currentPage.urlBlacklist = Array.prototype.slice.call(arguments); + var wc, wildcards; + wildcards = 1 <= arguments.length ? slice.call(arguments, 0) : []; + this.currentPage.urlBlacklist = (function() { + var i, len, results; + results = []; + for (i = 0, len = wildcards.length; i < len; i++) { + wc = wildcards[i]; + results.push(this._wildcardToRegexp(wc)); + } + return results; + }).call(this); return this.current_command.sendResponse(true); }; @@ -704,6 +725,13 @@ Poltergeist.Browser = (function() { return this.current_command.sendResponse(true); }; + Browser.prototype._wildcardToRegexp = function(wildcard) { + wildcard = wildcard.replace(/[\-\[\]\/\{\}\(\)\+\.\\\^\$\|]/g, "\\$&"); + wildcard = wildcard.replace(/\*/g, ".*"); + wildcard = wildcard.replace(/\?/g, "."); + return new RegExp(wildcard, "i"); + }; + return Browser; })(); diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index b1c65e5..2545f21 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -117,11 +117,11 @@ Poltergeist.WebPage = (function() { WebPage.prototype.onResourceRequestedNative = function(request, net) { var abort, blacklisted, ref2, useWhitelist, whitelisted; useWhitelist = this.urlWhitelist.length > 0; - whitelisted = this.urlWhitelist.some(function(whitelisted_url) { - return request.url.indexOf(whitelisted_url) !== -1; + whitelisted = this.urlWhitelist.some(function(whitelisted_regex) { + return whitelisted_regex.test(request.url); }); - blacklisted = this.urlBlacklist.some(function(blacklisted_url) { - return request.url.indexOf(blacklisted_url) !== -1; + blacklisted = this.urlBlacklist.some(function(blacklisted_regex) { + return blacklisted_regex.test(request.url); }); abort = false; if (useWhitelist && !whitelisted) { diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index c5cc4ec..5ccbd45 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -83,11 +83,11 @@ class Poltergeist.WebPage onResourceRequestedNative: (request, net) -> useWhitelist = @urlWhitelist.length > 0 - whitelisted = @urlWhitelist.some (whitelisted_url) -> - request.url.indexOf(whitelisted_url) != -1 + whitelisted = @urlWhitelist.some (whitelisted_regex) -> + whitelisted_regex.test request.url - blacklisted = @urlBlacklist.some (blacklisted_url) -> - request.url.indexOf(blacklisted_url) != -1 + blacklisted = @urlBlacklist.some (blacklisted_regex) -> + blacklisted_regex.test request.url abort = false diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index dafe6ff..d3d3b8d 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -952,6 +952,21 @@ module Capybara::Poltergeist end end + it 'supports wildcards' do + @driver.browser.url_blacklist = ['*wanted'] + + @session.visit '/poltergeist/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).not_to 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 + it 'can be configured in the driver and survive reset' do Capybara.register_driver :poltergeist_blacklist do |app| Capybara::Poltergeist::Driver.new(app, @driver.options.merge(url_blacklist: ['unwanted'])) @@ -977,7 +992,7 @@ module Capybara::Poltergeist context 'whitelisting urls for resource requests' do it 'allows whitelisted urls' do - @driver.browser.url_whitelist = ['url_whitelist', 'wanted'] + @driver.browser.url_whitelist = ['url_whitelist', '/wanted'] @session.visit '/poltergeist/url_whitelist' @@ -986,6 +1001,24 @@ module Capybara::Poltergeist @session.within_frame 'framename' do expect(@session).to 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 + + it 'supports wildcards' do + @driver.browser.url_whitelist = ['url_whitelist', '/*wanted'] + + @session.visit '/poltergeist/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('We should see this.') + end + @session.within_frame 'unwantedframe' do + expect(@session).to have_content("We shouldn't see this.") + end end it 'blocks overruled urls' do