refactor: 重命名 backend-single 目录为 server
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package com.emotion.interceptor;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* WechatApiLoggingInterceptor 单元测试
|
||||
*
|
||||
* 注:desensitize() 是 private 方法,通过反射测试
|
||||
*/
|
||||
class WechatApiLoggingInterceptorTest {
|
||||
|
||||
@Test
|
||||
void desensitize_shouldMaskSessionKey() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String input = "{\"openid\":\"oXyz123\",\"session_key\":\"abc@456\"}";
|
||||
String result = (String) method.invoke(interceptor, input);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.contains("\"openid\":\"***\""), "openid 应被脱敏: " + result);
|
||||
assertTrue(result.contains("\"session_key\":\"***\""), "session_key 应被脱敏: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldMaskAccessToken() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String input = "{\"access_token\":\"ACCESS-123\",\"refresh_token\":\"REFRESH-456\"}";
|
||||
String result = (String) method.invoke(interceptor, input);
|
||||
|
||||
assertTrue(result.contains("\"access_token\":\"***\""));
|
||||
assertTrue(result.contains("\"refresh_token\":\"***\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldHandleNull() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
assertEquals(null, method.invoke(interceptor, (Object) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldNotModifyNonSensitiveFields() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String input = "{\"errcode\":40013,\"errmsg\":\"invalid appid\"}";
|
||||
String result = (String) method.invoke(interceptor, input);
|
||||
|
||||
assertEquals(input, result, "非敏感字段不应被修改");
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldNotMatchAccessTokenV2() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
// 验证 access_token_v2 不会被 access_token 模式误匹配
|
||||
String input = "{\"access_token_v2\":\"PUBLIC_VALUE\",\"my_reset_secret\":\"OK\"}";
|
||||
String result = (String) method.invoke(interceptor, input);
|
||||
|
||||
// access_token_v2 不应被脱敏(正则精确匹配完整 key 名称)
|
||||
// my_reset_secret 也不应被脱敏(正则需要精确匹配 "secret" 而非子串)
|
||||
assertTrue(result.contains("\"access_token_v2\":\"PUBLIC_VALUE\""), "access_token_v2 不应被脱敏: " + result);
|
||||
assertTrue(result.contains("\"my_reset_secret\":\"OK\""), "my_reset_secret 不应被脱敏: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldHandleEmptyString() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String result = (String) method.invoke(interceptor, "");
|
||||
assertEquals("", result, "空字符串应原样返回");
|
||||
}
|
||||
|
||||
@Test
|
||||
void desensitize_shouldMaskMultipleSensitiveFieldsSimultaneously() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("desensitize", String.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String input = "{\"openid\":\"o123\",\"access_token\":\"tok\",\"unionid\":\"uni456\",\"refresh_token\":\"ref789\"}";
|
||||
String result = (String) method.invoke(interceptor, input);
|
||||
|
||||
assertTrue(result.contains("\"openid\":\"***\""));
|
||||
assertTrue(result.contains("\"access_token\":\"***\""));
|
||||
assertTrue(result.contains("\"unionid\":\"***\""));
|
||||
assertTrue(result.contains("\"refresh_token\":\"***\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void maskSensitiveQuery_shouldMaskWechatCredentialsInUrl() throws Exception {
|
||||
WechatApiLoggingInterceptor interceptor = new WechatApiLoggingInterceptor();
|
||||
Method method = WechatApiLoggingInterceptor.class.getDeclaredMethod("maskSensitiveQuery", URI.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
String result = (String) method.invoke(interceptor,
|
||||
URI.create("https://api.weixin.qq.com/sns/jscode2session?appid=wx123&secret=s1&js_code=c1&grant_type=authorization_code"));
|
||||
|
||||
assertTrue(result.contains("appid=wx123"));
|
||||
assertTrue(result.contains("secret=***"));
|
||||
assertTrue(result.contains("js_code=***"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user