refactor: 重命名 backend-single 目录为 server
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.emotion.exception;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* GlobalExceptionHandler 的 WechatApiException handler 单元测试
|
||||
*
|
||||
* 验证:
|
||||
* 1. 400 业务错误 → "微信授权失败,请重试"
|
||||
* 2. 500 内部错误 → "微信登录失败,请重试"
|
||||
* 3. 502 上游错误 → "微信服务暂不可用,请稍后重试"
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GlobalExceptionHandlerWechatTest {
|
||||
|
||||
@InjectMocks
|
||||
private GlobalExceptionHandler handler;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
request = new MockHttpServletRequest();
|
||||
request.setMethod("POST");
|
||||
request.setRequestURI("/api/wechat/login");
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code400_ReturnsBadRequestWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(400, "errcode=40029, errmsg=invalid code (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信授权失败,请重试", result.getMessage());
|
||||
// Ensure internal detail is NOT exposed to frontend
|
||||
assertFalse(result.getMessage().contains("errcode"));
|
||||
assertFalse(result.getMessage().contains("40029"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code500_ReturnsInternalErrorWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(500, "JSON parse failure at line 1 (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信登录失败,请重试", result.getMessage());
|
||||
assertFalse(result.getMessage().contains("JSON"));
|
||||
assertFalse(result.getMessage().contains("parse"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void handleWechatApiException_Code502_ReturnsBadGatewayWithFriendlyMessage() {
|
||||
WechatApiException ex = new WechatApiException(502, "Connection refused: api.weixin.qq.com (internal detail)", "");
|
||||
|
||||
Result<?> result = handler.handleWechatApiException(ex, request);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals("微信服务暂不可用,请稍后重试", result.getMessage());
|
||||
assertFalse(result.getMessage().contains("Connection"));
|
||||
assertFalse(result.getMessage().contains("refused"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user