1
1
package com .aliyuncs ;
2
2
3
+ import static org .mockito .Matchers .any ;
4
+
3
5
import java .io .IOException ;
4
6
import java .lang .reflect .Field ;
5
7
import java .net .SocketTimeoutException ;
6
8
import java .security .InvalidKeyException ;
7
9
import java .security .NoSuchAlgorithmException ;
8
10
9
- import com .aliyuncs .http .*;
10
11
import org .junit .Assert ;
11
12
import org .junit .Rule ;
12
13
import org .junit .Test ;
13
14
import org .junit .rules .ExpectedException ;
14
15
import org .junit .runner .RunWith ;
16
+ import org .mockito .BDDMockito ;
15
17
import org .mockito .Mockito ;
16
- import org .mockito . runners . MockitoJUnitRunner ;
18
+ import org .powermock . api . mockito . PowerMockito ;
17
19
import org .powermock .core .classloader .annotations .PrepareForTest ;
20
+ import org .powermock .modules .junit4 .PowerMockRunner ;
18
21
19
22
import com .aliyuncs .auth .Credential ;
20
23
import com .aliyuncs .auth .LegacyCredentials ;
26
29
import com .aliyuncs .exceptions .ErrorCodeConstant ;
27
30
import com .aliyuncs .exceptions .ErrorMessageConstant ;
28
31
import com .aliyuncs .exceptions .ServerException ;
32
+ import com .aliyuncs .http .FormatType ;
33
+ import com .aliyuncs .http .HttpClientConfig ;
34
+ import com .aliyuncs .http .HttpRequest ;
35
+ import com .aliyuncs .http .HttpResponse ;
36
+ import com .aliyuncs .http .HttpUtil ;
37
+ import com .aliyuncs .http .IHttpClient ;
38
+ import com .aliyuncs .http .ProtocolType ;
39
+ import com .aliyuncs .http .UserAgentConfig ;
29
40
import com .aliyuncs .http .clients .CompatibleUrlConnClient ;
30
41
import com .aliyuncs .profile .DefaultProfile ;
31
42
import com .aliyuncs .regions .ProductDomain ;
32
43
33
- @ RunWith (MockitoJUnitRunner .class )
34
- @ PrepareForTest (HttpClientFactory .class )
44
+ @ RunWith (PowerMockRunner .class )
45
+ @ PrepareForTest (HttpUtil .class )
35
46
public class DefaultAcsClientTest {
36
47
@ Rule
37
48
public ExpectedException thrown = ExpectedException .none ();
@@ -104,7 +115,7 @@ public void testShutdown() {
104
115
105
116
@ SuppressWarnings ("deprecation" )
106
117
private DefaultAcsClient initDefaultAcsClient () throws NoSuchFieldException , SecurityException ,
107
- IllegalArgumentException , IllegalAccessException {
118
+ IllegalArgumentException , IllegalAccessException , ClientException {
108
119
Credential credential = Mockito .mock (Credential .class );
109
120
Mockito .when (credential .getSecurityToken ()).thenReturn (null );
110
121
DefaultProfile profile = Mockito .mock (DefaultProfile .class );
@@ -117,7 +128,9 @@ private DefaultAcsClient initDefaultAcsClient() throws NoSuchFieldException, Sec
117
128
HttpClientConfig httpClientConfig = Mockito .mock (HttpClientConfig .class );
118
129
Mockito .when (httpClientConfig .getProtocolType ()).thenReturn (ProtocolType .HTTP );
119
130
Mockito .when (profile .getHttpClientConfig ()).thenReturn (httpClientConfig );
120
-
131
+ PowerMockito .mockStatic (HttpUtil .class );
132
+ BDDMockito .given (HttpUtil .debugHttpRequest (any (HttpRequest .class ))).willReturn (null );
133
+ BDDMockito .given (HttpUtil .debugHttpResponse (any (HttpResponse .class ))).willReturn (null );
121
134
return client ;
122
135
}
123
136
@@ -153,7 +166,7 @@ private HttpResponse initHttpResponse() throws ClientException {
153
166
154
167
@ Test
155
168
public void testRestoreSSLCertificate () throws NoSuchFieldException , SecurityException , IllegalArgumentException ,
156
- IllegalAccessException {
169
+ IllegalAccessException , ClientException {
157
170
DefaultAcsClient client = initDefaultAcsClient ();
158
171
client .restoreSSLCertificate ();
159
172
client .ignoreSSLCertificate ();
@@ -171,6 +184,7 @@ public void testGetAcsResponse() throws NoSuchFieldException, SecurityException,
171
184
Mockito .doReturn ("endpoint" ).when (endpointResolver ).resolve (Mockito .any (ResolveEndpointRequest .class ));
172
185
HttpResponse response = initHttpResponse ();
173
186
Mockito .doReturn (response ).when (getHttpClient (client )).syncInvoke (Mockito .any (HttpRequest .class ));
187
+ Mockito .doReturn ("http://test.domain" ).when (response ).getSysUrl ();
174
188
AcsRequest request = initRequest (DescribeEndpointsResponse .class );
175
189
176
190
Assert .assertTrue (client .getAcsResponse (request ) instanceof DescribeEndpointsResponse );
@@ -194,6 +208,7 @@ public void testDoActionBranch() throws ClientException, IOException, IllegalArg
194
208
DefaultAcsClient client = initDefaultAcsClient ();
195
209
HttpResponse response = Mockito .mock (HttpResponse .class );
196
210
Mockito .doReturn (response ).when (getHttpClient (client )).syncInvoke (Mockito .any (HttpRequest .class ));
211
+ Mockito .doReturn ("http://test.domain" ).when (response ).getSysUrl ();
197
212
DefaultEndpointResolver endpointResolver = Mockito .mock (DefaultEndpointResolver .class );
198
213
client .setEndpointResolver (endpointResolver );
199
214
Mockito .doReturn ("endpoint" ).when (endpointResolver ).resolve (Mockito .any (ResolveEndpointRequest .class ));
@@ -306,6 +321,7 @@ public void testGetAcsResponseInvalidServerResponseException() throws NoSuchFiel
306
321
Mockito .when (response .isSuccess ()).thenReturn (true );
307
322
Mockito .when (response .getHttpContentType ()).thenReturn (FormatType .XML );
308
323
Mockito .when (response .getHttpContentString ()).thenReturn (null );
324
+ Mockito .doReturn ("http://test.domain" ).when (response ).getSysUrl ();
309
325
Mockito .doReturn (response ).when (getHttpClient (client )).syncInvoke (Mockito .any (HttpRequest .class ));
310
326
AcsRequest request = initRequest (DescribeEndpointsResponse .class );
311
327
@@ -612,7 +628,8 @@ public void userAgentConfigTest() {
612
628
client .appendUserAgent ("test" , "1.2.3" );
613
629
client .appendUserAgent ("order" , "1.2.2" );
614
630
String userAgent = UserAgentConfig .resolve (null , client .getUserAgentConfig ());
615
- String resultStr = UserAgentConfig .getDefaultMessage () + " Client/CompatibleUrlConnClient test/1.2.3 order/1.2.2" ;
631
+ String resultStr = UserAgentConfig .getDefaultMessage ()
632
+ + " Client/CompatibleUrlConnClient test/1.2.3 order/1.2.2" ;
616
633
Assert .assertEquals (resultStr , userAgent );
617
634
}
618
635
}
0 commit comments