@@ -552,4 +552,63 @@ public void testRegionMatchesAsciiHandlesOutOfBounds() { |
552 | 552 | assertFalse(AsciiString.regionMatchesAscii(str, false, -1, hello, 0, 5)); |
553 | 553 | assertFalse(AsciiString.regionMatchesAscii(str, false, 0, hello, -1, 5)); |
554 | 554 | } |
| 555 | + |
| 556 | + @Test |
| 557 | + public void testCachedWithAsciiString() { |
| 558 | + // Pure ASCII strings should work unchanged |
| 559 | + String ascii = "hello"; |
| 560 | + AsciiString cached = AsciiString.cached(ascii); |
| 561 | + assertEquals(ascii, cached.toString()); |
| 562 | + // String is re-derived from byte array during sanitization, so identity may differ |
| 563 | + assertEquals(ascii.length(), cached.length()); |
| 564 | + assertTrue(cached.contentEquals(ascii)); |
| 565 | + } |
| 566 | + |
| 567 | + @Test |
| 568 | + public void testCachedWithAsciiLatin1String() { |
| 569 | + // Latin-1 strings (chars 128-255) should work unchanged |
| 570 | + String latin1 = "h" + (char) 233 + "llo"; // héllo |
| 571 | + AsciiString cached = AsciiString.cached(latin1); |
| 572 | + assertEquals(latin1, cached.toString()); |
| 573 | + } |
| 574 | + |
| 575 | + @Test |
| 576 | + public void testCachedSanitizesNonLatin1String() { |
| 577 | + // Chars > 255 should be sanitized to '?' in the cached string to match the byte content |
| 578 | + String nonLatin1 = "test" + (char) 0x1234 + "ing"; |
| 579 | + AsciiString cached = AsciiString.cached(nonLatin1); |
| 580 | + // The char 0x1234 gets converted to '?' by c2b, so toString should reflect that |
| 581 | + assertEquals("test?ing", cached.toString()); |
| 582 | + } |
| 583 | + |
| 584 | + @Test |
| 585 | + public void testCachedEmptyString() { |
| 586 | + AsciiString cached = AsciiString.cached(""); |
| 587 | + assertEquals("", cached.toString()); |
| 588 | + assertTrue(cached.isEmpty()); |
| 589 | + } |
| 590 | + |
| 591 | + @Test |
| 592 | + public void testCachedStringMatchesByteContent() { |
| 593 | + // The cached string should always match the byte content round-trip |
| 594 | + String nonLatin1 = "a" + (char) 0x4321 + "b"; |
| 595 | + AsciiString cached = AsciiString.cached(nonLatin1); |
| 596 | + // Manually compute the expected sanitized string from the byte array |
| 597 | + StringBuilder expected = new StringBuilder(); |
| 598 | + for (byte b : cached.toByteArray()) { |
| 599 | + expected.append((char) (b & 0xFF)); |
| 600 | + } |
| 601 | + assertEquals(expected.toString(), cached.toString()); |
| 602 | + } |
| 603 | + |
| 604 | + @Test |
| 605 | + public void testCachedWithAllAsciiConstants() { |
| 606 | + // Constants used in the codebase should be unaffected |
| 607 | + AsciiString host = AsciiString.cached("host"); |
| 608 | + assertEquals("host", host.toString()); |
| 609 | + AsciiString method = AsciiString.cached(":method"); |
| 610 | + assertEquals(":method", method.toString()); |
| 611 | + AsciiString status = AsciiString.cached(":status"); |
| 612 | + assertEquals(":status", status.toString()); |
| 613 | + } |
555 | 614 | } |