From db638dd8b16a2c99595d86f8ea698a3216fef42d Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Wed, 21 Aug 2019 14:02:44 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20#1087=20=E4=BF=AE=E5=A4=8D=E9=95=BF?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E8=BD=AC=E7=9F=AD=E9=93=BE=E6=8E=A5=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=AD=E5=90=AB=E6=9C=89=E7=89=B9=E6=AE=8A=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E5=AF=BC=E8=87=B4=E5=BE=AE=E4=BF=A1access=5Ftoken?= =?UTF-8?q?=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/mp/api/impl/BaseWxMpServiceImpl.java | 6 ++++++ .../weixin/mp/api/WxMpShortUrlAPITest.java | 15 +++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index 59a0efeb..42a84540 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -145,6 +145,12 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH @Override public String shortUrl(String longUrl) throws WxErrorException { + if (longUrl.contains("&access_token=")) { + throw new WxErrorException(WxError.builder().errorCode(-1) + .errorMsg("要转换的网址中存在非法字符{&access_token=},会导致微信接口报错,属于微信bug,请调整地址,否则不建议使用此方法!") + .build()); + } + JsonObject o = new JsonObject(); o.addProperty("action", "long2short"); o.addProperty("long_url", longUrl); diff --git a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpShortUrlAPITest.java b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpShortUrlAPITest.java index ec6e75d7..2117b984 100644 --- a/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpShortUrlAPITest.java +++ b/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpShortUrlAPITest.java @@ -6,21 +6,28 @@ import me.chanjar.weixin.mp.api.test.ApiTestModule; import org.testng.*; import org.testng.annotations.*; +import static org.assertj.core.api.Assertions.assertThat; + /** * 测试短连接 * * @author chanjarster */ -@Test(groups = "shortURLAPI") +@Test @Guice(modules = ApiTestModule.class) public class WxMpShortUrlAPITest { - @Inject protected WxMpService wxService; public void testShortUrl() throws WxErrorException { - String shortUrl = this.wxService.shortUrl("www.baidu.com"); - Assert.assertNotNull(shortUrl); + String shortUrl = this.wxService.shortUrl("http://www.baidu.com/test?access_token=123"); + assertThat(shortUrl).isNotEmpty(); + System.out.println(shortUrl); + } + + @Test(expectedExceptions = WxErrorException.class) + public void testShortUrl_with_exceptional_url() throws WxErrorException { + this.wxService.shortUrl("http://www.baidu.com/test?redirect_count=1&access_token=123"); } }