-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add block-network-request command #600
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `1.1` (a number)""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `1` (a number)""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected `\"` at the end of the string""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected `\"` at the end of the string""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `(\"a\", 2)` (a tuple)""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `()` (a tuple)""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `(\"x\")` (a tuple)""" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| error = """expected a string, found `(a, \"b\")` (a tuple)""" |
| 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); | ||
| } | ||
| });""", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <!DOCTYPE html> | ||
| <script src="external-script.js"></script> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| document.write("<div id=output></div>"); |
| 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" |
| 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 |
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you mention this in the documentation as well please? Could be surprising that a test fails if you block a request in another command.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I've addressed both nits. |
||
| 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" | ||
| 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 |
Uh oh!
There was an error while loading. Please reload this page.