Update src/test/java/stirling/software/SPDF/utils/CustomHtmlSanitizerTest.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Anthony Stirling 2025-05-18 23:55:16 +01:00 committed by GitHub
parent 0af295639d
commit 460d9038a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,62 +8,39 @@ import org.junit.jupiter.api.Test;
class CustomHtmlSanitizerTest { class CustomHtmlSanitizerTest {
@Test @ParameterizedTest
void testSanitizeAllowsValidHtml() { @MethodSource("provideHtmlTestCases")
// Arrange void testSanitizeHtml(String inputHtml, String[] expectedContainedTags) {
String validHtml = "<p>This is <strong>valid</strong> HTML with <em>formatting</em>.</p>";
// Act // Act
String sanitizedHtml = CustomHtmlSanitizer.sanitize(validHtml); String sanitizedHtml = CustomHtmlSanitizer.sanitize(inputHtml);
// Assert // Assert
assertEquals(validHtml, sanitizedHtml); for (String tag : expectedContainedTags) {
assertTrue(sanitizedHtml.contains(tag), tag + " should be preserved");
}
} }
@Test private static Stream<Arguments> provideHtmlTestCases() {
void testSanitizeAllowsFormattingElements() { return Stream.of(
// Arrange - Testing Sanitizers.FORMATTING Arguments.of(
String htmlWithFormatting = "<p>This is <strong>valid</strong> HTML with <em>formatting</em>.</p>",
new String[] {"<p>", "<strong>", "<em>"}
),
Arguments.of(
"<p>Text with <b>bold</b>, <i>italic</i>, <u>underline</u>, " "<p>Text with <b>bold</b>, <i>italic</i>, <u>underline</u>, "
+ "<em>emphasis</em>, <strong>strong</strong>, <strike>strikethrough</strike>, " + "<em>emphasis</em>, <strong>strong</strong>, <strike>strikethrough</strike>, "
+ "<s>strike</s>, <sub>subscript</sub>, <sup>superscript</sup>, " + "<s>strike</s>, <sub>subscript</sub>, <sup>superscript</sup>, "
+ "<tt>teletype</tt>, <code>code</code>, <big>big</big>, <small>small</small>.</p>"; + "<tt>teletype</tt>, <code>code</code>, <big>big</big>, <small>small</small>.</p>",
new String[] {"<b>bold</b>", "<i>italic</i>", "<em>emphasis</em>", "<strong>strong</strong>"}
// Act ),
String sanitizedHtml = CustomHtmlSanitizer.sanitize(htmlWithFormatting); Arguments.of(
// Assert
assertTrue(sanitizedHtml.contains("<b>bold</b>"), "Bold tags should be preserved");
assertTrue(sanitizedHtml.contains("<i>italic</i>"), "Italic tags should be preserved");
assertTrue(
sanitizedHtml.contains("<em>emphasis</em>"), "Emphasis tags should be preserved");
assertTrue(
sanitizedHtml.contains("<strong>strong</strong>"),
"Strong tags should be preserved");
// Note: Some formatting tags might be removed or transformed by the sanitizer,
// so we're checking the most common ones guaranteed to be preserved
}
@Test
void testSanitizeAllowsBlockElements() {
// Arrange - Testing Sanitizers.BLOCKS
String htmlWithBlocks =
"<div>Division</div><h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>" "<div>Division</div><h1>Heading 1</h1><h2>Heading 2</h2><h3>Heading 3</h3>"
+ "<h4>Heading 4</h4><h5>Heading 5</h5><h6>Heading 6</h6>" + "<h4>Heading 4</h4><h5>Heading 5</h5><h6>Heading 6</h6>"
+ "<blockquote>Blockquote</blockquote><ul><li>List item</li></ul>" + "<blockquote>Blockquote</blockquote><ul><li>List item</li></ul>"
+ "<ol><li>Ordered item</li></ol>"; + "<ol><li>Ordered item</li></ol>",
new String[] {"<div>", "<h1>", "<h6>", "<blockquote>", "<ul>", "<ol>", "<li>"}
// Act )
String sanitizedHtml = CustomHtmlSanitizer.sanitize(htmlWithBlocks); );
// Assert
assertTrue(sanitizedHtml.contains("<div>"), "Div tags should be preserved");
assertTrue(sanitizedHtml.contains("<h1>"), "H1 tags should be preserved");
assertTrue(sanitizedHtml.contains("<h6>"), "H6 tags should be preserved");
assertTrue(sanitizedHtml.contains("<blockquote>"), "Blockquote tags should be preserved");
assertTrue(sanitizedHtml.contains("<ul>"), "UL tags should be preserved");
assertTrue(sanitizedHtml.contains("<ol>"), "OL tags should be preserved");
assertTrue(sanitizedHtml.contains("<li>"), "LI tags should be preserved");
} }
@Test @Test