mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
测试all in one
This commit is contained in:
175
sop-test/src/main/java/com/gitee/sop/test/Client.java
Normal file
175
sop-test/src/main/java/com/gitee/sop/test/Client.java
Normal file
@@ -0,0 +1,175 @@
|
||||
package com.gitee.sop.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.test.alipay.AlipayApiException;
|
||||
import com.gitee.sop.test.alipay.AlipaySignature;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 简易客户端
|
||||
* @author tanghc
|
||||
*/
|
||||
@Data
|
||||
public class Client {
|
||||
private static HttpTool httpTool = new HttpTool();
|
||||
|
||||
private String url;
|
||||
private String appId;
|
||||
private String privateKey;
|
||||
|
||||
private Callback callback;
|
||||
|
||||
public Client(String url, String appId, String privateKey) {
|
||||
this.url = url;
|
||||
this.appId = appId;
|
||||
this.privateKey = privateKey;
|
||||
}
|
||||
|
||||
public Client(String url, String appId, String privateKey, Callback callback) {
|
||||
this.url = url;
|
||||
this.appId = appId;
|
||||
this.privateKey = privateKey;
|
||||
this.callback = callback;
|
||||
}
|
||||
|
||||
public String execute(RequestBuilder requestBuilder) {
|
||||
RequestInfo requestInfo = requestBuilder.build(appId, privateKey);
|
||||
HttpTool.HTTPMethod httpMethod = requestInfo.getHttpMethod();
|
||||
boolean postJson = requestInfo.isPostJson();
|
||||
Map<String, ?> form = requestInfo.getForm();
|
||||
Map<String, String> header = requestInfo.getHeader();
|
||||
String requestUrl = requestInfo.getUrl() != null ? requestInfo.getUrl() : url;
|
||||
String responseData = null;
|
||||
try {
|
||||
// 发送请求
|
||||
if (httpMethod == HttpTool.HTTPMethod.POST && postJson) {
|
||||
responseData = httpTool.requestJson(requestUrl, JSON.toJSONString(form), header);
|
||||
} else {
|
||||
responseData = httpTool.request(requestUrl, form, header, httpMethod);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (callback != null) {
|
||||
callback.callback(requestInfo, responseData);
|
||||
}
|
||||
return responseData;
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
void callback(RequestInfo requestInfo, String responseData);
|
||||
}
|
||||
|
||||
public static class RequestBuilder {
|
||||
private static final String DEFAULT_VERSION = "1.0";
|
||||
|
||||
private String url;
|
||||
private String method;
|
||||
private String version = DEFAULT_VERSION;
|
||||
private Map<String, String> bizContent;
|
||||
private HttpTool.HTTPMethod httpMethod;
|
||||
private Map<String, String> header;
|
||||
private boolean ignoreSign;
|
||||
private boolean postJson;
|
||||
|
||||
public RequestBuilder url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder method(String method) {
|
||||
this.method = method;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder version(String version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder bizContent(Map<String, String> bizContent) {
|
||||
this.bizContent = bizContent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder httpMethod(HttpTool.HTTPMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder header(Map<String, String> header) {
|
||||
this.header = header;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder ignoreSign(boolean ignoreSign) {
|
||||
this.ignoreSign = ignoreSign;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestBuilder postJson(boolean postJson) {
|
||||
this.postJson = postJson;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public RequestInfo build(String appId, String privateKey) {
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
if (method != null) {
|
||||
params.put("method", method);
|
||||
}
|
||||
if (version != null) {
|
||||
params.put("version", version);
|
||||
}
|
||||
params.put("format", "json");
|
||||
params.put("charset", "utf-8");
|
||||
params.put("sign_type", "RSA2");
|
||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
// 业务参数
|
||||
params.put("biz_content", JSON.toJSONString(bizContent == null ? Collections.emptyMap() : bizContent));
|
||||
|
||||
if (!ignoreSign) {
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
String sign = null;
|
||||
try {
|
||||
sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
} catch (AlipayApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
params.put("sign", sign);
|
||||
}
|
||||
|
||||
RequestInfo requestInfo = new RequestInfo();
|
||||
requestInfo.setUrl(url);
|
||||
requestInfo.setMethod(method);
|
||||
requestInfo.setVersion(version);
|
||||
requestInfo.setForm(params);
|
||||
requestInfo.setHeader(header);
|
||||
requestInfo.setPostJson(postJson);
|
||||
requestInfo.setHttpMethod(httpMethod);
|
||||
return requestInfo;
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class RequestInfo {
|
||||
private String url;
|
||||
private String method;
|
||||
private String version;
|
||||
private boolean postJson;
|
||||
private Map<String, ?> form;
|
||||
private Map<String, String> header;
|
||||
private HttpTool.HTTPMethod httpMethod;
|
||||
}
|
||||
|
||||
}
|
@@ -1,6 +1,5 @@
|
||||
package com.gitee.sop.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -112,9 +111,6 @@ public class HttpTool {
|
||||
* @throws IOException
|
||||
*/
|
||||
public String request(String url, Map<String, ?> form, Map<String, String> header, HTTPMethod method) throws IOException {
|
||||
if (method == HTTPMethod.JSON) {
|
||||
return requestJson(url, JSON.toJSONString(form), header);
|
||||
}
|
||||
Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
|
||||
// 添加header
|
||||
addHeader(requestBuilder, header);
|
||||
@@ -264,8 +260,7 @@ public class HttpTool {
|
||||
POST,
|
||||
PUT,
|
||||
HEAD,
|
||||
DELETE,
|
||||
JSON;
|
||||
DELETE;
|
||||
|
||||
private HTTPMethod() {
|
||||
}
|
||||
|
@@ -2,16 +2,9 @@ package com.gitee.sop.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.gitee.sop.test.alipay.AlipayApiException;
|
||||
import com.gitee.sop.test.alipay.AlipaySignature;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -23,120 +16,130 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
*/
|
||||
public class AllInOneTest extends TestBase {
|
||||
|
||||
static String url = "http://localhost:8081/api";
|
||||
static String appId = "2019032617262200001";
|
||||
static String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||||
String url = "http://localhost:8081/api";
|
||||
String appId = "2019032617262200001";
|
||||
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
|
||||
|
||||
Client client = new Client()
|
||||
.url(url)
|
||||
.appId(appId)
|
||||
.privateKey(privateKey)
|
||||
.callback(AllInOneTest::assertResult);
|
||||
private Client client = new Client(url, appId, privateKey, AllInOneTest::assertResult);
|
||||
|
||||
/**
|
||||
* 校验基本功能
|
||||
*
|
||||
* @throws AlipayApiException
|
||||
* 以get方式提交
|
||||
*/
|
||||
public void testBase() {
|
||||
client.request(
|
||||
"alipay.story.get"
|
||||
, "1.0"
|
||||
, new BizContent().add("id", "1").add("name", "葫芦娃")
|
||||
, HttpTool.HTTPMethod.GET
|
||||
);
|
||||
public void testGet() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("alipay.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.request(
|
||||
"alipay.story.get"
|
||||
, "1.0"
|
||||
, new BizContent().add("id", "1").add("name", "葫芦娃")
|
||||
, HttpTool.HTTPMethod.POST
|
||||
);
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
client.request(
|
||||
"alipay.story.get"
|
||||
, "1.0"
|
||||
, new BizContent().add("id", "1").add("name", "葫芦娃")
|
||||
, HttpTool.HTTPMethod.JSON
|
||||
);
|
||||
/**
|
||||
* 以表单方式提交(application/x-www-form-urlencoded)
|
||||
*/
|
||||
public void testPostForm() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("alipay.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.httpMethod(HttpTool.HTTPMethod.POST);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以json方式提交(application/json)
|
||||
*/
|
||||
public void testPostJSON() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("alipay.story.get")
|
||||
.version("1.0")
|
||||
// 以json方式提交
|
||||
.postJson(true)
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.httpMethod(HttpTool.HTTPMethod.POST);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试feign。gateway -> book-service(consumer) -> story-service(provider)
|
||||
*/
|
||||
public void testFeign() {
|
||||
client.request(
|
||||
"alipay.book.story.get"
|
||||
, "1.0"
|
||||
, new BizContent()
|
||||
, HttpTool.HTTPMethod.GET
|
||||
);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("alipay.book.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent())
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试dubbo服务,book会调用story提供的服务。参见:DemoConsumerController.java
|
||||
*/
|
||||
public void testDubbo() {
|
||||
client.request(
|
||||
"dubbo.story.get"
|
||||
, "1.0"
|
||||
, new BizContent().add("id", "222")
|
||||
, HttpTool.HTTPMethod.GET
|
||||
);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("dubbo.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "222"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略验证,不校验签名,只需传接口名、版本号、业务参数
|
||||
*/
|
||||
public void testIgnoreSign() {
|
||||
new Client()
|
||||
.url(url)
|
||||
.appId(appId)
|
||||
.privateKey(privateKey)
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get")
|
||||
.version("2.1")
|
||||
.ignoreSign(true)
|
||||
.request(
|
||||
"story.get"
|
||||
, "2.1"
|
||||
, new BizContent().add("id", "222").add("name", "忽略222")
|
||||
, HttpTool.HTTPMethod.GET);
|
||||
.bizContent(new BizContent().add("id", "222").add("name", "忽略222"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSR-303参数校验
|
||||
*/
|
||||
public void testJSR303() {
|
||||
client.request(
|
||||
"goods.add"
|
||||
, "1.0"
|
||||
, new BizContent().add("goods_name", "iphone6").add("goods_remark", "iphone6").add("goods_comment", "1")
|
||||
, HttpTool.HTTPMethod.POST
|
||||
);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("goods.add")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("goods_name", "iphone6").add("goods_remark", "iphone6").add("goods_comment", "1"))
|
||||
.httpMethod(HttpTool.HTTPMethod.POST);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试是否有权限访问,可在sop-admin中设置权限
|
||||
*/
|
||||
public void testPermission() {
|
||||
client.request("permission.story.get"
|
||||
, "1.0"
|
||||
, new BizContent()
|
||||
, HttpTool.HTTPMethod.GET
|
||||
);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("permission.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent())
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 演示将接口名版本号跟在url后面,规则:http://host:port/{method}/{version}/
|
||||
*/
|
||||
public void testRestful() {
|
||||
new Client()
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.url("http://localhost:8081/alipay.story.get/1.0/")
|
||||
.appId(appId)
|
||||
.privateKey(privateKey)
|
||||
.callback(((method, responseData) -> {
|
||||
assertResult("alipay.story.get", responseData);
|
||||
}))
|
||||
.request(new BizContent().add("name", "name111"), HttpTool.HTTPMethod.GET);
|
||||
.bizContent(new BizContent().add("name", "name111"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,12 +159,13 @@ public class AllInOneTest extends TestBase {
|
||||
try {
|
||||
countDownLatch.await(); // 等在这里,执行countDownLatch.countDown();集体触发
|
||||
// 业务方法
|
||||
client.request(
|
||||
"alipay.story.get"
|
||||
, "1.0"
|
||||
, new BizContent().add("id", "1").add("name", "葫芦娃")
|
||||
, HttpTool.HTTPMethod.GET
|
||||
);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("alipay.story.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
success.incrementAndGet();
|
||||
} catch (Exception e) {
|
||||
} finally {
|
||||
@@ -175,88 +179,6 @@ public class AllInOneTest extends TestBase {
|
||||
System.out.println("成功次数:" + success);
|
||||
}
|
||||
|
||||
static class Client {
|
||||
private String url;
|
||||
private String appId;
|
||||
private String privateKey;
|
||||
private Callback callback;
|
||||
private boolean ignoreSign;
|
||||
|
||||
public Client url(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client appId(String appId) {
|
||||
this.appId = appId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client privateKey(String privateKey) {
|
||||
this.privateKey = privateKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client callback(Callback callback) {
|
||||
this.callback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Client ignoreSign(boolean ignoreSign) {
|
||||
this.ignoreSign = ignoreSign;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String request(Map<String, String> bizContent, HttpTool.HTTPMethod httpMethod) {
|
||||
return request(null, null, bizContent, httpMethod);
|
||||
}
|
||||
|
||||
public String request(String method, String version, Map<String, String> bizContent, HttpTool.HTTPMethod httpMethod) {
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
if (method != null) {
|
||||
params.put("method", method);
|
||||
}
|
||||
if (version != null) {
|
||||
params.put("version", version);
|
||||
}
|
||||
params.put("format", "json");
|
||||
params.put("charset", "utf-8");
|
||||
params.put("sign_type", "RSA2");
|
||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
// 业务参数
|
||||
params.put("biz_content", JSON.toJSONString(bizContent == null ? Collections.emptyMap() : bizContent));
|
||||
|
||||
if (!ignoreSign) {
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
String sign = null;
|
||||
try {
|
||||
sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
} catch (AlipayApiException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
params.put("sign", sign);
|
||||
}
|
||||
String responseData = null;
|
||||
try {
|
||||
// 发送请求
|
||||
responseData = httpTool.request(url, params, null, httpMethod);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
if (callback != null) {
|
||||
callback.callback(method, responseData);
|
||||
}
|
||||
return responseData;
|
||||
}
|
||||
|
||||
interface Callback {
|
||||
void callback(String method, String responseData);
|
||||
}
|
||||
}
|
||||
|
||||
class BizContent extends HashMap<String, String> {
|
||||
public BizContent add(String key, String value) {
|
||||
this.put(key, value);
|
||||
@@ -264,11 +186,12 @@ public class AllInOneTest extends TestBase {
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertResult(String method, String responseData) {
|
||||
public static void assertResult(Client.RequestInfo requestInfo, String responseData) {
|
||||
System.out.println(responseData);
|
||||
String method = requestInfo.getMethod();
|
||||
if (method == null) {
|
||||
return;
|
||||
}
|
||||
System.out.println(responseData);
|
||||
String node = method.replace('.', '_') + "_response";
|
||||
JSONObject jsonObject = JSON.parseObject(responseData).getJSONObject(node);
|
||||
String code = Optional.ofNullable(jsonObject).map(json -> json.getString("code")).orElse("20000");
|
||||
|
Reference in New Issue
Block a user