internal/screentest: fix http status check

URL fragments are dropped from request targets. Stripping
the fragment from the URL ensures we capture the response
status.

Change-Id: I66ed67a23878315076762e4f99332613a4f67723
Reviewed-on: https://go-review.googlesource.com/c/website/+/385201
Run-TryBot: Jamal Carvalho <jamal@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Trust: Jamal Carvalho <jamalcarvalho@google.com>
diff --git a/internal/screentest/screentest.go b/internal/screentest/screentest.go
index d7c2d68..b6299e2 100644
--- a/internal/screentest/screentest.go
+++ b/internal/screentest/screentest.go
@@ -792,7 +792,8 @@
 		return nil, fmt.Errorf("chromedp.Run(...): %w", err)
 	}
 	if res.Status != tc.status {
-		return nil, fmt.Errorf("http status mismatch: got %d; want %d", res.Status, tc.status)
+		fmt.Fprintf(&tc.output, "\nFAIL http status mismatch: got %d; want %d", res.Status, tc.status)
+		return nil, fmt.Errorf("bad status: %d", res.Status)
 	}
 	return buf, nil
 }
@@ -889,12 +890,16 @@
 	}
 }
 
-func getResponse(url string, res *Response) chromedp.ActionFunc {
+func getResponse(u string, res *Response) chromedp.ActionFunc {
 	return func(ctx context.Context) error {
 		chromedp.ListenTarget(ctx, func(ev interface{}) {
 			switch e := ev.(type) {
 			case *network.EventResponseReceived:
-				if e.Response.URL == url {
+				// URL fragments are dropped in request targets so we must strip the fragment
+				// from the URL to make a comparison.
+				_u, _ := url.Parse(u)
+				_u.Fragment = ""
+				if e.Response.URL == _u.String() {
 					res.Status = int(e.Response.Status)
 				}
 			}
diff --git a/internal/screentest/testdata/pass.txt b/internal/screentest/testdata/pass.txt
index 4087561..a10ae31 100644
--- a/internal/screentest/testdata/pass.txt
+++ b/internal/screentest/testdata/pass.txt
@@ -7,3 +7,7 @@
 pathname /page-not-found
 status 404
 capture
+
+pathname /page-not-found-with-fragment#fragment
+status 404
+capture