1
0
mirror of synced 2025-12-23 02:18:07 +08:00

🎨 #2115 优化公众号、小程序、企业微信的access token自动刷新逻辑,避免循环递归调用

This commit is contained in:
hywr
2021-05-20 14:23:26 +08:00
committed by GitHub
parent dfb02eac38
commit 62a3931aee
8 changed files with 200 additions and 28 deletions

View File

@@ -2,14 +2,25 @@ package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.error.WxMpErrorMsgEnum;
import me.chanjar.weixin.common.util.http.RequestExecutor;
import org.apache.commons.lang3.StringUtils;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
@@ -101,6 +112,35 @@ public class WxMaServiceImplTest {
public void testExecute() {
}
@Test
public void testExecuteAutoRefreshToken() throws WxErrorException, IOException {
//测试access token获取时的重试机制
WxMaServiceImpl service = new WxMaServiceImpl() {
@Override
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
return "模拟一个过期的access token:" + System.currentTimeMillis();
}
};
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid("1");
service.setWxMaConfig(config);
RequestExecutor<Object, Object> re = mock(RequestExecutor.class);
AtomicInteger counter = new AtomicInteger();
Mockito.when(re.execute(Mockito.anyString(), Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
counter.incrementAndGet();
WxError error = WxError.builder().errorCode(WxMpErrorMsgEnum.CODE_40001.getCode()).errorMsg(WxMpErrorMsgEnum.CODE_40001.getMsg()).build();
throw new WxErrorException(error);
});
try {
Object execute = service.execute(re, "http://baidu.com", new HashMap<>());
Assert.assertTrue(false, "代码应该不会执行到这里");
} catch (WxErrorException e) {
Assert.assertEquals(WxMpErrorMsgEnum.CODE_40001.getCode(), e.getError().getErrorCode());
Assert.assertEquals(2, counter.get());
}
}
@Test
public void testGetWxMaConfig() {
}