Skip to content

Commit

Permalink
Add browsertest for redirect content support
Browse files Browse the repository at this point in the history
  • Loading branch information
bbondy committed Nov 13, 2019
1 parent 4a4bcd2 commit 8267ce2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
6 changes: 5 additions & 1 deletion components/brave_shields/browser/ad_block_base_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 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();
}

Expand Down
2 changes: 1 addition & 1 deletion components/brave_shields/browser/ad_block_base_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;

Expand Down
36 changes: 34 additions & 2 deletions components/brave_shields/browser/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
"["
"{\"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);
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);
}
19 changes: 19 additions & 0 deletions test/data/blocking.html
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions test/data/js_mock_me.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing

0 comments on commit 8267ce2

Please sign in to comment.