为新增加的发送红包的接口方法添加单元测试,仅测试接口格式,未测试实际功能

This commit is contained in:
Binary Wang
2016-09-25 00:28:58 +08:00
parent 7f830e2755
commit b13d3c9e6d
6 changed files with 102 additions and 48 deletions

View File

@@ -19,14 +19,8 @@ package me.chanjar.weixin.common.util.crypto;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
@@ -36,7 +30,6 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
@@ -79,32 +72,6 @@ public class WxCryptUtil {
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
}
/**
* 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
* @param packageParams 原始参数
* @param signKey 加密Key(即 商户Key)
* @return 签名字符串
*/
public static String createSignForPay(Map<String, String> packageParams, String signKey) {
SortedMap<String, String> sortedMap = new TreeMap<>();
sortedMap.putAll(packageParams);
List<String> keys = new ArrayList<>(packageParams.keySet());
Collections.sort(keys);
StringBuffer toSign = new StringBuffer();
for (String key : keys) {
String value = packageParams.get(key);
if (null != value && !"".equals(value) && !"sign".equals(key)
&& !"key".equals(key)) {
toSign.append(key + "=" + value + "&");
}
}
toSign.append("key=" + signKey);
String sign = DigestUtils.md5Hex(toSign.toString()).toUpperCase();
return sign;
}
static String extractEncryptPart(String xml) {
try {
DocumentBuilder db = builderLocal.get();

View File

@@ -1,7 +1,7 @@
package me.chanjar.weixin.common.util.http;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
import java.io.IOException;
import org.apache.http.Consts;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
@@ -10,7 +10,8 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import java.io.IOException;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
/**
* 简单的POST请求执行器请求的参数是String, 返回的结果也是String
@@ -34,6 +35,11 @@ public class SimplePostRequestExecutor implements RequestExecutor<String, String
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
if (responseContent.startsWith("<xml>")) {
//xml格式输出直接返回
return responseContent;
}
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);

View File

@@ -1,5 +1,7 @@
package me.chanjar.weixin.common.util.xml;
import java.io.Writer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
@@ -8,8 +10,6 @@ import com.thoughtworks.xstream.io.xml.XppDriver;
import com.thoughtworks.xstream.security.NullPermission;
import com.thoughtworks.xstream.security.PrimitiveTypePermission;
import java.io.Writer;
public class XStreamInitializer {
public static XStream getInstance() {
@@ -34,6 +34,11 @@ public class XStreamInitializer {
}
}
@Override
public String encodeNode(String name) {
return name;//防止将_转换成__
}
};
}
});