mirror of
https://github.com/teampoltergeist/poltergeist.git
synced 2022-11-09 12:05:00 -05:00
Support wildcard matching for white/blacklists
This commit is contained in:
parent
ce93ea06b8
commit
b77112d5ea
6 changed files with 85 additions and 16 deletions
|
@ -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]
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
})();
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue