This commit is contained in:
tanghc
2020-07-28 18:21:01 +08:00
parent ee30f18d53
commit 0fea955db9
165 changed files with 918 additions and 4838 deletions

View File

@@ -41,7 +41,7 @@ public class AlipayClientPostTest extends TestBase {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "alipay.story.get");
params.put("method", "story.get");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");

View File

@@ -8,11 +8,10 @@ import org.junit.Assert;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 涵盖所有测试情况,发版前运行这个类,确保功能没有问题。
@@ -25,14 +24,14 @@ public class AllInOneTest extends TestBase {
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=";
private Client client = new Client(url, appId, privateKey, AllInOneTest::assertResult);
private final Client client = new Client(url, appId, privateKey, AllInOneTest::assertResult);
/**
* 以get方式提交
*/
public void testGet() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("alipay.story.get")
.method("story.get")
.version("1.0")
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
.httpMethod(HttpTool.HTTPMethod.GET);
@@ -45,7 +44,7 @@ public class AllInOneTest extends TestBase {
*/
public void testPostForm() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("alipay.story.get")
.method("story.get")
.version("1.0")
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
.httpMethod(HttpTool.HTTPMethod.POST);
@@ -58,7 +57,7 @@ public class AllInOneTest extends TestBase {
*/
public void testPostJSON() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("alipay.story.get")
.method("story.get")
.version("1.0")
// 以json方式提交
.postJson(true)
@@ -68,17 +67,14 @@ public class AllInOneTest extends TestBase {
}
/**
* 测试遗留接口
* 以json方式提交(application/json)
* 以get方式提交
*/
public void testPostJSON2() {
public void testGet_v2() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("getStory33")
.version("1.0")
// 以json方式提交
.postJson(true)
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
.httpMethod(HttpTool.HTTPMethod.POST);
.method("story.get")
.version("2.0")
.bizContent(new BizContent().add("id", "2").add("name", "葫芦娃2"))
.httpMethod(HttpTool.HTTPMethod.GET);
client.execute(requestBuilder);
}
@@ -88,8 +84,8 @@ public class AllInOneTest extends TestBase {
*/
public void testIgnoreSign() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.get")
.version("2.1")
.method("story.get.ignore")
.version("1.0")
.ignoreSign(true)
.bizContent(new BizContent().add("id", "222").add("name", "忽略222"))
.httpMethod(HttpTool.HTTPMethod.GET);
@@ -97,39 +93,6 @@ public class AllInOneTest extends TestBase {
client.execute(requestBuilder);
}
/**
* OpenContext参数绑定
*/
public void testOpenContext() {
Client client = new Client(url, appId, privateKey);
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.get")
.version("2.2")
.bizContent(new BizContent().add("id", "222").add("name", "openContext"))
.httpMethod(HttpTool.HTTPMethod.GET)
.callback((requestInfo, responseData) -> {
System.out.println(responseData);
JSONObject jsonObject = JSON.parseObject(responseData);
String name = jsonObject.getJSONObject(requestInfo.getDataNode()).getString("name");
Assert.assertEquals(name, "appId:" + appId + ", openContext");
});
client.execute(requestBuilder);
}
/**
* 其它参数绑定
*/
public void testOtherParam() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.get")
.version("2.3")
.bizContent(new BizContent().add("id", "222").add("name", "request param"))
.httpMethod(HttpTool.HTTPMethod.GET);
client.execute(requestBuilder);
}
/**
* JSR-303参数校验
*/
@@ -155,10 +118,16 @@ public class AllInOneTest extends TestBase {
*/
public void testPermission() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("permission.story.get")
.method("story.get.permission")
.version("1.0")
.bizContent(new BizContent())
.httpMethod(HttpTool.HTTPMethod.GET);
.httpMethod(HttpTool.HTTPMethod.GET)
.callback((requestInfo, responseData) -> {
System.out.println(responseData);
String node = requestInfo.getDataNode();
JSONObject jsonObject = JSON.parseObject(responseData).getJSONObject(node);
Assert.assertEquals("isv.route-no-permissions", jsonObject.getString("sub_code"));
});
client.execute(requestBuilder);
}
@@ -170,7 +139,7 @@ public class AllInOneTest extends TestBase {
Client client = new Client(url, appId, privateKey);
String root = System.getProperty("user.dir");
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("demo.file.upload")
.method("file.upload")
.version("1.0")
.bizContent(new BizContent().add("remark", "test file upload"))
// 添加文件
@@ -195,7 +164,7 @@ public class AllInOneTest extends TestBase {
Client client = new Client(url, appId, privateKey);
String root = System.getProperty("user.dir");
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("demo.file.upload2")
.method("file.upload2")
.version("1.0")
.bizContent(new BizContent().add("remark", "test file upload"))
// 添加文件
@@ -219,7 +188,7 @@ public class AllInOneTest extends TestBase {
Client client = new Client(url, appId, privateKey);
String root = System.getProperty("user.dir");
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("demo.file.upload3")
.method("file.upload3")
.version("1.0")
.bizContent(new BizContent().add("remark", "test file upload"))
// 添加文件
@@ -240,79 +209,21 @@ public class AllInOneTest extends TestBase {
*/
public void testDownloadFile() throws IOException {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.download")
.method("file.download")
.version("1.0")
.bizContent(new BizContent().add("id",1).add("name","Jim"))
.httpMethod(HttpTool.HTTPMethod.GET);
// 文件流
InputStream download = client.download(requestBuilder);
String content = IOUtils.toString(download, "UTF-8");
String content = IOUtils.toString(download, StandardCharsets.UTF_8);
System.out.println("下载文件内容:" + content);
Assert.assertEquals(content, "spring.profiles.active=dev");
}
/**
* 验证中文乱码问题
*/
public void testString() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.string.get")
.version("1.0")
.bizContent(new BizContent().add("name", "name111"))
.httpMethod(HttpTool.HTTPMethod.GET)
.callback((requestInfo, responseData) -> {
JSONObject jsonObject = JSON.parseObject(responseData);
JSONObject data = jsonObject.getJSONObject("story_string_get_response");
Assert.assertEquals("海底小纵队", data.getString("name"));
});
client.execute(requestBuilder);
}
/**
* 限流测试根据路由id限流
*
* @throws InterruptedException
*/
public void testLimit() throws InterruptedException {
int threadsCount = 10; // threadsCount个线程同时提交
final CountDownLatch countDownLatch = new CountDownLatch(1);
final CountDownLatch count = new CountDownLatch(threadsCount);
final AtomicInteger success = new AtomicInteger();
for (int i = 0; i < threadsCount; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await(); // 等在这里执行countDownLatch.countDown();集体触发
Map<String, String> header = new HashMap<>(4);
header.put("Accept-Language", "en-US");
// 业务方法
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("alipay.story.get")
.version("1.2")
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
//.header(header)
.httpMethod(HttpTool.HTTPMethod.GET);
client.execute(requestBuilder);
success.incrementAndGet();
} catch (Exception e) {
} finally {
count.countDown();
}
}
}).start();
}
countDownLatch.countDown();
count.await();
System.out.println("成功次数:" + success);
}
public void testToken() {
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("story.token.get")
.method("story.get.token")
.version("1.0")
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
.appAuthToken("asdfasdfadsf")
@@ -329,7 +240,7 @@ public class AllInOneTest extends TestBase {
Map<String, String> header = new HashMap<>(4);
header.put("Accept-Language", "en-US");
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
.method("alipay.story.get9")
.method("story.get==") // 制造错误的接口名
.version("1.0")
.header(header)
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
@@ -344,7 +255,7 @@ public class AllInOneTest extends TestBase {
client.execute(requestBuilder);
}
class BizContent extends HashMap<String, Object> {
static class BizContent extends HashMap<String, Object> {
public BizContent add(String key, Object value) {
this.put(key, value);
return this;

View File

@@ -25,7 +25,7 @@ public class PermissionDemoPostTest extends TestBase {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "permission.story.get");
params.put("method", "story.get.permission");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");