Commit ff95fa93 authored by dgozman@chromium.org's avatar dgozman@chromium.org

[DevTools] Allow stars in blocked url pattern.

BUG=none

Review URL: https://codereview.chromium.org/1328273003

git-svn-id: svn://svn.chromium.org/blink/trunk@201947 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 69bf0047
...@@ -4,15 +4,85 @@ Tests that blocked reason is recognized correctly. ...@@ -4,15 +4,85 @@ Tests that blocked reason is recognized correctly.
Running: testCSP Running: testCSP
Request: csp.js
BlockedReason: csp BlockedReason: csp
Running: testDevTools Running: testBlockedByDevTools1
Blocked patterns: resources**/silent*.js
Request: resources/silent_script.js
BlockedReason: inspector
Running: testBlockedByDevTools2
Blocked patterns: a*b
Request: ba
BlockedReason: undefined
Running: testBlockedByDevTools3
Blocked patterns: ***pattern***
Request: there/is/a/pattern/inside.js
BlockedReason: inspector
Running: testBlockedByDevTools4
Blocked patterns: pattern
Request: patt1ern
BlockedReason: undefined
Running: testBlockedByDevTools5
Blocked patterns: *this***is*a*pattern
Request: file/this/is/the/pattern
BlockedReason: undefined
Running: testBlockedByDevTools6
Blocked patterns: *this***is*a*pattern
Request: this/is/a/pattern
BlockedReason: inspector
Running: testBlockedByDevTools6
Blocked patterns: *this***is*a*pattern
Request: this/is
BlockedReason: undefined
Running: testBlockedByDevTools7
Blocked patterns: pattern
Request: long/pattern/inside
BlockedReason: inspector
Running: testBlockedByDevTools8
Blocked patterns: pattern
Request: pattern
BlockedReason: inspector
Running: testBlockedByDevTools9
Blocked patterns: pattern;pattern
Request: pattern
BlockedReason: inspector
Running: testBlockedByDevTools10
Blocked patterns: a*b*c*d*e
Request: edcbaedcbaedcbaedcba
BlockedReason: undefined
Running: testBlockedByDevTools11
Blocked patterns: a*b*c*d*e
Request: edcbaedcbaedcbaedcbae
BlockedReason: inspector
Running: testBlockedByDevTools12
Blocked patterns: one1;two2
Request: one1two2
BlockedReason: inspector
Running: testBlockedByDevTools13
Blocked patterns: one1;two2;three3
Request: four4
BlockedReason: undefined
Request: silent_script.js Running: testBlockedByDevTools14
Blocked patterns: one1;two2;three3
Request: only-two2-here
BlockedReason: inspector BlockedReason: inspector
Request: silent_script.js Running: cleanupBlockedURLs
Blocked patterns:
Request: resources/silent_script.js
BlockedReason: undefined BlockedReason: undefined
...@@ -11,10 +11,10 @@ function sendCSPRequest() ...@@ -11,10 +11,10 @@ function sendCSPRequest()
document.head.appendChild(script); document.head.appendChild(script);
} }
function addBlockedScript() function addBlockedScript(url)
{ {
var script = document.createElement("script"); var script = document.createElement("script");
script.src = "resources/silent_script.js"; script.src = url;
document.head.appendChild(script); document.head.appendChild(script);
} }
...@@ -30,14 +30,27 @@ function test() ...@@ -30,14 +30,27 @@ function test()
if (request.name() !== requestName) if (request.name() !== requestName)
return; return;
requestName = undefined; requestName = undefined;
InspectorTest.addResult("");
InspectorTest.addResult("Request: " + request.name());
InspectorTest.addResult("BlockedReason: " + request.blockedReason()); InspectorTest.addResult("BlockedReason: " + request.blockedReason());
nextStep(); nextStep();
} }
InspectorTest.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestFinished, onRequest); InspectorTest.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestFinished, onRequest);
function testBlockedURL(patterns, url, next)
{
InspectorTest.addResult("Blocked patterns: " + patterns.join(";"));
InspectorTest.addResult("Request: " + url);
blockedSetting.set(patterns);
nextStep = next;
InspectorTest.runAfterPendingDispatches(addScript);
function addScript()
{
requestName = url.substring(url.lastIndexOf("/") + 1);
InspectorTest.evaluateInPage("addBlockedScript(\"" + url + "\")");
}
}
InspectorTest.runTestSuite([ InspectorTest.runTestSuite([
function testCSP(next) function testCSP(next)
{ {
...@@ -46,24 +59,84 @@ function test() ...@@ -46,24 +59,84 @@ function test()
InspectorTest.evaluateInPage("sendCSPRequest()"); InspectorTest.evaluateInPage("sendCSPRequest()");
}, },
function testDevTools(next) function testBlockedByDevTools1(next)
{ {
blockedSetting.set(["http://some.strange.origin/script.js", "resources/silent_script.js"]); testBlockedURL(["resources**/silent*.js"], "resources/silent_script.js", next);
nextStep = removeBlock; },
InspectorTest.runAfterPendingDispatches(addScript);
function removeBlock() function testBlockedByDevTools2(next)
{ {
blockedSetting.set(["http://some.strange.origin/script.js"]); testBlockedURL(["a*b"], "ba", next);
nextStep = next; },
InspectorTest.runAfterPendingDispatches(addScript);
} function testBlockedByDevTools3(next)
{
testBlockedURL(["***pattern***"], "there/is/a/pattern/inside.js", next);
},
function addScript() function testBlockedByDevTools4(next)
{ {
requestName = "silent_script.js"; testBlockedURL(["pattern"], "patt1ern", next);
InspectorTest.evaluateInPage("addBlockedScript()"); },
}
function testBlockedByDevTools5(next)
{
testBlockedURL(["*this***is*a*pattern"], "file/this/is/the/pattern", next);
},
function testBlockedByDevTools6(next)
{
testBlockedURL(["*this***is*a*pattern"], "this/is/a/pattern", next);
},
function testBlockedByDevTools6(next)
{
testBlockedURL(["*this***is*a*pattern"], "this/is", next);
},
function testBlockedByDevTools7(next)
{
testBlockedURL(["pattern"], "long/pattern/inside", next);
},
function testBlockedByDevTools8(next)
{
testBlockedURL(["pattern"], "pattern", next);
},
function testBlockedByDevTools9(next)
{
testBlockedURL(["pattern", "pattern"], "pattern", next);
},
function testBlockedByDevTools10(next)
{
testBlockedURL(["a*b*c*d*e"], "edcbaedcbaedcbaedcba", next);
},
function testBlockedByDevTools11(next)
{
testBlockedURL(["a*b*c*d*e"], "edcbaedcbaedcbaedcbae", next);
},
function testBlockedByDevTools12(next)
{
testBlockedURL(["one1", "two2"], "one1two2", next);
},
function testBlockedByDevTools13(next)
{
testBlockedURL(["one1", "two2", "three3"], "four4", next);
},
function testBlockedByDevTools14(next)
{
testBlockedURL(["one1", "two2", "three3"], "only-two2-here", next);
},
function cleanupBlockedURLs(next)
{
testBlockedURL([], "resources/silent_script.js", next);
} }
]); ]);
} }
......
...@@ -95,6 +95,22 @@ namespace { ...@@ -95,6 +95,22 @@ namespace {
// Keep in sync with kDevToolsRequestInitiator defined in devtools_network_controller.cc // Keep in sync with kDevToolsRequestInitiator defined in devtools_network_controller.cc
const char kDevToolsEmulateNetworkConditionsClientId[] = "X-DevTools-Emulate-Network-Conditions-Client-Id"; const char kDevToolsEmulateNetworkConditionsClientId[] = "X-DevTools-Emulate-Network-Conditions-Client-Id";
// Pattern may contain stars ('*') which match to any (possibly empty) string.
// Stars implicitly assumed at the begin/end of pattern.
bool matches(const String& url, const String& pattern)
{
Vector<String> parts;
pattern.split("*", parts);
size_t pos = 0;
for (const String& part : parts) {
pos = url.find(part, pos);
if (pos == kNotFound)
return false;
pos += part.length();
}
return true;
}
static PassRefPtr<JSONObject> buildObjectForHeaders(const HTTPHeaderMap& headers) static PassRefPtr<JSONObject> buildObjectForHeaders(const HTTPHeaderMap& headers)
{ {
RefPtr<JSONObject> headersObject = JSONObject::create(); RefPtr<JSONObject> headersObject = JSONObject::create();
...@@ -407,7 +423,7 @@ bool InspectorResourceAgent::shouldBlockRequest(const ResourceRequest& request) ...@@ -407,7 +423,7 @@ bool InspectorResourceAgent::shouldBlockRequest(const ResourceRequest& request)
String url = request.url().string(); String url = request.url().string();
RefPtr<JSONObject> blockedURLs = m_state->getObject(ResourceAgentState::blockedURLs); RefPtr<JSONObject> blockedURLs = m_state->getObject(ResourceAgentState::blockedURLs);
for (const auto& entry : *blockedURLs) { for (const auto& entry : *blockedURLs) {
if (url.contains(entry.key)) if (matches(url, entry.key))
return true; return true;
} }
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment