Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(audit): mark a 4xx/5xx main document as a failed page via its HTTP status, not a broken asset (PIN-414)

Luigi Agosti14d1a50c3cce
diff --git a/internal/audit/run.go b/internal/audit/run.go
index 6f9c0f85..9c92fe12 100644
--- a/internal/audit/run.go
+++ b/internal/audit/run.go
@@ -3,6 +3,7 @@ package audit
import (
"errors"
"fmt"
+ "strings"
"sync"
"time"
)
@@ -176,7 +177,7 @@ func RunAudit(input AuditInput, seaportalPages []SeaportalPage, opts RunOptions,
// ToPageResult converts a single-page audit into the report page shape.
func (pa PageAudit) ToPageResult() PageResult {
- return PageResult{
+ pr := PageResult{
URL: pa.URL,
Title: pa.Title,
Error: pa.Error,
@@ -184,6 +185,56 @@ func (pa PageAudit) ToPageResult() PageResult {
SecurityFindings: pa.SecurityFindings,
Browser: pa.BrowserPageData,
}
+ // The page's own HTTP status is a first-class field, read from the main
+ // document's network request. When that document failed, it IS the page —
+ // not a sub-resource — so it is removed from BrokenAssets, which counts only
+ // failed sub-resources (a 404 image, script, or nested iframe still counts).
+ if status, docURL, ok := mainDocumentStatus(pr.Browser, pr.URL); ok {
+ pr.StatusCode = status
+ pr.Browser.BrokenAssets = withoutAsset(pr.Browser.BrokenAssets, "document", docURL)
+ }
+ return pr
+}
+
+// mainDocumentStatus reports the HTTP status and URL of the page's own document,
+// read from the observed network requests: the request for the audited URL
+// itself, or failing that the first document request (the main-frame navigation,
+// for the redirect case where no request carries the exact audited URL). ok is
+// false when the network collector did not run, so StatusCode stays unset rather
+// than a misleading zero.
+func mainDocumentStatus(browser BrowserPageData, pageURL string) (status int, url string, ok bool) {
+ var first *NetworkRequest
+ for i := range browser.NetworkRequests {
+ req := &browser.NetworkRequests[i]
+ if !strings.EqualFold(req.ResourceType, "document") {
+ continue
+ }
+ if first == nil {
+ first = req
+ }
+ if req.URL == pageURL {
+ return req.Status, req.URL, true
+ }
+ }
+ if first != nil {
+ return first.Status, first.URL, true
+ }
+ return 0, "", false
+}
+
+// withoutAsset returns the broken assets with the one that is the main document
+// itself removed (matched on the lowercased resourceType and the document URL),
+// so the page failure is not double-counted as a broken sub-resource. A
+// same-typed asset at another URL — a 404 iframe document — is kept.
+func withoutAsset(assets []BrokenAsset, resourceType, url string) []BrokenAsset {
+ out := make([]BrokenAsset, 0, len(assets))
+ for _, a := range assets {
+ if strings.EqualFold(a.ResourceType, resourceType) && a.URL == url {
+ continue
+ }
+ out = append(out, a)
+ }
+ return out
}
// seaportalOnlyResult is the report entry for a page seaportal marked as not
@@ -214,13 +265,18 @@ func mergeSeaportal(pr PageResult, sp *SeaportalPage) PageResult {
}
// PageStatus is the one-line health verdict every report surface prints for
-// a page: the audit failure when it could not be collected, otherwise the
-// uncaught-exception count, otherwise ok. An uncaught exception halts the
-// script that raised it, so a page carrying one is never ok.
+// a page: the audit failure when it could not be collected, otherwise the page's
+// own 4xx/5xx HTTP status, otherwise the uncaught-exception count, otherwise ok.
+// A page whose own document is an error response is a failed page, not an "ok"
+// page with a broken asset. An uncaught exception halts the script that raised
+// it, so a page carrying one is never ok either. (3xx and an unset status are
+// not failures.)
func PageStatus(p PageResult) string {
switch {
case p.Error != "":
return "error: " + p.Error
+ case p.StatusCode >= 400:
+ return fmt.Sprintf("HTTP %d", p.StatusCode)
case len(p.Browser.JSErrors) > 0:
return fmt.Sprintf("%d uncaught JS error(s)", len(p.Browser.JSErrors))
default:
diff --git a/internal/audit/run_test.go b/internal/audit/run_test.go
index 88e0f9d0..4da52955 100644
--- a/internal/audit/run_test.go
+++ b/internal/audit/run_test.go
@@ -230,3 +230,58 @@ func TestPageStatus(t *testing.T) {
})
}
}
+
+// ToPageResult populates the page's own StatusCode from the main document and
+// removes that document from BrokenAssets — but a NESTED document (an iframe)
+// that 404s is a real sub-resource and stays a broken asset.
+func TestToPageResultDropsMainDocumentButKeepsAnIframeDocument(t *testing.T) {
+ const page = "http://x/page.html"
+ const iframe = "http://x/frame.html"
+ pa := PageAudit{
+ URL: page,
+ BrowserPageData: BrowserPageData{
+ NetworkRequests: []NetworkRequest{
+ {URL: page, ResourceType: "Document", Status: 404, Failed: true},
+ {URL: iframe, ResourceType: "Document", Status: 404, Failed: true},
+ },
+ BrokenAssets: []BrokenAsset{
+ {URL: page, ResourceType: "document", Status: 404},
+ {URL: iframe, ResourceType: "document", Status: 404},
+ },
+ },
+ }
+
+ pr := pa.ToPageResult()
+ if pr.StatusCode != 404 {
+ t.Errorf("StatusCode = %d, want 404 (the main document)", pr.StatusCode)
+ }
+ if len(pr.Browser.BrokenAssets) != 1 || pr.Browser.BrokenAssets[0].URL != iframe {
+ t.Errorf("BrokenAssets = %+v, want only the iframe document kept", pr.Browser.BrokenAssets)
+ }
+ if got := PageStatus(pr); got != "HTTP 404" {
+ t.Errorf("PageStatus = %q, want HTTP 404", got)
+ }
+}
+
+// A 200 main document leaves the page ok and its status set, and does not touch
+// sub-resource broken assets.
+func TestToPageResultKeepsA200PageOk(t *testing.T) {
+ const page = "http://x/ok.html"
+ pa := PageAudit{
+ URL: page,
+ BrowserPageData: BrowserPageData{
+ NetworkRequests: []NetworkRequest{{URL: page, ResourceType: "Document", Status: 200}},
+ BrokenAssets: []BrokenAsset{{URL: page + "?img", ResourceType: "image", Status: 404}},
+ },
+ }
+ pr := pa.ToPageResult()
+ if pr.StatusCode != 200 {
+ t.Errorf("StatusCode = %d, want 200", pr.StatusCode)
+ }
+ if got := PageStatus(pr); got != "ok" {
+ t.Errorf("PageStatus = %q, want ok", got)
+ }
+ if len(pr.Browser.BrokenAssets) != 1 {
+ t.Errorf("a 200 page's sub-resource broken asset was dropped: %+v", pr.Browser.BrokenAssets)
+ }
+}
diff --git a/internal/cli/actions/actions_audit.go b/internal/cli/actions/actions_audit.go
index 739eebb6..97b8d538 100644
--- a/internal/cli/actions/actions_audit.go
+++ b/internal/cli/actions/actions_audit.go
@@ -505,7 +505,11 @@ func printAuditSummary(report map[string]any) {
func auditSummaryLines(report audit.AuditReport) []string {
failed, broken, jsErrors := 0, 0, 0
for _, p := range report.Pages {
- if p.Error != "" {
+ // A page failed when the audit could not collect it (transport Error) OR
+ // when its own document returned 4xx/5xx — the latter has no Error, so it
+ // used to read as ok. The main-document failure is not in BrokenAssets
+ // (dropped in ToPageResult), so broken counts only failed sub-resources.
+ if p.Error != "" || p.StatusCode >= 400 {
failed++
}
broken += len(p.Browser.BrokenAssets)
diff --git a/internal/cli/actions/actions_audit_test.go b/internal/cli/actions/actions_audit_test.go
index 823d6d45..781e50c9 100644
--- a/internal/cli/actions/actions_audit_test.go
+++ b/internal/cli/actions/actions_audit_test.go
@@ -315,3 +315,79 @@ func TestAuditSummaryLines(t *testing.T) {
t.Errorf("headline still calls the accessibility mean a summary score: %q", lines[0])
}
}
+
+// A page whose own document is 4xx/5xx used to read `· ok` with 0 failed pages
+// (only p.Error was counted) while the document doubled as a broken asset. This
+// audits a 404 URL, a plain 200 URL, and a 200 URL with a broken sub-resource
+// through the real audit path and asserts the summary now distinguishes them.
+func TestAuditSummaryDistinguishesA4xxDocumentFromA200(t *testing.T) {
+ const url404 = "http://x/missing404.html"
+ const url200 = "http://x/ok.html"
+ const urlBrokenImg = "http://x/broken-image.html"
+
+ auditor := func(u string, _ audit.PageOptions) audit.PageAudit {
+ pa := audit.PageAudit{URL: u}
+ switch u {
+ case url404:
+ pa.BrowserPageData = audit.BrowserPageData{
+ NetworkRequests: []audit.NetworkRequest{{URL: u, ResourceType: "Document", Status: 404, Failed: true}},
+ // The document itself is captured as a broken asset today; the fix
+ // must drop it so the page failure is not also counted as broken.
+ BrokenAssets: []audit.BrokenAsset{{URL: u, ResourceType: "document", Status: 404}},
+ }
+ case urlBrokenImg:
+ img := u + "?img"
+ pa.BrowserPageData = audit.BrowserPageData{
+ NetworkRequests: []audit.NetworkRequest{
+ {URL: u, ResourceType: "Document", Status: 200},
+ {URL: img, ResourceType: "Image", Status: 404, Failed: true},
+ },
+ BrokenAssets: []audit.BrokenAsset{{URL: img, ResourceType: "image", Status: 404}},
+ }
+ default:
+ pa.BrowserPageData = audit.BrowserPageData{
+ NetworkRequests: []audit.NetworkRequest{{URL: u, ResourceType: "Document", Status: 200}},
+ }
+ }
+ return pa
+ }
+
+ input := audit.AuditInput{URLs: []string{url404, url200, urlBrokenImg}}
+ report, err := audit.RunAudit(input, nil, audit.RunOptions{Page: audit.PageOptions{Network: true}}, nil, auditor)
+ if err != nil {
+ t.Fatalf("RunAudit: %v", err)
+ }
+
+ lines := auditSummaryLines(report)
+ // One failed page (the 404), one broken asset (the image on the 200 page —
+ // the 404 document must NOT be counted as broken).
+ if !strings.Contains(lines[0], "1 failed page(s)") {
+ t.Errorf("headline failed count wrong: %q", lines[0])
+ }
+ if !strings.Contains(lines[0], "1 broken asset(s)") {
+ t.Errorf("headline broken count wrong (document double-counted?): %q", lines[0])
+ }
+
+ status := map[string]audit.PageResult{}
+ for _, p := range report.Pages {
+ status[p.URL] = p
+ }
+ if got := audit.PageStatus(status[url404]); got != "HTTP 404" {
+ t.Errorf("404 page status = %q, want HTTP 404", got)
+ }
+ if got := status[url404].StatusCode; got != 404 {
+ t.Errorf("404 page StatusCode = %d, want 404", got)
+ }
+ if n := len(status[url404].Browser.BrokenAssets); n != 0 {
+ t.Errorf("404 page still lists its own document as a broken asset: %+v", status[url404].Browser.BrokenAssets)
+ }
+ if got := audit.PageStatus(status[url200]); got != "ok" {
+ t.Errorf("200 page status = %q, want ok", got)
+ }
+ if got := audit.PageStatus(status[urlBrokenImg]); got != "ok" {
+ t.Errorf("200 page with a broken image status = %q, want ok (a sub-resource 404 is not a page failure)", got)
+ }
+ if n := len(status[urlBrokenImg].Browser.BrokenAssets); n != 1 {
+ t.Errorf("the 200 page's broken image was dropped: %+v", status[urlBrokenImg].Browser.BrokenAssets)
+ }
+}
diff --git a/tests/e2e/fixtures/audit-site/golden-report.json b/tests/e2e/fixtures/audit-site/golden-report.json
index 44026d8e..ef64b802 100644
--- a/tests/e2e/fixtures/audit-site/golden-report.json
+++ b/tests/e2e/fixtures/audit-site/golden-report.json
@@ -73,6 +73,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Audit Fixture Site",
"url": "http://fixtures/audit-site/index.html"
},
@@ -146,6 +147,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Broken Assets — Audit Fixture Site",
"url": "http://fixtures/audit-site/broken-assets.html"
},
@@ -185,6 +187,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Console Errors — Audit Fixture Site",
"url": "http://fixtures/audit-site/console-errors.html"
},
@@ -226,6 +229,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Accessibility Issues — Audit Fixture Site",
"url": "http://fixtures/audit-site/a11y-issues.html"
},
@@ -260,6 +264,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Clean Page — Audit Fixture Site",
"url": "http://fixtures/audit-site/clean.html"
},
@@ -306,6 +311,7 @@
"url": "http://fixtures/audit-site/forms.html"
}
],
+ "statusCode": 200,
"title": "Forms — Audit Fixture Site",
"url": "http://fixtures/audit-site/forms.html"
},
@@ -337,6 +343,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Cookie Echo — Audit Fixture Site",
"url": "http://fixtures/audit-site/cookie-echo.html"
},
@@ -363,6 +370,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Product 1 — Audit Fixture Site",
"url": "http://fixtures/audit-site/products/p1.html"
},
@@ -389,6 +397,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Product 2 — Audit Fixture Site",
"url": "http://fixtures/audit-site/products/p2.html"
},
@@ -415,6 +424,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Product 3 — Audit Fixture Site",
"url": "http://fixtures/audit-site/products/p3.html"
},
@@ -441,6 +451,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Product 4 — Audit Fixture Site",
"url": "http://fixtures/audit-site/products/p4.html"
},
@@ -467,6 +478,7 @@
"screenshotPath": "",
"timingMetrics": {}
},
+ "statusCode": 200,
"title": "Product 5 — Audit Fixture Site",
"url": "http://fixtures/audit-site/products/p5.html"
}