-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 完善测试用例 2. 提取微信请求基类,不同类型的请求初始化不同的子类
- Loading branch information
Showing
29 changed files
with
556 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 10 additions & 7 deletions
17
mars-demo/src/main/java/com/github/howwrite/mars/marsdemo/controller/MarsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
package com.github.howwrite.mars.marsdemo.controller; | ||
|
||
import com.github.howwrite.mars.sdk.request.MarsWxRequest; | ||
import com.github.howwrite.mars.sdk.response.BaseMarsWxResponse; | ||
import com.github.howwrite.mars.sdk.request.BaseMarsRequest; | ||
import com.github.howwrite.mars.sdk.request.MarsTextRequest; | ||
import com.github.howwrite.mars.sdk.response.BaseMarsResponse; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author howwrite | ||
* @Description demo controller | ||
* @create 2019/12/15 15:31 | ||
*/ | ||
@RestController | ||
@Controller | ||
public class MarsController { | ||
|
||
@PostMapping("/mars") | ||
public BaseMarsWxResponse helloMars(MarsWxRequest request) { | ||
|
||
return BaseMarsWxResponse.createTextResponse(request, "hello " + request.getContent()); | ||
public BaseMarsResponse helloMars(BaseMarsRequest request) { | ||
if (request instanceof MarsTextRequest) { | ||
return BaseMarsResponse.createTextResponse(request, "hello " + ((MarsTextRequest) request).getContent()); | ||
} | ||
return BaseMarsResponse.createTextResponse(request, "hello world"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
mars-demo/src/test/java/com/github/howwrite/mars/test/BaseMarsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.github.howwrite.mars.test; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
/** | ||
* @author howwrite | ||
* @date 2020/3/7 上午9:24:23 | ||
*/ | ||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@SpringBootTest(classes = MarsTestConfiguration.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles("test") | ||
public abstract class BaseMarsTest { | ||
} |
15 changes: 15 additions & 0 deletions
15
mars-demo/src/test/java/com/github/howwrite/mars/test/MarsTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.github.howwrite.mars.test; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @author howwrite | ||
* @date 2020/3/7 上午9:33:24 | ||
*/ | ||
@Configuration | ||
@ComponentScan | ||
@EnableAutoConfiguration | ||
public class MarsTestConfiguration { | ||
} |
24 changes: 24 additions & 0 deletions
24
mars-demo/src/test/java/com/github/howwrite/mars/test/controller/MarsTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.github.howwrite.mars.test.controller; | ||
|
||
import com.github.howwrite.mars.sdk.request.BaseMarsRequest; | ||
import com.github.howwrite.mars.sdk.request.MarsTextRequest; | ||
import com.github.howwrite.mars.sdk.response.BaseMarsResponse; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
|
||
/** | ||
* @author howwrite | ||
* @date 2020/3/7 上午9:34:07 | ||
*/ | ||
@Controller | ||
public class MarsTestController { | ||
|
||
@PostMapping("/testMars") | ||
public BaseMarsResponse handlerWxMessage(BaseMarsRequest request) { | ||
if (request instanceof MarsTextRequest) { | ||
return BaseMarsResponse.createTextResponse(request, ((MarsTextRequest) request).getContent() + " too"); | ||
} | ||
return null; | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
mars-demo/src/test/java/com/github/howwrite/mars/test/test/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.github.howwrite.mars.test.test; | ||
|
||
import com.github.howwrite.mars.test.BaseMarsTest; | ||
import okhttp3.*; | ||
import org.dom4j.Document; | ||
import org.dom4j.DocumentHelper; | ||
import org.dom4j.Element; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
|
||
/** | ||
* 模拟微信请求测试controller消息加解密 | ||
* | ||
* @author howwrite | ||
* @date 2020/3/7 上午9:42:09 | ||
*/ | ||
public class TestController extends BaseMarsTest { | ||
private static final Logger log = LoggerFactory.getLogger(TestController.class); | ||
private static final String REQUEST_PATH = "/testMars"; | ||
private static final MediaType XML_MEDIA_TYPE | ||
= MediaType.get("application/xml; charset=utf-8"); | ||
private OkHttpClient client = new OkHttpClient(); | ||
@LocalServerPort | ||
private int port; | ||
|
||
/** | ||
* 测试get请求验证签名 | ||
*/ | ||
@Test | ||
public void testCheckSign() throws Exception { | ||
String echostr = "abcdefg"; | ||
String parameters = "signature=d9ab76da6454d9a719af76994a2424a1b82162bd&nonce=384783965×tamp=1583549822&echostr=" + echostr; | ||
Request request = new Request.Builder() | ||
.url(getUrl() + "?" + parameters) | ||
.build(); | ||
try (Response response = client.newCall(request).execute()) { | ||
Assert.assertEquals(200, response.code()); | ||
String result = response.body().string(); | ||
log.debug("request result:[{}]", result); | ||
Assert.assertEquals(echostr, result.trim()); | ||
} | ||
} | ||
|
||
/** | ||
* 测试文本格式明文消息 | ||
*/ | ||
@Test | ||
public void testTextLaws() throws Exception { | ||
String body = "<xml><ToUserName><![CDATA[testToUserName]]></ToUserName><FromUserName><![CDATA[testFromUserName]]></FromUserName><CreateTime>123456</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[helloMars]]></Content><MsgId>1</MsgId></xml>"; | ||
try (Response response = client.newCall(createTextLawsRequest(body, "")).execute()) { | ||
Assert.assertEquals(200, response.code()); | ||
String result = response.body().string(); | ||
log.debug("request result:[{}]", result); | ||
Document document = DocumentHelper.parseText(result); | ||
Element rootElement = document.getRootElement(); | ||
String content = rootElement.elementText("Content"); | ||
String toUserName = rootElement.elementText("ToUserName"); | ||
String fromUserName = rootElement.elementText("FromUserName"); | ||
String msgType = rootElement.elementText("MsgType"); | ||
// 发送内容是helloMars, 断言结果是: 用户发送内容+ too | ||
Assert.assertEquals("helloMars too", content); | ||
Assert.assertEquals("testToUserName", fromUserName); | ||
Assert.assertEquals("testFromUserName", toUserName); | ||
Assert.assertEquals("text", msgType); | ||
} | ||
} | ||
|
||
/** | ||
* 测试文本格式兼容消息 | ||
*/ | ||
@Test | ||
public void testTextCompatible() throws Exception { | ||
String body = "<xml><ToUserName><![CDATA[testToUserName]]></ToUserName> <FromUserName><![CDATA[testFromUserName]]></FromUserName> <CreateTime>123456</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[helloMars]]></Content> <MsgId>1</MsgId> <Encrypt><![CDATA[Lmq5gxVEDHIdoEy3NUH86DVXkk2R0z4vCx9cL190d9Kntww43Bpjr3a4lXakn4G4ERpvAXuYYnulg97I5zuZ2bpeUr6au+znWt6tR5eedHZWnUyWLmZc0XX2Y/sK0uEHUtaFsYC9kwXqF/7HTQ1mJ8nT1OTyVtXCckYqHbL4xS0vvUDUqmEdyQEuhU56hjufqo6gzmKZCvz13BCjVqHFfWNV/2jGEtodwhl4fg6/9wxSjnqZXcUKX200jNioKGP43jTzZff7iZiVMuyaCApp7hA/XWRUBlFsrez2uVgSwQIEwddG2joCKxPEG4UEKKcP/8UJ9d9+rq41FdhMCh7pSa7prN5NkDhzIcsjqFC1Uj7IFi/P6OIUVxG64e7LWgDFf70evMsBtHEy78AZ/NK89zNMNeTXjmtxupM0MnwVUHtjCmnD5CZCkw9HlvqC7qdzGis2nGFgxta7CposUGGqSw==]]></Encrypt></xml>"; | ||
String parameters = "signature=f41c13f383640b9cb10c22cf2d0bc43994efd3c8×tamp=1583549370&nonce=423099419"; | ||
try (Response response = client.newCall(createTextLawsRequest(body, parameters)).execute()) { | ||
Assert.assertEquals(200, response.code()); | ||
String result = response.body().string(); | ||
Assert.assertNotNull(result); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* 测试文本格式加密消息 | ||
*/ | ||
@Test | ||
public void testTextEncryption() throws Exception { | ||
String body = "<xml> <ToUserName><![CDATA[testToUserName]]></ToUserName> <Encrypt><![CDATA[XmHu1XUAjxg5xqiwM8tjy2Stmm4q8lG8o44o7+0kjChEDQh98/VGu2z4qYkXICradNRp4bFk8gkaREqN9Tg3bVeRB4ulg5wh/0ywHs3ZoL7JlLYagEHeB/cnJvY/QSuRU2x1Fg4DZlQ8kLUTaR4eV6LHAgvWv2uZa+q8lODkeEx5+oHdt9Br8CziQ99L23vrHr5c8Q4tAWebS6slhh7J/hsFzTLn3RwB0H3v8EXHRm0WyAGlgxCML/D5MdLOydlTIc3TPk1HBG8p0qcxtE501nIzyyB+XPv5mkY4zXjsJ65u7JrSsvEhXprOsYFNluDOOrpO5J2tP/dlf8vCfxXdLJiekl5dqPxF2NFFMX/ORdud6OMXfAsAV9KnNI4R8sbERgC+zEBMx5uw0KcwVvOwBF36/lWn1gWUFbhAmUxH+UYwJPHYigVRiQdOmW+nGGqqfYYAJUPrgyulRqRdgxa7Ug==]]></Encrypt></xml>"; | ||
String parameters = "signature=069da1d26873136a1f7cd67e9f5dd3e868f9b67f×tamp=1583549661&nonce=2072430883"; | ||
try (Response response = client.newCall(createTextLawsRequest(body, parameters)).execute()) { | ||
Assert.assertEquals(200, response.code()); | ||
String result = response.body().string(); | ||
Assert.assertNotNull(result); | ||
} | ||
} | ||
|
||
private String getUrl() { | ||
return "http://localhost:" + port + REQUEST_PATH; | ||
} | ||
|
||
|
||
/** | ||
* 生成请求 | ||
*/ | ||
public Request createTextLawsRequest(String body, String parameters) { | ||
RequestBody requestBody = RequestBody.create(XML_MEDIA_TYPE, body); | ||
return new Request.Builder() | ||
.url(getUrl() + "?" + parameters) | ||
.post(requestBody) | ||
.build(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
mars-demo/src/test/java/com/github/howwrite/mars/test/test/TestWxUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.howwrite.mars.test.test; | ||
|
||
import com.github.howwrite.mars.sdk.config.MarsWxProperties; | ||
import com.github.howwrite.mars.sdk.utils.WxUtils; | ||
import com.github.howwrite.mars.sdk.utils.wx.aes.Sha1; | ||
import com.github.howwrite.mars.test.BaseMarsTest; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author howwrite | ||
* @date 2020/3/7 上午10:40:45 | ||
*/ | ||
public class TestWxUtil extends BaseMarsTest { | ||
|
||
@Resource | ||
private WxUtils wxUtils; | ||
|
||
@Resource | ||
private MarsWxProperties marsWxProperties; | ||
|
||
/** | ||
* 测试解密消息 | ||
*/ | ||
@Test | ||
public void testDecrypt() { | ||
Map<String, Object> decryptResultMap = wxUtils.parseXml("<xml> <ToUserName><![CDATA[testToUserName]]></ToUserName> <Encrypt><![CDATA[XmHu1XUAjxg5xqiwM8tjy2Stmm4q8lG8o44o7+0kjChEDQh98/VGu2z4qYkXICradNRp4bFk8gkaREqN9Tg3bVeRB4ulg5wh/0ywHs3ZoL7JlLYagEHeB/cnJvY/QSuRU2x1Fg4DZlQ8kLUTaR4eV6LHAgvWv2uZa+q8lODkeEx5+oHdt9Br8CziQ99L23vrHr5c8Q4tAWebS6slhh7J/hsFzTLn3RwB0H3v8EXHRm0WyAGlgxCML/D5MdLOydlTIc3TPk1HBG8p0qcxtE501nIzyyB+XPv5mkY4zXjsJ65u7JrSsvEhXprOsYFNluDOOrpO5J2tP/dlf8vCfxXdLJiekl5dqPxF2NFFMX/ORdud6OMXfAsAV9KnNI4R8sbERgC+zEBMx5uw0KcwVvOwBF36/lWn1gWUFbhAmUxH+UYwJPHYigVRiQdOmW+nGGqqfYYAJUPrgyulRqRdgxa7Ug==]]></Encrypt></xml>" | ||
, "069da1d26873136a1f7cd67e9f5dd3e868f9b67f", "1583549661", "2072430883" | ||
); | ||
Assert.assertEquals("helloMars", decryptResultMap.get("Content")); | ||
Assert.assertEquals("123456", decryptResultMap.get("CreateTime")); | ||
Assert.assertEquals(true, decryptResultMap.get("Encryption")); | ||
Assert.assertEquals("testToUserName", decryptResultMap.get("ToUserName")); | ||
Assert.assertEquals("testFromUserName", decryptResultMap.get("FromUserName")); | ||
Assert.assertEquals("text", decryptResultMap.get("MsgType")); | ||
Assert.assertEquals("1", decryptResultMap.get("MsgId")); | ||
} | ||
|
||
/** | ||
* 测试加密消息 | ||
*/ | ||
@Test | ||
public void testEncryption() throws Exception { | ||
String xmlString = "<xml><name>mars</name><Author>howwrite</Author></xml>"; | ||
String timeStamp = "1234567"; | ||
String nonce = "nonce"; | ||
String encryption = wxUtils.encryptMsg(xmlString, timeStamp, nonce); | ||
String decryptSignature = Sha1.getSha1(marsWxProperties.getToken(), timeStamp, nonce); | ||
|
||
// 解密加密的消息 | ||
Map<String, Object> decryptResult = wxUtils.parseXml(encryption, decryptSignature, timeStamp, nonce); | ||
Assert.assertEquals("mars", decryptResult.get("name")); | ||
Assert.assertEquals("howwrite", decryptResult.get("Author")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mars: | ||
path: /testMars | ||
weixin: | ||
token: testToken | ||
app-id: testAppId | ||
encoding-aes-key: abcdefghijabcdefghijabcdefghijabcdefghijabc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
mars/src/main/java/com/github/howwrite/mars/sdk/config/MarsConfiguration.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.