-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #600 from notriddle/notriddle/block-network-request
Add block-network-request command
- Loading branch information
Showing
20 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// List commands handling network blocking. | ||
|
||
const { validator } = require('../validator.js'); | ||
// Not the same `utils.js`! | ||
const { hasError } = require('../utils.js'); | ||
|
||
// Input: glob (for example: "*.js") | ||
function parseBlockNetworkRequest(parser) { | ||
const ret = validator(parser, { | ||
kind: 'string', | ||
allowEmpty: false, | ||
}); | ||
if (hasError(ret)) { | ||
return ret; | ||
} | ||
const glob = ret.value.value.trim(); | ||
|
||
return { | ||
'instructions': [ | ||
`await page.setRequestInterception(true); | ||
page.on('request', interceptedRequest => { | ||
if (interceptedRequest.isInterceptResolutionHandled()) return; | ||
function matchesGlob(glob, text) { | ||
const wildcard = glob.indexOf("*"); | ||
if (wildcard === -1) { | ||
return glob === text; | ||
} | ||
const prefixGlob = glob.substring(0, wildcard); | ||
const prefixText = text.substring(0, wildcard); | ||
if (prefixGlob !== prefixText) { | ||
return false; | ||
} | ||
const suffixGlob = glob.substring(wildcard + 1); | ||
let suffixText = text.substring(wildcard); | ||
if (suffixGlob.indexOf("*") === -1) { | ||
return suffixText.endsWith(suffixGlob); | ||
} | ||
let matched = matchesGlob(suffixGlob, suffixText); | ||
while (suffixText !== "" && !matched) { | ||
suffixText = suffixText.substring(1); | ||
matched = matchesGlob(suffixGlob, suffixText); | ||
} | ||
return matched; | ||
} | ||
if (matchesGlob("${glob}", interceptedRequest.url())) { | ||
interceptedRequest.abort(); | ||
} else { | ||
interceptedRequest.continue({}, 0); | ||
} | ||
});`, | ||
], | ||
}; | ||
} | ||
|
||
module.exports = { | ||
'parseBlockNetworkRequest': parseBlockNetworkRequest, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `1.1` (a number)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `1` (a number)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected `\"` at the end of the string""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected `\"` at the end of the string""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `(\"a\", 2)` (a tuple)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `()` (a tuple)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `(\"x\")` (a tuple)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
error = """expected a string, found `(a, \"b\")` (a tuple)""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
instructions = [ | ||
"""await page.setRequestInterception(true); | ||
page.on('request', interceptedRequest => { | ||
if (interceptedRequest.isInterceptResolutionHandled()) return; | ||
function matchesGlob(glob, text) { | ||
const wildcard = glob.indexOf(\"*\"); | ||
if (wildcard === -1) { | ||
return glob === text; | ||
} | ||
const prefixGlob = glob.substring(0, wildcard); | ||
const prefixText = text.substring(0, wildcard); | ||
if (prefixGlob !== prefixText) { | ||
return false; | ||
} | ||
const suffixGlob = glob.substring(wildcard + 1); | ||
let suffixText = text.substring(wildcard); | ||
if (suffixGlob.indexOf(\"*\") === -1) { | ||
return suffixText.endsWith(suffixGlob); | ||
} | ||
let matched = matchesGlob(suffixGlob, suffixText); | ||
while (suffixText !== \"\" && !matched) { | ||
suffixText = suffixText.substring(1); | ||
matched = matchesGlob(suffixGlob, suffixText); | ||
} | ||
return matched; | ||
} | ||
if (matchesGlob(\"x\", interceptedRequest.url())) { | ||
interceptedRequest.abort(); | ||
} else { | ||
interceptedRequest.continue({}, 0); | ||
} | ||
});""", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
<script src="external-script.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
document.write("<div id=output></div>"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// This test is meant to ensure that we can block an external script request. | ||
// Since we are intentionally causing request errors, don't block them. | ||
fail-on-request-error: false | ||
go-to: "file://" + |CURRENT_DIR| + "/" + |DOC_PATH| + "/external-script.html" | ||
assert: "#output" | ||
// This does not match, so it won't cause a fail. | ||
block-network-request: "*s6ojwNhzwbj9F2cYwA9tqjtnFyUzt6YhQzfviRjB*.js" | ||
reload: | ||
assert: "#output" | ||
// This matches, so the script won't load | ||
block-network-request: "*extern*.js" | ||
reload: | ||
assert-false: "#output" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=> Starting doc-ui tests... | ||
|
||
block-network-request-multiple-star... OK | ||
|
||
<= doc-ui tests done: 1 succeeded, 0 failed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// This test is meant to ensure that we can block an external script request. | ||
// Since we are intentionally causing request errors, don't block them. | ||
fail-on-request-error: false | ||
go-to: "file://" + |CURRENT_DIR| + "/" + |DOC_PATH| + "/external-script.html" | ||
assert: "#output" | ||
// This does not match, so it won't cause a fail. | ||
block-network-request: "*s6ojwNhzwbj9F2cYwA9tqjtnFyUzt6YhQzfviRjB.js" | ||
reload: | ||
assert: "#output" | ||
// This matches, so the script won't load | ||
block-network-request: "*.js" | ||
reload: | ||
assert-false: "#output" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=> Starting doc-ui tests... | ||
|
||
block-network-request... OK | ||
|
||
<= doc-ui tests done: 1 succeeded, 0 failed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters