44import static org .testng .Assert .assertNotNull ;
55import static org .testng .Assert .assertSame ;
66
7+ import java .io .ByteArrayOutputStream ;
8+ import java .io .IOException ;
9+ import java .io .InputStream ;
710import me .chanjar .weixin .channel .bean .kf .WxChannelKfSendMsgParam ;
811import me .chanjar .weixin .channel .bean .kf .WxChannelKfSendMsgResponse ;
912import me .chanjar .weixin .channel .util .JsonUtils ;
1013import me .chanjar .weixin .common .bean .CommonUploadParam ;
1114import me .chanjar .weixin .common .error .WxErrorException ;
15+ import me .chanjar .weixin .common .util .http .RequestExecutor ;
1216import org .testng .annotations .Test ;
1317
1418/** 商家客服服务离线测试。 */
@@ -49,10 +53,19 @@ public void shouldSendJsonMessageAndDecodeResponse() throws WxErrorException {
4953 assertEquals (channelService .postUrl , "https://api.weixin.qq.com/channels/ec/commkf/sendmsg" );
5054 assertEquals (channelService .postJson ,
5155 "{\" request_id\" :\" request-id\" ,\" open_id\" :\" open-id\" ,\" msg_type\" :\" text\" ,\" text\" :{\" content\" :\" hello\" }}" );
56+ assertEquals (channelService .executeWithoutLogCalled , true );
5257 assertEquals (response .getMsgId (), "message-id" );
5358 assertEquals (response .getErrCode (), 0 );
5459 }
5560
61+ @ Test
62+ public void shouldProvideFreshUploadStreamForEachAttempt () throws IOException {
63+ CommonUploadParam uploadParam = CommonUploadParam .fromBytes ("file" , "image.png" , new byte []{1 , 2 , 3 });
64+
65+ assertEquals (readAllBytes (uploadParam .getData ().getInputStream ()), new byte []{1 , 2 , 3 });
66+ assertEquals (readAllBytes (uploadParam .getData ().getInputStream ()), new byte []{1 , 2 , 3 });
67+ }
68+
5669 @ Test
5770 public void shouldCacheKfServiceEntryPoint () {
5871 WxChannelServiceImpl channelService = new WxChannelServiceImpl ();
@@ -68,6 +81,7 @@ private static class RecordingChannelService extends WxChannelServiceImpl {
6881 private CommonUploadParam uploadParam ;
6982 private String postUrl ;
7083 private String postJson ;
84+ private boolean executeWithoutLogCalled ;
7185
7286 @ Override
7387 public String upload (String url , CommonUploadParam param ) {
@@ -82,5 +96,25 @@ public String post(String url, Object obj) {
8296 this .postJson = JsonUtils .encode (obj );
8397 return postResult ;
8498 }
99+
100+ @ Override
101+ @ SuppressWarnings ("unchecked" )
102+ public <T , E > T executeWithoutLog (RequestExecutor <T , E > executor , String uri , E data ) {
103+ this .executeWithoutLogCalled = true ;
104+ this .postUrl = uri ;
105+ this .postJson = (String ) data ;
106+ return (T ) postResult ;
107+ }
108+ }
109+
110+ private byte [] readAllBytes (InputStream inputStream ) throws IOException {
111+ try (InputStream stream = inputStream ; ByteArrayOutputStream outputStream = new ByteArrayOutputStream ()) {
112+ byte [] buffer = new byte [16 ];
113+ int count ;
114+ while ((count = stream .read (buffer )) != -1 ) {
115+ outputStream .write (buffer , 0 , count );
116+ }
117+ return outputStream .toByteArray ();
118+ }
85119 }
86120}
0 commit comments