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

Add support for ABP filter redirects as mock responses #3419

Merged
merged 4 commits into from Nov 19, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add browsertest for redirect content support

  • Loading branch information
bbondy committed Nov 12, 2019
commit 58f81907d2fce6848282280c502625ddf5361e56
@@ -244,12 +244,16 @@ bool AdBlockBaseService::Init() {
return true;
}

void AdBlockBaseService::ResetForTest(const std::string& rules) {
void AdBlockBaseService::ResetForTest(const std::string& rules,
const std::string& resources) {
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 14, 2019

Contributor

clang-format

// This is temporary until adblock-rust supports incrementally adding
// filter rules to an existing instance. At which point the hack below
// will dissapear.
ad_block_client_.reset(new adblock::Engine(rules));
AddKnownTagsToAdBlockInstance();
if (!resources.empty()) {
resources_ = resources;
}
AddKnownResourcesToAdBlockInstance();
}

@@ -54,7 +54,7 @@ class AdBlockBaseService : public BaseBraveShieldsService {
void GetDATFileData(const base::FilePath& dat_file_path);
void AddKnownTagsToAdBlockInstance();
void AddKnownResourcesToAdBlockInstance();
void ResetForTest(const std::string& rules);
void ResetForTest(const std::string& rules, const std::string& resources);

std::unique_ptr<adblock::Engine> ad_block_client_;

@@ -86,9 +86,10 @@ class AdBlockServiceTest : public ExtensionBrowserTest {
ASSERT_TRUE(g_brave_browser_process->ad_block_service()->IsInitialized());
}

void UpdateAdBlockInstanceWithRules(const char* rules) {
void UpdateAdBlockInstanceWithRules(const std::string& rules,
const std::string& resources = "") {
g_brave_browser_process->ad_block_service()
->ResetForTest(rules);
->ResetForTest(rules, resources);
}

void AssertTagExists(const std::string& tag, bool expected_exists) const {
@@ -744,3 +745,34 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CancelRequestOptionTest) {
EXPECT_TRUE(as_expected);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}

// Load a page with a script which uses a redirect.
IN_PROC_BROWSER_TEST_F(AdBlockServiceTest,
RedirectRulesAreRespected) {
UpdateAdBlockInstanceWithRules("js_mock_me.js$redirect=noopjs",
"["
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 14, 2019

Contributor

clang-format.
Also you may reformat it as a raw string literal to improve readability:

R"([
     { "name": ...
     }
   ]
)";```
"{\"name\":\"noop.js\",\"aliases\":[\"noopjs\"],"
"\"kind\":{\"mime\":\"application/javascript\"},"
"\"content\":\"KGZ1bmN0aW9uKCkgewogICAgJ3VzZSBzdHJpY3QnOwp9KSgpOwo=\"}"
"]");
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 0ULL);

GURL url = embedded_test_server()->GetURL("example.com", kAdBlockTestPage);
This conversation was marked as resolved by bbondy

This comment has been minimized.

Copy link
@iefremov

iefremov Nov 14, 2019

Contributor

nit: const for url, noopjs, resource_url

ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();

std::string noopjs = "(function() {\\n \\'use strict\\';\\n})();\\n";
bool as_expected = false;
GURL resource_url =
embedded_test_server()->GetURL("example.com", "/js_mock_me.js");
ASSERT_TRUE(ExecuteScriptAndExtractBool(contents,
base::StringPrintf(
"setExpectations(0, 0, 0, 1, 0, 0);"
"xhr_expect_content('%s', '%s');",
resource_url.spec().c_str(),
noopjs.c_str()),
&as_expected));
EXPECT_TRUE(as_expected);
EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL);
}
@@ -33,6 +33,25 @@
xhr.send();
}

// Performs an XHR for the specified src and reports successful,
// only if the content matches expected_content.
function xhr_expect_content(src, expected_content) {
const xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.onload = function(e) {
if (xhr.response === expected_content) {
numXHRLoaded++
}
onLoad()
}
xhr.onerror = function(e) {
console.log(e)
numXHRBlocked++
onLoad()
}
xhr.send();
}

// Adds an image to the DOM with the specified src
function addImage(src) {
const img = document.createElement('img')
@@ -0,0 +1 @@
testing
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.