OCT REM: 1. 修正代码检测问题
This commit is contained in:
parent
d4b1d5fb77
commit
9c12891ac1
|
@ -22,12 +22,10 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
@ -173,8 +171,8 @@ public class RequestBodyCacheWrapperTest {
|
|||
@Test
|
||||
@DisplayName("使用getReader()获取请求体内容")
|
||||
public void getReader_ShouldReturnCorrectBody() throws IOException {
|
||||
String requestBody = "{\"key\":\"value\"}";
|
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
||||
String requestBody = "{\"key\":\"value\"}";
|
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
||||
RequestBodyCacheWrapper requestWrapper = new RequestBodyCacheWrapper(mockRequest);
|
||||
|
||||
mockRequest.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
|
||||
|
@ -230,7 +228,7 @@ public class RequestBodyCacheWrapperTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("构造函数读写异常")
|
||||
public void requestBodyCacheWrapper_ReadException() throws IOException, NoSuchFieldException, IllegalAccessException {
|
||||
public void requestBodyCacheWrapper_ReadException() throws IOException {
|
||||
when(httpRequest.getInputStream()).thenThrow(new IOException("null"));
|
||||
|
||||
// 验证 log.info 是否被调用,并检查输出内容
|
||||
|
@ -241,7 +239,7 @@ public class RequestBodyCacheWrapperTest {
|
|||
@Test
|
||||
@DisplayName("获取请求体JSON内容")
|
||||
public void getReader_Json_ShouldReturnCorrectBody() throws IOException {
|
||||
String requestBody = "{\"key\":\"value\"}";
|
||||
String requestBody = "{\"key\":\"value\"}";
|
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest();
|
||||
mockRequest.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
|
||||
// 添加单个请求头
|
||||
|
@ -254,7 +252,7 @@ public class RequestBodyCacheWrapperTest {
|
|||
when(httpRequest.getInputStream()).thenReturn(mockRequest.getInputStream());
|
||||
|
||||
RequestBodyCacheWrapper requestWrapper = new RequestBodyCacheWrapper(mockRequest);
|
||||
BufferedReader reader = requestWrapper.getReader();
|
||||
BufferedReader reader = requestWrapper.getReader();
|
||||
assertNotNull(reader);
|
||||
assertNotNull(requestWrapper.getHeaders());
|
||||
assertEquals(3, requestWrapper.getHeaders().size());
|
||||
|
|
|
@ -59,6 +59,7 @@ public class RequestBodyFilterTest {
|
|||
private ServletResponse servletResponse;
|
||||
@Mock
|
||||
private HttpServletRequest httpServletRequest;
|
||||
|
||||
private MockedStatic<ProtocolJsonUtils> mockProtocolJsonUtils;
|
||||
private MockedStatic<ObjectMapperProvider> mockObjectMapperProvider;
|
||||
private MockedStatic<MessageUtil> mockMessageUtil;
|
||||
|
@ -100,6 +101,7 @@ public class RequestBodyFilterTest {
|
|||
*/
|
||||
@Test
|
||||
@DisplayName("HttpServletRequest时包装请求")
|
||||
@SuppressWarnings("unchecked")
|
||||
public void doFilter_RequestIsHttpServletRequest_WrapsRequest() throws Exception {
|
||||
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
|
||||
DeploymentMiddlewareResp resp = DeploymentMiddlewareResp.builder().deploymentId("3dd6175c-ca47-4640-83e0-5231c037c5ac").build();
|
||||
|
|
|
@ -51,6 +51,7 @@ import static org.mockito.Mockito.when;
|
|||
*/
|
||||
@SpringBootTest
|
||||
@DisplayName("HttpClient 测试类")
|
||||
@SuppressWarnings("unchecked")
|
||||
public class HttpClientUtilsTest {
|
||||
|
||||
private static final String SUCCESS_URL = "https://example.com/success";
|
||||
|
@ -145,7 +146,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("GET 成功")
|
||||
public void get_Success() throws IOException, ParseException {
|
||||
public void get_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
// 调用 get 方法并验证结果
|
||||
String response = HttpClientUtils.get(SUCCESS_URL, null, null);
|
||||
|
@ -177,7 +178,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("GET 带头部成功")
|
||||
public void get_WithHead_Success() throws IOException, ParseException {
|
||||
public void get_WithHead_Success() throws IOException {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Custom-Header", "Value");
|
||||
|
||||
|
@ -188,7 +189,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("GET 带超时成功")
|
||||
public void get_WithTiemout_Success() throws IOException, ParseException {
|
||||
public void get_WithTiemout_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
String response = HttpClientUtils.get(SUCCESS_URL, null, 10);
|
||||
assertEquals(SUCCESS_RESPONSE, response);
|
||||
|
@ -196,7 +197,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("GET 超时为零或负数成功")
|
||||
public void get_WithTiemoutLessOrEqueleZero_Success() throws IOException, ParseException {
|
||||
public void get_WithTiemoutLessOrEqueleZero_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
String response = HttpClientUtils.get(SUCCESS_URL, null, 0);
|
||||
assertEquals(SUCCESS_RESPONSE, response);
|
||||
|
@ -207,7 +208,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 成功")
|
||||
public void post_Success() throws IOException, ParseException {
|
||||
public void post_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
String response = HttpClientUtils.post(SUCCESS_URL, null, null, null);
|
||||
assertEquals(SUCCESS_RESPONSE, response);
|
||||
|
@ -215,7 +216,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 带头部成功")
|
||||
public void post_WithHead_Success() throws IOException, ParseException {
|
||||
public void post_WithHead_Success() throws IOException {
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("Custom-Header", "Value");
|
||||
|
||||
|
@ -226,7 +227,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 带参数成功")
|
||||
public void post_WithParams_Success() throws IOException, ParseException {
|
||||
public void post_WithParams_Success() throws IOException {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("user", "user");
|
||||
params.put("pwd", "pwd");
|
||||
|
@ -238,7 +239,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 带空参数成功")
|
||||
public void post_WithEmptyParams_Success() throws IOException, ParseException {
|
||||
public void post_WithEmptyParams_Success() throws IOException {
|
||||
Map<String, String> params = new HashMap<>(1);
|
||||
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
|
@ -248,7 +249,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 带超时成功")
|
||||
public void post_WithTiemout_Success() throws IOException, ParseException {
|
||||
public void post_WithTiemout_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
String response = HttpClientUtils.post(SUCCESS_URL, null, null, 10);
|
||||
assertEquals(SUCCESS_RESPONSE, response);
|
||||
|
@ -256,7 +257,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST 超时为零或负数成功")
|
||||
public void post_WithTiemoutLessOrEqueleZero_Success() throws IOException, ParseException {
|
||||
public void post_WithTiemoutLessOrEqueleZero_Success() throws IOException {
|
||||
when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenReturn(SUCCESS_RESPONSE);
|
||||
String response = HttpClientUtils.post(SUCCESS_URL, null, null, 0);
|
||||
assertEquals(SUCCESS_RESPONSE, response);
|
||||
|
@ -659,7 +660,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("私有handleResponse调用空Entity")
|
||||
public void get_WithHead_Handle_entityNull() throws IOException, ParseException {
|
||||
public void get_WithHead_Handle_entityNull() throws IOException {
|
||||
Mockito.when(mockResponse.getCode()).thenReturn(404);
|
||||
mockResponse.setEntity(new StringEntity(FAILURE_JSON_RESPONSE, StandardCharsets.UTF_8));
|
||||
// 模拟对于不同的响应处理器返回不同的值
|
||||
|
@ -680,7 +681,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("私有handleResponse返回值200")
|
||||
public void get_WithHead_Handle_isOK() throws IOException, ParseException {
|
||||
public void get_WithHead_Handle_isOK() throws IOException {
|
||||
when(mockResponse.getCode()).thenReturn(200);
|
||||
when(mockResponse.getEntity()).thenReturn(new StringEntity(FAILURE_JSON_RESPONSE, StandardCharsets.UTF_8));
|
||||
// mockResponse.setEntity(new StringEntity(FAILURE_JSON_RESPONSE, StandardCharsets.UTF_8));
|
||||
|
@ -703,7 +704,7 @@ public class HttpClientUtilsTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("私有handleResponse调用非空Entity")
|
||||
public void get_WithHead_Handle_entityNotNull() throws IOException, ParseException {
|
||||
public void get_WithHead_Handle_entityNotNull() throws IOException {
|
||||
Mockito.when(mockResponse.getCode()).thenReturn(404);
|
||||
when(mockResponse.getEntity()).thenReturn(new StringEntity(FAILURE_JSON_RESPONSE, StandardCharsets.UTF_8));
|
||||
entityUtilsMock.when(() -> EntityUtils.toString(any(HttpEntity.class), any(Charset.class))).thenReturn("message");
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.cmcc.magent.misc;
|
|||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletRequestWrapper;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -52,7 +51,7 @@ public class HttpUtilsTest {
|
|||
@Test
|
||||
@DisplayName("输入多个IP")
|
||||
public void getHttpRequestSrcIp_returnMutileIpSplit() throws UnknownHostException {
|
||||
String testIp = "172.28.22.21, 172.28.22.22";
|
||||
String testIp = "172.28.22.21, 172.28.22.22";
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
when(request.getHeader("x-forwarded-for")).thenReturn(testIp);
|
||||
assertEquals("172.28.22.21", HttpUtils.getHttpRequestSrcIp(request));
|
||||
|
|
|
@ -4,7 +4,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.springframework.context.MessageSource;
|
||||
|
|
Loading…
Reference in New Issue