summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-08-14 01:55:10 +0200
committererdgeist <erdgeist@erdgeist.org>2026-08-14 01:55:10 +0200
commit3d1f82c459e184379f7cc617ffe38b6b63594620 (patch)
tree756f7c2e963feb3268b3e0fb2b6ea58a0019c16a
parent3d4eec32441d37460ff6ca60dc7abf31d5b1bba5 (diff)
Check rendered markup for balance in controller tests
Every successful HTML response is parsed, as a document or a fragment depending on whether it carries a doctype, and parse errors fail the test. New actions inherit the check.
-rw-r--r--test/test_helper.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/test/test_helper.rb b/test/test_helper.rb
index b7fe2b58..ec9b18b3 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -78,4 +78,25 @@ class ActiveSupport::TestCase
78 node.lock_for_editing!(user) 78 node.lock_for_editing!(user)
79 node.draft || node.create_new_draft(user) 79 node.draft || node.create_new_draft(user)
80 end 80 end
81
82 def assert_well_formed
83 body = @response.body
84 doc = if body.lstrip.start_with?("<!DOCTYPE", "<html")
85 Nokogiri::HTML5(body, max_errors: -1)
86 else
87 Nokogiri::HTML5.fragment(body, max_errors: -1)
88 end
89 assert_empty doc.errors,
90 "Malformed markup:\n#{doc.errors.map(&:to_s).join("\n")}"
91 end
92end
93
94class ActionController::TestCase
95 def process(*args, **kwargs)
96 super.tap do
97 if @response&.successful? && @response.media_type == "text/html"
98 assert_well_formed
99 end
100 end
101 end
81end 102end