mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
新增restful模式
This commit is contained in:
@@ -12,6 +12,7 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -268,6 +269,21 @@ public class Client {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置请求头
|
||||
*
|
||||
* @param key 请求头key
|
||||
* @param value 请求头value
|
||||
* @return 返回RequestBuilder
|
||||
*/
|
||||
public RequestBuilder header(String key, String value) {
|
||||
if (this.header == null) {
|
||||
this.header = new LinkedHashMap<>(8);
|
||||
}
|
||||
header.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否忽略签名验证
|
||||
*
|
||||
|
@@ -2,6 +2,7 @@ package com.gitee.sop.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.gitee.sop.test.sign.AlipaySignature;
|
||||
import okhttp3.internal.http2.Header;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -77,8 +78,8 @@ public class AlipayClientPostTest extends TestBase {
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
params.put("method", "story.get");
|
||||
params.put("format", "xml"); // xml
|
||||
params.put("method", "pay.trade.wap.pay");
|
||||
params.put("format", "xml");
|
||||
params.put("charset", "utf-8");
|
||||
params.put("sign_type", "RSA2");
|
||||
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
@@ -86,44 +87,10 @@ public class AlipayClientPostTest extends TestBase {
|
||||
|
||||
// 业务参数
|
||||
Map<String, Object> bizContent = new HashMap<>();
|
||||
bizContent.put("id", "1");
|
||||
bizContent.put("name", "葫芦娃");
|
||||
|
||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
params.put("sign", sign);
|
||||
|
||||
System.out.println("----------- 请求信息 -----------");
|
||||
System.out.println("请求参数:" + buildParamQuery(params));
|
||||
System.out.println("商户秘钥:" + privateKey);
|
||||
System.out.println("待签名内容:" + content);
|
||||
System.out.println("签名(sign):" + sign);
|
||||
System.out.println("URL参数:" + buildUrlQuery(params));
|
||||
|
||||
System.out.println("----------- 返回结果 -----------");
|
||||
String responseData = postJson(url, params);// 发送请求
|
||||
// <ApiResponse><code>0</code><msg>success</msg><sub_code></sub_code><sub_msg></sub_msg><data><name>乌鸦喝水</name><id>1</id></data></ApiResponse>
|
||||
System.out.println(responseData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFind() throws Exception {
|
||||
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
params.put("method", "story.find");
|
||||
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("version", "1.0");
|
||||
|
||||
// 业务参数
|
||||
Map<String, Object> bizContent = new HashMap<>();
|
||||
bizContent.put("id", "122");
|
||||
bizContent.put("name", "葫芦娃");
|
||||
bizContent.put("outTradeNo", "70501111111S001111119");
|
||||
bizContent.put("totalAmount", "9.00");
|
||||
bizContent.put("subject", "衣服");
|
||||
bizContent.put("productCode", "QUICK_WAP_WAY");
|
||||
|
||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
@@ -143,12 +110,12 @@ public class AlipayClientPostTest extends TestBase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSave() throws Exception {
|
||||
public void testSearch() throws Exception {
|
||||
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
params.put("method", "story.save");
|
||||
params.put("method", "pay.order.search");
|
||||
params.put("format", "json");
|
||||
params.put("charset", "utf-8");
|
||||
params.put("sign_type", "RSA2");
|
||||
@@ -157,8 +124,7 @@ public class AlipayClientPostTest extends TestBase {
|
||||
|
||||
// 业务参数
|
||||
Map<String, Object> bizContent = new HashMap<>();
|
||||
bizContent.put("storyName", "小猫钓鱼");
|
||||
bizContent.put("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
bizContent.put("orderNo", "70501111111S001111119");
|
||||
|
||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
@@ -173,45 +139,10 @@ public class AlipayClientPostTest extends TestBase {
|
||||
System.out.println("URL参数:" + buildUrlQuery(params));
|
||||
|
||||
System.out.println("----------- 返回结果 -----------");
|
||||
String responseData = postJson(url, params);// 发送请求
|
||||
Header[] header = {new Header("open-env", "gray")};
|
||||
// Header[] header = new Header[0];
|
||||
String responseData = postJson(url, params, header);// 发送请求
|
||||
System.out.println(responseData);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() throws Exception {
|
||||
|
||||
// 公共请求参数
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("app_id", appId);
|
||||
params.put("method", "story.update");
|
||||
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("version", "1.0");
|
||||
|
||||
// 业务参数
|
||||
Map<String, Object> bizContent = new HashMap<>();
|
||||
bizContent.put("id", "1");
|
||||
bizContent.put("storyName", "花仙子");
|
||||
bizContent.put("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
params.put("biz_content", JSON.toJSONString(bizContent));
|
||||
String content = AlipaySignature.getSignContent(params);
|
||||
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
|
||||
params.put("sign", sign);
|
||||
|
||||
System.out.println("----------- 请求信息 -----------");
|
||||
System.out.println("请求参数:" + buildParamQuery(params));
|
||||
System.out.println("商户秘钥:" + privateKey);
|
||||
System.out.println("待签名内容:" + content);
|
||||
System.out.println("签名(sign):" + sign);
|
||||
System.out.println("URL参数:" + buildUrlQuery(params));
|
||||
|
||||
System.out.println("----------- 返回结果 -----------");
|
||||
String responseData = postJson(url, params);// 发送请求
|
||||
System.out.println(responseData);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -34,7 +34,21 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testGet() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get")
|
||||
.method("product.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以get方式提交
|
||||
*/
|
||||
public void testGetGray() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("product.get")
|
||||
.header("open-env", "gray")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
@@ -47,9 +61,9 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testPostForm() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get")
|
||||
.method("product.get")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "彩电"))
|
||||
.httpMethod(HttpTool.HTTPMethod.POST);
|
||||
|
||||
client.execute(requestBuilder);
|
||||
@@ -60,11 +74,11 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testPostJSON() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get")
|
||||
.method("product.get")
|
||||
.version("1.0")
|
||||
// 以json方式提交
|
||||
.postJson(true)
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"));
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "彩电"));
|
||||
|
||||
client.execute(requestBuilder);
|
||||
}
|
||||
@@ -79,12 +93,12 @@ public class AllInOneTest extends TestBase {
|
||||
header.put("Accept-Language", "en-US");
|
||||
// header.put("Accept-Language", "zh-CN");
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.updateError")
|
||||
.method("product.updateError")
|
||||
.version("1.0")
|
||||
.header(header)
|
||||
// 以json方式提交
|
||||
.postJson(true)
|
||||
.bizContent(new BizContent().add("id", "0").add("name", "葫芦娃"))
|
||||
.bizContent(new BizContent().add("id", "0").add("name", "彩电"))
|
||||
.callback((requestInfo, responseData) -> {
|
||||
System.out.println(responseData);
|
||||
JSONObject jsonObject = JSON.parseObject(responseData);
|
||||
@@ -100,9 +114,9 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testGet_v2() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get")
|
||||
.method("product.get")
|
||||
.version("2.0")
|
||||
.bizContent(new BizContent().add("id", "2").add("name", "葫芦娃2"))
|
||||
.bizContent(new BizContent().add("id", "2").add("name", "电脑"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET)
|
||||
.callback((requestInfo, responseData) -> {
|
||||
System.out.println(responseData);
|
||||
@@ -118,10 +132,10 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testGetContext() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get.context")
|
||||
.method("product.get.context")
|
||||
.version("1.0")
|
||||
.notifyUrl("http://www.baidu.com")
|
||||
.bizContent(new BizContent().add("id", "2").add("name", "葫芦娃2"))
|
||||
.bizContent(new BizContent().add("id", "2").add("name", "电脑"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET)
|
||||
.callback((requestInfo, responseData) -> {
|
||||
System.out.println(responseData);
|
||||
@@ -138,7 +152,7 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testPermission() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.get.permission")
|
||||
.method("product.get.permission")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent())
|
||||
.httpMethod(HttpTool.HTTPMethod.GET)
|
||||
@@ -159,10 +173,10 @@ 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("story.upload")
|
||||
.method("product.upload")
|
||||
.version("1.0")
|
||||
.bizContent(
|
||||
new BizContent().add("storyName", "白雪公主")
|
||||
new BizContent().add("productName", "冰箱")
|
||||
.add("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
)
|
||||
// 添加文件
|
||||
@@ -181,10 +195,10 @@ 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("story.upload.more")
|
||||
.method("product.upload.more")
|
||||
.version("1.0")
|
||||
.bizContent(
|
||||
new BizContent().add("storyName", "白雪公主")
|
||||
new BizContent().add("productName", "冰箱")
|
||||
.add("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
)
|
||||
// 添加文件
|
||||
@@ -203,10 +217,10 @@ public class AllInOneTest extends TestBase {
|
||||
public void testFileList() {
|
||||
String root = System.getProperty("user.dir");
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.upload.list")
|
||||
.method("product.upload.list")
|
||||
.version("1.0")
|
||||
.bizContent(
|
||||
new BizContent().add("storyName", "白雪公主")
|
||||
new BizContent().add("productName", "冰箱")
|
||||
.add("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
)
|
||||
// 添加文件
|
||||
@@ -223,7 +237,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("story.upload")
|
||||
.method("product.upload")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("remark", "test file upload"))
|
||||
// 添加文件, 这个文件1.5M,上传会报错
|
||||
@@ -243,10 +257,10 @@ public class AllInOneTest extends TestBase {
|
||||
public void testFileMaxSize() {
|
||||
String root = System.getProperty("user.dir");
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.upload.list")
|
||||
.method("product.upload.list")
|
||||
.version("1.0")
|
||||
.bizContent(
|
||||
new BizContent().add("storyName", "白雪公主")
|
||||
new BizContent().add("productName", "冰箱")
|
||||
.add("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
)
|
||||
.callback((requestInfo, responseData) -> {
|
||||
@@ -274,7 +288,7 @@ public class AllInOneTest extends TestBase {
|
||||
File file = new File(root + "/src/main/resources/large_data.txt");
|
||||
String fileContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.find")
|
||||
.method("product.find")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", 111))
|
||||
.bizContent(new BizContent().add("name", fileContent))
|
||||
@@ -288,7 +302,7 @@ public class AllInOneTest extends TestBase {
|
||||
*/
|
||||
public void testDownloadFile() throws IOException {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.download")
|
||||
.method("product.download")
|
||||
.version("1.0")
|
||||
.bizContent(new BizContent().add("id", 1))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET);
|
||||
@@ -302,10 +316,10 @@ public class AllInOneTest extends TestBase {
|
||||
|
||||
public void testToken() {
|
||||
Client.RequestBuilder requestBuilder = new Client.RequestBuilder()
|
||||
.method("story.save")
|
||||
.method("product.save")
|
||||
.version("1.0")
|
||||
.bizContent(
|
||||
new BizContent().add("storyName", "白雪公主")
|
||||
new BizContent().add("productName", "冰箱")
|
||||
.add("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()))
|
||||
)
|
||||
.appAuthToken("asdfasdfadsf")
|
||||
@@ -322,10 +336,10 @@ 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("story.get==") // 制造错误的接口名
|
||||
.method("product.get==") // 制造错误的接口名
|
||||
.version("1.0")
|
||||
.header(header)
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "葫芦娃"))
|
||||
.bizContent(new BizContent().add("id", "1").add("name", "彩电"))
|
||||
.httpMethod(HttpTool.HTTPMethod.GET)
|
||||
.callback((requestInfo, responseData) -> {
|
||||
System.out.println(responseData);
|
||||
|
@@ -2,12 +2,14 @@ package com.gitee.sop.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import junit.framework.TestCase;
|
||||
import okhttp3.internal.http2.Header;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -31,9 +33,13 @@ public class TestBase extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public static String postJson(String url, Map<String, String> params) {
|
||||
public static String postJson(String url, Map<String, String> params, Header... headers) {
|
||||
try {
|
||||
return httpTool.requestJson(url, JSON.toJSONString(params), Collections.emptyMap());
|
||||
Map<String, String> headerMap = new LinkedHashMap<>();
|
||||
for (Header header : headers) {
|
||||
headerMap.put(header.name.utf8(), header.value.utf8());
|
||||
}
|
||||
return httpTool.requestJson(url, JSON.toJSONString(params), headerMap);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("网络请求异常", e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user