mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-31 02:28:25 +08:00
整理及重构
This commit is contained in:
@ -0,0 +1,18 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class WxAccessTokenTest {
|
||||
|
||||
public void testFromJson() {
|
||||
|
||||
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
|
||||
WxAccessToken wxError = WxAccessToken.fromJson(json);
|
||||
Assert.assertEquals(wxError.getAccessToken(), "ACCESS_TOKEN");
|
||||
Assert.assertTrue(wxError.getExpiresIn() == 7200);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class WxErrorTest {
|
||||
|
||||
public void testFromJson() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson1() {
|
||||
|
||||
String json = "{ \"errcode\": 40003, \"errmsg\": \"invalid openid\", \"media_id\": \"12323423dsfafsf232f\" }";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 40003);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), "invalid openid");
|
||||
|
||||
}
|
||||
|
||||
public void testFromBadJson2() {
|
||||
|
||||
String json = "{\"access_token\":\"ACCESS_TOKEN\",\"expires_in\":7200}";
|
||||
WxError wxError = WxError.fromJson(json);
|
||||
Assert.assertTrue(wxError.getErrorCode() == 0);
|
||||
Assert.assertEquals(wxError.getErrorMsg(), null);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
package me.chanjar.weixin.common.bean;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class WxMenuTest {
|
||||
|
||||
@Test(dataProvider="wxReturnMenu")
|
||||
public void testFromJson(String json) {
|
||||
WxMenu menu = WxMenu.fromJson(json);
|
||||
Assert.assertEquals(menu.getButtons().size(), 3);
|
||||
}
|
||||
|
||||
@Test(dataProvider="wxPushMenu")
|
||||
public void testToJson(String json) {
|
||||
WxMenu menu = new WxMenu();
|
||||
WxMenuButton button1 = new WxMenuButton();
|
||||
button1.setType("click");
|
||||
button1.setName("今日歌曲");
|
||||
button1.setKey("V1001_TODAY_MUSIC");
|
||||
|
||||
WxMenuButton button2 = new WxMenuButton();
|
||||
button2.setType("click");
|
||||
button2.setName("歌手简介");
|
||||
button2.setKey("V1001_TODAY_SINGER");
|
||||
|
||||
WxMenuButton button3 = new WxMenuButton();
|
||||
button3.setName("菜单");
|
||||
|
||||
menu.getButtons().add(button1);
|
||||
menu.getButtons().add(button2);
|
||||
menu.getButtons().add(button3);
|
||||
|
||||
WxMenuButton button31 = new WxMenuButton();
|
||||
button31.setType("view");
|
||||
button31.setName("搜索");
|
||||
button31.setUrl("http://www.soso.com/");
|
||||
|
||||
WxMenuButton button32 = new WxMenuButton();
|
||||
button32.setType("view");
|
||||
button32.setName("视频");
|
||||
button32.setUrl("http://v.qq.com/");
|
||||
|
||||
WxMenuButton button33 = new WxMenuButton();
|
||||
button33.setType("click");
|
||||
button33.setName("赞一下我们");
|
||||
button33.setKey("V1001_GOOD");
|
||||
|
||||
button3.getSubButtons().add(button31);
|
||||
button3.getSubButtons().add(button32);
|
||||
button3.getSubButtons().add(button33);
|
||||
|
||||
Assert.assertEquals(menu.toJson(), json);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] wxReturnMenu() {
|
||||
Object[][] res = menuJson();
|
||||
String json = "{ \"menu\" : " + res[0][0] + " }";
|
||||
return new Object[][] {
|
||||
new Object[] { json }
|
||||
};
|
||||
}
|
||||
|
||||
@DataProvider(name="wxPushMenu")
|
||||
public Object[][] menuJson() {
|
||||
String json =
|
||||
"{"
|
||||
+"\"button\":["
|
||||
+"{"
|
||||
+"\"type\":\"click\","
|
||||
+"\"name\":\"今日歌曲\","
|
||||
+"\"key\":\"V1001_TODAY_MUSIC\""
|
||||
+"},"
|
||||
+"{"
|
||||
+"\"type\":\"click\","
|
||||
+"\"name\":\"歌手简介\","
|
||||
+"\"key\":\"V1001_TODAY_SINGER\""
|
||||
+"},"
|
||||
+"{"
|
||||
+"\"name\":\"菜单\","
|
||||
+"\"sub_button\":["
|
||||
+"{"
|
||||
+"\"type\":\"view\","
|
||||
+"\"name\":\"搜索\","
|
||||
+"\"url\":\"http://www.soso.com/\""
|
||||
+"},"
|
||||
+"{"
|
||||
+"\"type\":\"view\","
|
||||
+"\"name\":\"视频\","
|
||||
+"\"url\":\"http://v.qq.com/\""
|
||||
+"},"
|
||||
+"{"
|
||||
+"\"type\":\"click\","
|
||||
+"\"name\":\"赞一下我们\","
|
||||
+"\"key\":\"V1001_GOOD\""
|
||||
+"}"
|
||||
+"]"
|
||||
+"}"
|
||||
+"]"
|
||||
+"}";
|
||||
return new Object[][] {
|
||||
new Object[] { json }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,95 @@
|
||||
package me.chanjar.weixin.common.util.crypto;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.fail;
|
||||
|
||||
@Test
|
||||
public class WxCryptUtilTest {
|
||||
String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
|
||||
String token = "pamtest";
|
||||
String timestamp = "1409304348";
|
||||
String nonce = "xxxxxx";
|
||||
String appId = "wxb11529c136998cb6";
|
||||
String randomStr = "aaaabbbbccccdddd";
|
||||
|
||||
String xmlFormat = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%1$s]]></Encrypt></xml>";
|
||||
String replyMsg = "我是中文abcd123";
|
||||
|
||||
String afterAesEncrypt = "jn1L23DB+6ELqJ+6bruv21Y6MD7KeIfP82D6gU39rmkgczbWwt5+3bnyg5K55bgVtVzd832WzZGMhkP72vVOfg==";
|
||||
|
||||
String replyMsg2 = "<xml><ToUserName><![CDATA[oia2Tj我是中文jewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
|
||||
String afterAesEncrypt2 = "jn1L23DB+6ELqJ+6bruv23M2GmYfkv0xBh2h+XTBOKVKcgDFHle6gqcZ1cZrk3e1qjPQ1F4RsLWzQRG9udbKWesxlkupqcEcW7ZQweImX9+wLMa0GaUzpkycA8+IamDBxn5loLgZpnS7fVAbExOkK5DYHBmv5tptA9tklE/fTIILHR8HLXa5nQvFb3tYPKAlHF3rtTeayNf0QuM+UW/wM9enGIDIJHF7CLHiDNAYxr+r+OrJCmPQyTy8cVWlu9iSvOHPT/77bZqJucQHQ04sq7KZI27OcqpQNSto2OdHCoTccjggX5Z9Mma0nMJBU+jLKJ38YB1fBIz+vBzsYjrTmFQ44YfeEuZ+xRTQwr92vhA9OxchWVINGC50qE/6lmkwWTwGX9wtQpsJKhP+oS7rvTY8+VdzETdfakjkwQ5/Xka042OlUb1/slTwo4RscuQ+RdxSGvDahxAJ6+EAjLt9d8igHngxIbf6YyqqROxuxqIeIch3CssH/LqRs+iAcILvApYZckqmA7FNERspKA5f8GoJ9sv8xmGvZ9Yrf57cExWtnX8aCMMaBropU/1k+hKP5LVdzbWCG0hGwx/dQudYR/eXp3P0XxjlFiy+9DMlaFExWUZQDajPkdPrEeOwofJb";
|
||||
|
||||
public void testNormal() throws ParserConfigurationException, SAXException, IOException {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
String encryptedXml = pc.encrypt(replyMsg);
|
||||
|
||||
System.out.println(encryptedXml);
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
Document document = documentBuilder.parse(new InputSource(new StringReader(encryptedXml)));
|
||||
|
||||
Element root = document.getDocumentElement();
|
||||
String cipherText = root.getElementsByTagName("Encrypt").item(0).getTextContent();
|
||||
String msgSignature = root.getElementsByTagName("MsgSignature").item(0).getTextContent();
|
||||
String timestamp = root.getElementsByTagName("TimeStamp").item(0).getTextContent();
|
||||
String nonce = root.getElementsByTagName("Nonce").item(0).getTextContent();
|
||||
|
||||
String messageText = String.format(xmlFormat, cipherText);
|
||||
|
||||
// 第三方收到企业号平台发送的消息
|
||||
String plainMessage = pc.decrypt(cipherText);
|
||||
|
||||
System.out.println(plainMessage);
|
||||
assertEquals(plainMessage, replyMsg);
|
||||
}
|
||||
|
||||
public void testAesEncrypt() {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
assertEquals(pc.encrypt(randomStr, replyMsg), afterAesEncrypt);
|
||||
}
|
||||
|
||||
public void testAesEncrypt2() {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
assertEquals(pc.encrypt(randomStr, replyMsg2), afterAesEncrypt2);
|
||||
}
|
||||
|
||||
public void testValidateSignatureError() throws ParserConfigurationException, SAXException,
|
||||
IOException {
|
||||
try {
|
||||
WxCryptUtil pc = new WxCryptUtil(token, encodingAesKey, appId);
|
||||
String afterEncrpt = pc.encrypt(replyMsg);
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
StringReader sr = new StringReader(afterEncrpt);
|
||||
InputSource is = new InputSource(sr);
|
||||
Document document = db.parse(is);
|
||||
|
||||
Element root = document.getDocumentElement();
|
||||
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
|
||||
|
||||
String encrypt = nodelist1.item(0).getTextContent();
|
||||
String fromXML = String.format(xmlFormat, encrypt);
|
||||
pc.decrypt("12345", timestamp, nonce, fromXML); // 这里签名错误
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals(e.getMessage(), "加密消息签名校验失败");
|
||||
return;
|
||||
}
|
||||
fail("错误流程不抛出异常???");
|
||||
}
|
||||
|
||||
}
|
||||
12
weixin-java-common/src/test/resources/testng.xml
Normal file
12
weixin-java-common/src/test/resources/testng.xml
Normal file
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
|
||||
<suite name="Weixin-java-tool-suite" verbose="1">
|
||||
<test name="Bean_Test">
|
||||
<classes>
|
||||
<class name="me.chanjar.weixin.common.bean.WxAccessTokenTest" />
|
||||
<class name="me.chanjar.weixin.common.bean.WxErrorTest" />
|
||||
<class name="me.chanjar.weixin.common.bean.WxMenuTest" />
|
||||
<class name="me.chanjar.weixin.common.util.crypto.WxCryptUtilTest" />
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
Reference in New Issue
Block a user