🎨 补充单元测试
This commit is contained in:
committed by
GitHub
parent
6a4f0db39e
commit
509bce53f3
@@ -0,0 +1,138 @@
|
||||
package me.chanjar.weixin.open.bean.message;
|
||||
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.util.WxOpenCryptUtil;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link WxOpenXmlMessage} 单元测试
|
||||
*/
|
||||
public class WxOpenXmlMessageTest {
|
||||
|
||||
private static final String COMPONENT_APP_ID = "wx0000000000000001";
|
||||
private static final String COMPONENT_TOKEN = "test_component_token";
|
||||
/** 43 位 EncodingAESKey 占位值,非真实密钥 */
|
||||
private static final String COMPONENT_AES_KEY = "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY";
|
||||
|
||||
private static final Pattern ENCRYPT_PATTERN = Pattern.compile("<Encrypt><!\\[CDATA\\[(.*?)]]></Encrypt>");
|
||||
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("<TimeStamp>(.*?)</TimeStamp>");
|
||||
private static final Pattern NONCE_PATTERN = Pattern.compile("<Nonce><!\\[CDATA\\[(.*?)]]></Nonce>");
|
||||
|
||||
private static final String VERIFY_TICKET_XML = "<xml>\n"
|
||||
+ " <AppId><![CDATA[" + COMPONENT_APP_ID + "]]></AppId>\n"
|
||||
+ " <CreateTime>1413192605</CreateTime>\n"
|
||||
+ " <InfoType><![CDATA[component_verify_ticket]]></InfoType>\n"
|
||||
+ " <ComponentVerifyTicket><![CDATA[ticket@@@abcdefg]]></ComponentVerifyTicket>\n"
|
||||
+ "</xml>";
|
||||
|
||||
private WxOpenInMemoryConfigStorage config() {
|
||||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
|
||||
config.setComponentAppId(COMPONENT_APP_ID);
|
||||
config.setComponentToken(COMPONENT_TOKEN);
|
||||
config.setComponentAesKey(COMPONENT_AES_KEY);
|
||||
return config;
|
||||
}
|
||||
|
||||
private String group(Pattern pattern, String text) {
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
assertTrue(matcher.find(), "未匹配到期望的节点:" + pattern.pattern());
|
||||
return matcher.group(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromXmlComponentVerifyTicket() {
|
||||
WxOpenXmlMessage message = WxOpenXmlMessage.fromXml(VERIFY_TICKET_XML);
|
||||
assertNotNull(message);
|
||||
assertEquals(message.getAppId(), COMPONENT_APP_ID);
|
||||
assertEquals(message.getCreateTime(), Long.valueOf(1413192605L));
|
||||
assertEquals(message.getInfoType(), "component_verify_ticket");
|
||||
assertEquals(message.getComponentVerifyTicket(), "ticket@@@abcdefg");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromXmlAuthorized() {
|
||||
String xml = "<xml>\n"
|
||||
+ " <AppId><![CDATA[" + COMPONENT_APP_ID + "]]></AppId>\n"
|
||||
+ " <CreateTime>1413192760</CreateTime>\n"
|
||||
+ " <InfoType><![CDATA[authorized]]></InfoType>\n"
|
||||
+ " <AuthorizerAppid><![CDATA[wx0000000000000002]]></AuthorizerAppid>\n"
|
||||
+ " <AuthorizationCode><![CDATA[auth_code_value]]></AuthorizationCode>\n"
|
||||
+ " <AuthorizationCodeExpiredTime>600</AuthorizationCodeExpiredTime>\n"
|
||||
+ " <PreAuthCode><![CDATA[pre_auth_code_value]]></PreAuthCode>\n"
|
||||
+ "</xml>";
|
||||
|
||||
WxOpenXmlMessage message = WxOpenXmlMessage.fromXml(xml);
|
||||
assertNotNull(message);
|
||||
assertEquals(message.getInfoType(), "authorized");
|
||||
assertEquals(message.getAuthorizerAppid(), "wx0000000000000002");
|
||||
assertEquals(message.getAuthorizationCode(), "auth_code_value");
|
||||
assertEquals(message.getAuthorizationCodeExpiredTime(), Long.valueOf(600L));
|
||||
assertEquals(message.getPreAuthCode(), "pre_auth_code_value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromXmlFastRegisterWeApp() {
|
||||
String xml = "<xml>\n"
|
||||
+ " <AppId><![CDATA[" + COMPONENT_APP_ID + "]]></AppId>\n"
|
||||
+ " <CreateTime>1535442403</CreateTime>\n"
|
||||
+ " <InfoType><![CDATA[notify_third_fasteregister]]></InfoType>\n"
|
||||
+ " <appid>wx0000000000000003</appid>\n"
|
||||
+ " <status>0</status>\n"
|
||||
+ " <auth_code>auth_code_value</auth_code>\n"
|
||||
+ " <msg><![CDATA[OK]]></msg>\n"
|
||||
+ " <info>\n"
|
||||
+ " <name><![CDATA[тест]]></name>\n"
|
||||
+ " </info>\n"
|
||||
+ "</xml>";
|
||||
|
||||
WxOpenXmlMessage message = WxOpenXmlMessage.fromXml(xml);
|
||||
assertNotNull(message);
|
||||
assertEquals(message.getSubAppId(), "wx0000000000000003");
|
||||
assertEquals(message.getRegistAppId(), "wx0000000000000003");
|
||||
assertEquals(message.getStatus(), 0);
|
||||
assertEquals(message.getAuthCode(), "auth_code_value");
|
||||
assertEquals(message.getMsg(), "OK");
|
||||
assertNotNull(message.getInfo());
|
||||
assertEquals(message.getInfo().getName(), "тест");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromXmlInputStream() {
|
||||
InputStream is = new ByteArrayInputStream(VERIFY_TICKET_XML.getBytes(StandardCharsets.UTF_8));
|
||||
WxOpenXmlMessage message = WxOpenXmlMessage.fromXml(is);
|
||||
assertNotNull(message);
|
||||
assertEquals(message.getComponentVerifyTicket(), "ticket@@@abcdefg");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromEncryptedXml() {
|
||||
WxOpenInMemoryConfigStorage config = config();
|
||||
String encryptedXml = new WxOpenCryptUtil(config).encrypt(VERIFY_TICKET_XML);
|
||||
|
||||
String encrypt = group(ENCRYPT_PATTERN, encryptedXml);
|
||||
String timestamp = group(TIMESTAMP_PATTERN, encryptedXml);
|
||||
String nonce = group(NONCE_PATTERN, encryptedXml);
|
||||
String signature = SHA1.gen(COMPONENT_TOKEN, timestamp, nonce, encrypt);
|
||||
|
||||
WxOpenXmlMessage message = WxOpenXmlMessage.fromEncryptedXml(encryptedXml, config, timestamp, nonce, signature);
|
||||
assertNotNull(message);
|
||||
assertEquals(message.getComponentVerifyTicket(), "ticket@@@abcdefg");
|
||||
assertEquals(message.getContext(), VERIFY_TICKET_XML);
|
||||
|
||||
InputStream is = new ByteArrayInputStream(encryptedXml.getBytes(StandardCharsets.UTF_8));
|
||||
WxOpenXmlMessage fromStream = WxOpenXmlMessage.fromEncryptedXml(is, config, timestamp, nonce, signature);
|
||||
assertNotNull(fromStream);
|
||||
assertEquals(fromStream.getInfoType(), "component_verify_ticket");
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@ package me.chanjar.weixin.open.bean.result;
|
||||
import me.chanjar.weixin.open.util.json.WxOpenGsonBuilder;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
|
||||
public class WxFastMaCanSetCategoryResultTest {
|
||||
@@ -73,6 +75,10 @@ public class WxFastMaCanSetCategoryResultTest {
|
||||
|
||||
assertNotNull(res);
|
||||
assertNotNull(res.getCategoriesList());
|
||||
assertEquals(res.getErrcode(), "0");
|
||||
assertEquals(res.getErrmsg(), "ok");
|
||||
assertEquals(res.getErrCode(), 0);
|
||||
assertTrue(res.isSuccess());
|
||||
System.out.println(res);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package me.chanjar.weixin.open.util;
|
||||
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
import static org.testng.Assert.expectThrows;
|
||||
|
||||
/**
|
||||
* {@link WxOpenCryptUtil} 单元测试
|
||||
*/
|
||||
public class WxOpenCryptUtilTest {
|
||||
|
||||
private static final String COMPONENT_APP_ID = "wx0000000000000001";
|
||||
private static final String COMPONENT_TOKEN = "test_component_token";
|
||||
/** 43 位 EncodingAESKey 占位值,非真实密钥 */
|
||||
private static final String COMPONENT_AES_KEY = "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY";
|
||||
|
||||
private static final Pattern ENCRYPT_PATTERN = Pattern.compile("<Encrypt><!\\[CDATA\\[(.*?)]]></Encrypt>");
|
||||
private static final Pattern SIGNATURE_PATTERN =
|
||||
Pattern.compile("<MsgSignature><!\\[CDATA\\[(.*?)]]></MsgSignature>");
|
||||
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("<TimeStamp>(.*?)</TimeStamp>");
|
||||
private static final Pattern NONCE_PATTERN = Pattern.compile("<Nonce><!\\[CDATA\\[(.*?)]]></Nonce>");
|
||||
|
||||
private WxOpenInMemoryConfigStorage config(String aesKey) {
|
||||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
|
||||
config.setComponentAppId(COMPONENT_APP_ID);
|
||||
config.setComponentToken(COMPONENT_TOKEN);
|
||||
config.setComponentAesKey(aesKey);
|
||||
return config;
|
||||
}
|
||||
|
||||
private String group(Pattern pattern, String text) {
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
assertTrue(matcher.find(), "未匹配到期望的节点:" + pattern.pattern());
|
||||
return matcher.group(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptThenDecrypt() {
|
||||
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(config(COMPONENT_AES_KEY));
|
||||
String plainText = "<xml><AppId><![CDATA[" + COMPONENT_APP_ID + "]]></AppId>"
|
||||
+ "<InfoType><![CDATA[component_verify_ticket]]></InfoType></xml>";
|
||||
|
||||
String encryptedXml = cryptUtil.encrypt(plainText);
|
||||
assertNotNull(encryptedXml);
|
||||
|
||||
String encrypt = group(ENCRYPT_PATTERN, encryptedXml);
|
||||
String signature = group(SIGNATURE_PATTERN, encryptedXml);
|
||||
String timestamp = group(TIMESTAMP_PATTERN, encryptedXml);
|
||||
String nonce = group(NONCE_PATTERN, encryptedXml);
|
||||
|
||||
assertEquals(signature, SHA1.gen(COMPONENT_TOKEN, timestamp, nonce, encrypt));
|
||||
assertEquals(cryptUtil.decryptXml(signature, timestamp, nonce, encryptedXml), plainText);
|
||||
assertEquals(cryptUtil.decryptContent(signature, timestamp, nonce, encrypt), plainText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithWrongSignature() {
|
||||
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(config(COMPONENT_AES_KEY));
|
||||
String encryptedXml = cryptUtil.encrypt("<xml><InfoType><![CDATA[unauthorized]]></InfoType></xml>");
|
||||
|
||||
String timestamp = group(TIMESTAMP_PATTERN, encryptedXml);
|
||||
String nonce = group(NONCE_PATTERN, encryptedXml);
|
||||
|
||||
expectThrows(WxRuntimeException.class,
|
||||
() -> cryptUtil.decryptXml("wrong_signature", timestamp, nonce, encryptedXml));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAesKeyWithSpaces() {
|
||||
String plainText = "<xml><InfoType><![CDATA[authorized]]></InfoType></xml>";
|
||||
WxOpenCryptUtil cryptUtil = new WxOpenCryptUtil(config(COMPONENT_AES_KEY));
|
||||
WxOpenCryptUtil cryptUtilWithSpaces = new WxOpenCryptUtil(
|
||||
config(" " + COMPONENT_AES_KEY.substring(0, 10) + " " + COMPONENT_AES_KEY.substring(10) + " "));
|
||||
|
||||
String randomStr = "1234567890123456";
|
||||
assertEquals(cryptUtilWithSpaces.encrypt(randomStr, plainText), cryptUtil.encrypt(randomStr, plainText));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package me.chanjar.weixin.open.util.json;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
|
||||
import me.chanjar.weixin.open.bean.WxOpenComponentAccessToken;
|
||||
import me.chanjar.weixin.open.bean.auth.WxOpenAuthorizationInfo;
|
||||
import me.chanjar.weixin.open.bean.result.WxOpenQueryAuthResult;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertSame;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link WxOpenGsonBuilder} 及其注册的反序列化适配器单元测试
|
||||
*/
|
||||
public class WxOpenGsonBuilderTest {
|
||||
|
||||
@Test
|
||||
public void testCreateReturnsSameInstance() {
|
||||
assertSame(WxOpenGsonBuilder.create(), WxOpenGsonBuilder.create());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComponentAccessToken() {
|
||||
String json = "{\"component_access_token\":\"component_access_token_value\",\"expires_in\":7200}";
|
||||
WxOpenComponentAccessToken token = WxOpenGsonBuilder.create().fromJson(json, WxOpenComponentAccessToken.class);
|
||||
assertNotNull(token);
|
||||
assertEquals(token.getComponentAccessToken(), "component_access_token_value");
|
||||
assertEquals(token.getExpiresIn(), 7200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthorizerAccessToken() {
|
||||
String json = "{\"authorizer_access_token\":\"access_token_value\","
|
||||
+ "\"authorizer_refresh_token\":\"refresh_token_value\",\"expires_in\":7200}";
|
||||
WxOpenAuthorizerAccessToken token = WxOpenGsonBuilder.create().fromJson(json, WxOpenAuthorizerAccessToken.class);
|
||||
assertNotNull(token);
|
||||
assertEquals(token.getAuthorizerAccessToken(), "access_token_value");
|
||||
assertEquals(token.getAuthorizerRefreshToken(), "refresh_token_value");
|
||||
assertEquals(token.getExpiresIn(), 7200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthorizationInfo() {
|
||||
String json = "{\"authorizer_appid\":\"wx0000000000000002\","
|
||||
+ "\"authorizer_access_token\":\"access_token_value\","
|
||||
+ "\"authorizer_refresh_token\":\"refresh_token_value\","
|
||||
+ "\"expires_in\":7200,"
|
||||
+ "\"func_info\":[{\"funcscope_category\":{\"id\":1}},{\"funcscope_category\":{\"id\":15}},{}]}";
|
||||
|
||||
WxOpenAuthorizationInfo info = WxOpenGsonBuilder.create().fromJson(json, WxOpenAuthorizationInfo.class);
|
||||
assertNotNull(info);
|
||||
assertEquals(info.getAuthorizerAppid(), "wx0000000000000002");
|
||||
assertEquals(info.getAuthorizerAccessToken(), "access_token_value");
|
||||
assertEquals(info.getAuthorizerRefreshToken(), "refresh_token_value");
|
||||
assertEquals(info.getExpiresIn(), 7200);
|
||||
assertEquals(info.getFuncInfo(), Arrays.asList(1, 15));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAuthorizationInfoWithoutFuncInfo() {
|
||||
WxOpenAuthorizationInfo info = WxOpenGsonBuilder.create()
|
||||
.fromJson("{\"authorizer_appid\":\"wx0000000000000002\"}", WxOpenAuthorizationInfo.class);
|
||||
assertNotNull(info);
|
||||
assertNotNull(info.getFuncInfo());
|
||||
assertTrue(info.getFuncInfo().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryAuthResult() {
|
||||
String json = "{\"authorization_info\":{\"authorizer_appid\":\"wx0000000000000002\","
|
||||
+ "\"authorizer_access_token\":\"access_token_value\",\"expires_in\":7200,"
|
||||
+ "\"authorizer_refresh_token\":\"refresh_token_value\","
|
||||
+ "\"func_info\":[{\"funcscope_category\":{\"id\":1}}]}}";
|
||||
|
||||
WxOpenQueryAuthResult result = WxOpenGsonBuilder.create().fromJson(json, WxOpenQueryAuthResult.class);
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getAuthorizationInfo());
|
||||
assertEquals(result.getAuthorizationInfo().getAuthorizerAppid(), "wx0000000000000002");
|
||||
assertEquals(result.getAuthorizationInfo().getFuncInfo(), Arrays.asList(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHtmlEscapingDisabled() {
|
||||
Gson gson = WxOpenGsonBuilder.create();
|
||||
assertEquals(gson.toJson("a&b"), "\"a&b\"");
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,11 @@
|
||||
<class name="me.chanjar.weixin.open.bean.result.WxFastMaBeenSetCategoryResultTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
<test name="Util_Test">
|
||||
<classes>
|
||||
<class name="me.chanjar.weixin.open.util.WxOpenCryptUtilTest"/>
|
||||
<class name="me.chanjar.weixin.open.util.json.WxOpenGsonBuilderTest"/>
|
||||
<class name="me.chanjar.weixin.open.bean.message.WxOpenXmlMessageTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
Reference in New Issue
Block a user