internal/screentest: add request blocking keyword to scripts

Scripts can use the block keyword to set URL patterns to block.
Wildcards ('*') are allowed.

While loading a webpage if chrome attempts to load a resource
from a blocked URL the request will fail. This is useful for
blocking things like dynamic badge images from a code coverage
or build indicator service.

Change-Id: Ifbde82d56918928333836b493e993c1ef1054a76
Reviewed-on: https://go-review.googlesource.com/c/website/+/381335
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Trust: Jamal Carvalho <jamalcarvalho@google.com>
Run-TryBot: Jamal Carvalho <jamalcarvalho@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/internal/screentest/screentest.go b/internal/screentest/screentest.go
index 2d7f5fd..24a339c 100644
--- a/internal/screentest/screentest.go
+++ b/internal/screentest/screentest.go
@@ -42,6 +42,10 @@
 //
 //  compare https://go.dev::cache http://localhost:6060
 //
+// Use block URL ... to set URL patterns to block. Wildcards ('*') are allowed.
+//
+//  block https://codecov.io/* https://travis-ci.com/*
+//
 // Use output DIRECTORY to set the output directory for diffs and cached images.
 //
 //  output testdata/snapshots
@@ -385,6 +389,7 @@
 	viewportHeight            int
 	screenshotType            screenshotType
 	screenshotElement         string
+	blockedURLs               []string
 	output                    bytes.Buffer
 }
 
@@ -412,6 +417,7 @@
 		gcsBucket          bool
 		width, height      int
 		lineNo             int
+		blockedURLs        []string
 	)
 	cache, err := os.UserCacheDir()
 	if err != nil {
@@ -506,6 +512,8 @@
 			tasks = append(tasks, chromedp.WaitReady(args))
 		case "EVAL":
 			tasks = append(tasks, chromedp.Evaluate(args, nil))
+		case "BLOCK":
+			blockedURLs = append(blockedURLs, strings.Fields(args)...)
 		case "CAPTURE":
 			if originA == "" || originB == "" {
 				return nil, fmt.Errorf("missing compare for capture on line %d", lineNo)
@@ -522,11 +530,12 @@
 				return nil, fmt.Errorf("url.Parse(%q): %w", originB+pathname, err)
 			}
 			test := &testcase{
-				name:    testName,
-				tasks:   tasks,
-				urlA:    urlA.String(),
-				urlB:    urlB.String(),
-				headers: headers,
+				name:        testName,
+				tasks:       tasks,
+				urlA:        urlA.String(),
+				urlB:        urlB.String(),
+				headers:     headers,
+				blockedURLs: blockedURLs,
 				// Default to viewportScreenshot
 				screenshotType: viewportScreenshot,
 				viewportWidth:  width,
@@ -739,6 +748,9 @@
 	if tc.headers != nil {
 		tasks = append(tasks, network.SetExtraHTTPHeaders(tc.headers))
 	}
+	if tc.blockedURLs != nil {
+		tasks = append(tasks, network.SetBlockedURLS(tc.blockedURLs))
+	}
 	tasks = append(tasks,
 		chromedp.EmulateViewport(int64(tc.viewportWidth), int64(tc.viewportHeight)),
 		chromedp.Navigate(url),