From 968242a49f15c3bb3f31439fdc469e66d701ab9a Mon Sep 17 00:00:00 2001 From: cheese8 Date: Thu, 23 Jun 2022 19:30:40 +0800 Subject: [PATCH] solve the chaos code problem on rejectTips in 2020.0 (#283) * solve the chaos code problem on rejectTips * revert on importOrder * align to use text/html resolve the chaos problem * Update QuotaCheckServletFilterTest.java --- CHANGELOG.md | 1 + .../filter/QuotaCheckReactiveFilter.java | 4 +-- .../filter/QuotaCheckServletFilter.java | 3 +- .../filter/QuotaCheckReactiveFilterTest.java | 6 ++-- .../filter/QuotaCheckServletFilterTest.java | 28 +++++++++++++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f651b6997..003dc4974 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - [feat:support reading configuration from application.yml or application.properties.](https://github.com/Tencent/spring-cloud-tencent/pull/261) - [change the way from escape to encode in 2020.0](https://github.com/Tencent/spring-cloud-tencent/pull/257) - [fix:fix ClassNotFoundException while not importing openfeign when using circuit-breaker module.](https://github.com/Tencent/spring-cloud-tencent/pull/270) +- [fix:solve the chaos code problem on rejectTips](https://github.com/Tencent/spring-cloud-tencent/pull/283) diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java index 7340c73de..8796fc1ab 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilter.java @@ -55,7 +55,7 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB /** * Reactive filter to check quota. * - * @author Haotian Zhang, lepdou + * @author Haotian Zhang, lepdou, cheese8 */ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { @@ -106,7 +106,7 @@ public class QuotaCheckReactiveFilter implements WebFilter, Ordered { if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) { ServerHttpResponse response = exchange.getResponse(); response.setRawStatusCode(polarisRateLimitProperties.getRejectHttpCode()); - response.getHeaders().setContentType(MediaType.APPLICATION_JSON); + response.getHeaders().setContentType(MediaType.TEXT_HTML); DataBuffer dataBuffer = response.bufferFactory().allocateBuffer() .write(rejectTips.getBytes(StandardCharsets.UTF_8)); return response.writeWith(Mono.just(dataBuffer)); diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java index 08767b721..f3e4c3b07 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/main/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilter.java @@ -53,7 +53,7 @@ import static com.tencent.cloud.polaris.ratelimit.constant.RateLimitConstant.LAB /** * Servlet filter to check quota. * - * @author Haotian Zhang, lepdou + * @author Haotian Zhang, lepdou, cheese8 */ @Order(RateLimitConstant.FILTER_ORDER) public class QuotaCheckServletFilter extends OncePerRequestFilter { @@ -99,6 +99,7 @@ public class QuotaCheckServletFilter extends OncePerRequestFilter { if (quotaResponse.getCode() == QuotaResultCode.QuotaResultLimited) { response.setStatus(polarisRateLimitProperties.getRejectHttpCode()); + response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(rejectTips); return; } diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java index 3bae2e3bd..0a9a4988f 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckReactiveFilterTest.java @@ -65,7 +65,7 @@ import static org.mockito.Mockito.when; /** * Test for {@link QuotaCheckReactiveFilter}. * - * @author Haotian Zhang + * @author Haotian Zhang, cheese8 */ @RunWith(MockitoJUnitRunner.class) @SpringBootTest(classes = QuotaCheckReactiveFilterTest.TestApplication.class, properties = { @@ -118,7 +118,7 @@ public class QuotaCheckReactiveFilterTest { }); PolarisRateLimitProperties polarisRateLimitProperties = new PolarisRateLimitProperties(); - polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips"); + polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips提示消息"); polarisRateLimitProperties.setRejectHttpCode(419); RateLimitRuleLabelResolver rateLimitRuleLabelResolver = mock(RateLimitRuleLabelResolver.class); @@ -138,7 +138,7 @@ public class QuotaCheckReactiveFilterTest { try { Field rejectTips = QuotaCheckReactiveFilter.class.getDeclaredField("rejectTips"); rejectTips.setAccessible(true); - assertThat(rejectTips.get(quotaCheckReactiveFilter)).isEqualTo("RejectRequestTips"); + assertThat(rejectTips.get(quotaCheckReactiveFilter)).isEqualTo("RejectRequestTips提示消息"); } catch (NoSuchFieldException | IllegalAccessException e) { fail("Exception encountered.", e); diff --git a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilterTest.java b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilterTest.java index a2cc344dc..3f93f0a3b 100644 --- a/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-ratelimit/src/test/java/com/tencent/cloud/polaris/ratelimit/filter/QuotaCheckServletFilterTest.java @@ -65,7 +65,7 @@ import static org.mockito.Mockito.when; /** * Test for {@link QuotaCheckServletFilter}. * - * @author Haotian Zhang + * @author Haotian Zhang, cheese8 */ @RunWith(MockitoJUnitRunner.class) @SpringBootTest(classes = QuotaCheckServletFilterTest.TestApplication.class, properties = { @@ -77,6 +77,8 @@ public class QuotaCheckServletFilterTest { private QuotaCheckServletFilter quotaCheckServletFilter; + private QuotaCheckServletFilter quotaCheckWithHtmlRejectTipsServletFilter; + private static MockedStatic mockedApplicationContextAwareUtils; private static MockedStatic expressionLabelUtilsMockedStatic; @BeforeClass @@ -87,7 +89,6 @@ public class QuotaCheckServletFilterTest { mockedApplicationContextAwareUtils = Mockito.mockStatic(ApplicationContextAwareUtils.class); mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) .thenReturn("unit-test"); - } @AfterClass @@ -118,13 +119,18 @@ public class QuotaCheckServletFilterTest { }); PolarisRateLimitProperties polarisRateLimitProperties = new PolarisRateLimitProperties(); - polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips"); + polarisRateLimitProperties.setRejectRequestTips("RejectRequestTips提示消息"); polarisRateLimitProperties.setRejectHttpCode(419); + PolarisRateLimitProperties polarisRateLimitWithHtmlRejectTipsProperties = new PolarisRateLimitProperties(); + polarisRateLimitWithHtmlRejectTipsProperties.setRejectRequestTips("

RejectRequestTips提示消息

"); + polarisRateLimitWithHtmlRejectTipsProperties.setRejectHttpCode(419); + RateLimitRuleLabelResolver rateLimitRuleLabelResolver = mock(RateLimitRuleLabelResolver.class); when(rateLimitRuleLabelResolver.getExpressionLabelKeys(anyString(), anyString())).thenReturn(Collections.EMPTY_SET); this.quotaCheckServletFilter = new QuotaCheckServletFilter(limitAPI, labelResolver, polarisRateLimitProperties, rateLimitRuleLabelResolver); + this.quotaCheckWithHtmlRejectTipsServletFilter = new QuotaCheckServletFilter(limitAPI, labelResolver, polarisRateLimitWithHtmlRejectTipsProperties, rateLimitRuleLabelResolver); } @Test @@ -133,7 +139,16 @@ public class QuotaCheckServletFilterTest { try { Field rejectTips = QuotaCheckServletFilter.class.getDeclaredField("rejectTips"); rejectTips.setAccessible(true); - assertThat(rejectTips.get(quotaCheckServletFilter)).isEqualTo("RejectRequestTips"); + assertThat(rejectTips.get(quotaCheckServletFilter)).isEqualTo("RejectRequestTips提示消息"); + } + catch (NoSuchFieldException | IllegalAccessException e) { + fail("Exception encountered.", e); + } + quotaCheckWithHtmlRejectTipsServletFilter.init(); + try { + Field rejectTips = QuotaCheckServletFilter.class.getDeclaredField("rejectTips"); + rejectTips.setAccessible(true); + assertThat(rejectTips.get(quotaCheckWithHtmlRejectTipsServletFilter)).isEqualTo("

RejectRequestTips提示消息

"); } catch (NoSuchFieldException | IllegalAccessException e) { fail("Exception encountered.", e); @@ -201,8 +216,11 @@ public class QuotaCheckServletFilterTest { MetadataContext.LOCAL_SERVICE = "TestApp3"; quotaCheckServletFilter.doFilterInternal(request, response, filterChain); assertThat(response.getStatus()).isEqualTo(419); - assertThat(response.getContentAsString()).isEqualTo("RejectRequestTips"); + assertThat(response.getContentAsString()).isEqualTo("RejectRequestTips提示消息"); + quotaCheckWithHtmlRejectTipsServletFilter.doFilterInternal(request, response, filterChain); + assertThat(response.getStatus()).isEqualTo(419); + assertThat(response.getContentAsString()).isEqualTo("RejectRequestTips提示消息"); // Exception MetadataContext.LOCAL_SERVICE = "TestApp4";