|
21 | 21 |
|
22 | 22 | import static java.util.Arrays.asList; |
23 | 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | +import static org.hamcrest.Matchers.allOf; |
24 | 25 | import static org.hamcrest.Matchers.containsString; |
25 | 26 | import static org.hamcrest.Matchers.empty; |
26 | 27 | import static org.hamcrest.Matchers.equalTo; |
@@ -72,6 +73,7 @@ class HttpMessageUnitTest { |
72 | 73 | @AfterEach |
73 | 74 | void cleanUp() throws Exception { |
74 | 75 | HttpMessage.setContentEncodingsHandler(null); |
| 76 | + HttpMessage.resetWarnedContentTypeValues(); |
75 | 77 | Configurator.reconfigure(getClass().getResource("/log4j2-test.properties").toURI()); |
76 | 78 | } |
77 | 79 |
|
@@ -624,6 +626,61 @@ void shouldUseDefaultAndWarnOnUnknownCharsetWhenSettingRequestBody(String charse |
624 | 626 | containsString("Failed to set charset")); |
625 | 627 | } |
626 | 628 |
|
| 629 | + @Test |
| 630 | + void shouldWarnOnceOnSameUnknownCharsetWhenSettingRequestBody() throws Exception { |
| 631 | + // Given |
| 632 | + HttpMessage message = new HttpMessage(); |
| 633 | + message.setRequestHeader( |
| 634 | + new HttpRequestHeader( |
| 635 | + "GET / HTTP/1.1\r\nContent-Type: text/plain; charset=1st_unknown")); |
| 636 | + withLoggerAppender(); |
| 637 | + // When |
| 638 | + message.setRequestBody("Body"); |
| 639 | + message.setRequestBody("Body"); |
| 640 | + message.setRequestHeader( |
| 641 | + new HttpRequestHeader( |
| 642 | + "GET / HTTP/1.1\r\nContent-Type: text/plain; charset=2nd_unknown")); |
| 643 | + message.setRequestBody("Body"); |
| 644 | + message.setRequestBody("Body"); |
| 645 | + // Then |
| 646 | + assertThat(testAppender.getLogEvents(), hasSize(2)); |
| 647 | + assertThat( |
| 648 | + testAppender.getLogEvents().get(0).getMessage(), |
| 649 | + allOf( |
| 650 | + containsString("Failed to set charset"), |
| 651 | + containsString("charset=1st_unknown"))); |
| 652 | + assertThat( |
| 653 | + testAppender.getLogEvents().get(1).getMessage(), |
| 654 | + allOf( |
| 655 | + containsString("Failed to set charset"), |
| 656 | + containsString("charset=2nd_unknown"))); |
| 657 | + } |
| 658 | + |
| 659 | + @Test |
| 660 | + void shouldWarnOnceAgainOnSameUnknownCharsetWhenSettingRequestBodyAfterResettingWarns() |
| 661 | + throws Exception { |
| 662 | + // Given |
| 663 | + HttpMessage message = new HttpMessage(); |
| 664 | + message.setRequestHeader( |
| 665 | + new HttpRequestHeader( |
| 666 | + "GET / HTTP/1.1\r\nContent-Type: text/plain; charset=unknown")); |
| 667 | + withLoggerAppender(); |
| 668 | + // When |
| 669 | + message.setRequestBody("Body"); |
| 670 | + message.setRequestBody("Body"); |
| 671 | + HttpMessage.resetWarnedContentTypeValues(); |
| 672 | + message.setRequestBody("Body"); |
| 673 | + message.setRequestBody("Body"); |
| 674 | + // Then |
| 675 | + assertThat(testAppender.getLogEvents(), hasSize(2)); |
| 676 | + assertThat( |
| 677 | + testAppender.getLogEvents().get(0).getMessage(), |
| 678 | + allOf(containsString("Failed to set charset"), containsString("charset=unknown"))); |
| 679 | + assertThat( |
| 680 | + testAppender.getLogEvents().get(1).getMessage(), |
| 681 | + allOf(containsString("Failed to set charset"), containsString("charset=unknown"))); |
| 682 | + } |
| 683 | + |
627 | 684 | static Stream<Arguments> setBodyData() { |
628 | 685 | String iso8851 = StandardCharsets.ISO_8859_1.name(); |
629 | 686 | String utf8 = StandardCharsets.UTF_8.name(); |
@@ -681,6 +738,61 @@ void shouldUseDefaultAndWarnOnUnknownCharsetWhenSettingResponseBody(String chars |
681 | 738 | containsString("Failed to set charset")); |
682 | 739 | } |
683 | 740 |
|
| 741 | + @Test |
| 742 | + void shouldWarnOnceOnSameUnknownCharsetWhenSettingResponseBody() throws Exception { |
| 743 | + // Given |
| 744 | + HttpMessage message = new HttpMessage(); |
| 745 | + message.setResponseHeader( |
| 746 | + new HttpResponseHeader( |
| 747 | + "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=1st_unknown")); |
| 748 | + withLoggerAppender(); |
| 749 | + // When |
| 750 | + message.setResponseBody("Body"); |
| 751 | + message.setResponseBody("Body"); |
| 752 | + message.setResponseHeader( |
| 753 | + new HttpResponseHeader( |
| 754 | + "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=2nd_unknown")); |
| 755 | + message.setResponseBody("Body"); |
| 756 | + message.setResponseBody("Body"); |
| 757 | + // Then |
| 758 | + assertThat(testAppender.getLogEvents(), hasSize(2)); |
| 759 | + assertThat( |
| 760 | + testAppender.getLogEvents().get(0).getMessage(), |
| 761 | + allOf( |
| 762 | + containsString("Failed to set charset"), |
| 763 | + containsString("charset=1st_unknown"))); |
| 764 | + assertThat( |
| 765 | + testAppender.getLogEvents().get(1).getMessage(), |
| 766 | + allOf( |
| 767 | + containsString("Failed to set charset"), |
| 768 | + containsString("charset=2nd_unknown"))); |
| 769 | + } |
| 770 | + |
| 771 | + @Test |
| 772 | + void shouldWarnOnceAgainOnSameUnknownCharsetWhenSettingResponseBodyAfterResettingWarns() |
| 773 | + throws Exception { |
| 774 | + // Given |
| 775 | + HttpMessage message = new HttpMessage(); |
| 776 | + message.setResponseHeader( |
| 777 | + new HttpResponseHeader( |
| 778 | + "HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=unknown")); |
| 779 | + withLoggerAppender(); |
| 780 | + // When |
| 781 | + message.setResponseBody("Body"); |
| 782 | + message.setResponseBody("Body"); |
| 783 | + HttpMessage.resetWarnedContentTypeValues(); |
| 784 | + message.setResponseBody("Body"); |
| 785 | + message.setResponseBody("Body"); |
| 786 | + // Then |
| 787 | + assertThat(testAppender.getLogEvents(), hasSize(2)); |
| 788 | + assertThat( |
| 789 | + testAppender.getLogEvents().get(0).getMessage(), |
| 790 | + allOf(containsString("Failed to set charset"), containsString("charset=unknown"))); |
| 791 | + assertThat( |
| 792 | + testAppender.getLogEvents().get(1).getMessage(), |
| 793 | + allOf(containsString("Failed to set charset"), containsString("charset=unknown"))); |
| 794 | + } |
| 795 | + |
684 | 796 | private static HttpMessage newHttpMessage() throws Exception { |
685 | 797 | HttpMessage message = |
686 | 798 | new HttpMessage( |
|
0 commit comments