1
1
package com .aliyuncs .auth ;
2
2
3
- import com .aliyuncs .exceptions .ClientException ;
4
- import com .aliyuncs .http .HttpRequest ;
5
- import com .aliyuncs .http .HttpResponse ;
6
- import com .aliyuncs .http .clients .CompatibleUrlConnClient ;
3
+ import static org .mockito .Matchers .any ;
4
+ import static org .mockito .Mockito .mock ;
5
+ import static org .mockito .Mockito .when ;
6
+
7
+ import java .io .IOException ;
8
+ import java .net .HttpURLConnection ;
9
+ import java .net .MalformedURLException ;
10
+ import java .net .URL ;
11
+
7
12
import org .junit .Assert ;
8
13
import org .junit .Rule ;
9
14
import org .junit .Test ;
14
19
import org .powermock .core .classloader .annotations .PrepareForTest ;
15
20
import org .powermock .modules .junit4 .PowerMockRunner ;
16
21
17
- import java .io .IOException ;
18
- import java .net .HttpURLConnection ;
19
- import java .net .MalformedURLException ;
20
- import java .net .URL ;
21
-
22
- import static org .mockito .Matchers .any ;
23
- import static org .mockito .Mockito .mock ;
24
- import static org .mockito .Mockito .when ;
22
+ import com .aliyuncs .exceptions .ClientException ;
23
+ import com .aliyuncs .http .HttpRequest ;
24
+ import com .aliyuncs .http .HttpResponse ;
25
+ import com .aliyuncs .http .clients .CompatibleUrlConnClient ;
25
26
26
27
@ RunWith (PowerMockRunner .class )
27
28
@ PrepareForTest (CompatibleUrlConnClient .class )
@@ -31,7 +32,8 @@ public class ECSMetadataServiceCredentialsFetcherTest {
31
32
32
33
@ Test
33
34
public void test () throws MalformedURLException {
34
- String url = "http://1abfwefwe/f\\ fwe000))00.100.100.200/latest/meta-data/ram/security-credentials//%%http:///\\ ```////~!@#$%^&*()_+|:<>?|MN" ;
35
+ String url = "http://1abfwefwe/f\\ fwe000))00.100.100.200/latest/meta-data/ram/security-credentials//"
36
+ + "%%http:///\\ ```////~!@#$%^&*()_+|:<>?|MN" ;
35
37
new URL (url );
36
38
}
37
39
@@ -77,8 +79,7 @@ public void getMetadataNormal() throws IOException, ClientException {
77
79
String content = "content" ;
78
80
when (response .getHttpContent ()).thenReturn (content .getBytes ());
79
81
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
80
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
81
- .willReturn (response );
82
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
82
83
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
83
84
fetcher .setRoleName ("role" );
84
85
String res = fetcher .getMetadata ();
@@ -89,8 +90,8 @@ public void getMetadataNormal() throws IOException, ClientException {
89
90
public void getMetadataThrowClientException1 () throws IOException , ClientException {
90
91
thrown .expect (ClientException .class );
91
92
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
92
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
93
- . willThrow ( new ClientException ("client exception" ));
93
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))). willThrow (
94
+ new ClientException ("client exception" ));
94
95
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
95
96
fetcher .setRoleName ("role" );
96
97
String res = fetcher .getMetadata ();
@@ -104,8 +105,7 @@ public void getMetadataThrowClientException2() throws IOException, ClientExcepti
104
105
String content = "content" ;
105
106
when (response .getHttpContent ()).thenReturn (content .getBytes ());
106
107
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
107
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
108
- .willReturn (response );
108
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
109
109
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
110
110
fetcher .setRoleName ("role" );
111
111
String res = fetcher .getMetadata ();
@@ -115,11 +115,11 @@ public void getMetadataThrowClientException2() throws IOException, ClientExcepti
115
115
public void fetchNormal () throws IOException , ClientException {
116
116
HttpResponse response = mock (HttpResponse .class );
117
117
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
118
- String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
118
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
119
+ + "\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
119
120
when (response .getHttpContent ()).thenReturn (content .getBytes ());
120
121
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
121
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
122
- .willReturn (response );
122
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
123
123
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
124
124
fetcher .setRoleName ("role" );
125
125
InstanceProfileCredentials credentials = fetcher .fetch ();
@@ -131,11 +131,11 @@ public void fetchThrowClientException1MissCode() throws IOException, ClientExcep
131
131
thrown .expect (ClientException .class );
132
132
HttpResponse response = mock (HttpResponse .class );
133
133
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
134
- String content = "{\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
134
+ String content = "{\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,"
135
+ + "\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
135
136
when (response .getHttpContent ()).thenReturn (content .getBytes ());
136
137
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
137
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
138
- .willReturn (response );
138
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
139
139
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
140
140
fetcher .setRoleName ("role" );
141
141
fetcher .fetch ();
@@ -146,11 +146,11 @@ public void fetchThrowClientException1MissAK() throws IOException, ClientExcepti
146
146
thrown .expect (ClientException .class );
147
147
HttpResponse response = mock (HttpResponse .class );
148
148
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
149
- String content = "{\" Code\" :\" Success\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
149
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,"
150
+ + "\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
150
151
when (response .getHttpContent ()).thenReturn (content .getBytes ());
151
152
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
152
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
153
- .willReturn (response );
153
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
154
154
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
155
155
fetcher .setRoleName ("role" );
156
156
fetcher .fetch ();
@@ -161,11 +161,11 @@ public void fetchThrowClientException1MissSK() throws IOException, ClientExcepti
161
161
thrown .expect (ClientException .class );
162
162
HttpResponse response = mock (HttpResponse .class );
163
163
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
164
- String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
164
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" SecurityToken\" :\" token\" ,"
165
+ + "\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
165
166
when (response .getHttpContent ()).thenReturn (content .getBytes ());
166
167
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
167
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
168
- .willReturn (response );
168
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
169
169
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
170
170
fetcher .setRoleName ("role" );
171
171
fetcher .fetch ();
@@ -176,11 +176,11 @@ public void fetchThrowClientException1MissToken() throws IOException, ClientExce
176
176
thrown .expect (ClientException .class );
177
177
HttpResponse response = mock (HttpResponse .class );
178
178
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
179
- String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
179
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
180
+ + "\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
180
181
when (response .getHttpContent ()).thenReturn (content .getBytes ());
181
182
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
182
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
183
- .willReturn (response );
183
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
184
184
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
185
185
fetcher .setRoleName ("role" );
186
186
fetcher .fetch ();
@@ -191,11 +191,11 @@ public void fetchThrowClientException1MissExpiration() throws IOException, Clien
191
191
thrown .expect (ClientException .class );
192
192
HttpResponse response = mock (HttpResponse .class );
193
193
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
194
- String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" }" ;
194
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
195
+ + "\" SecurityToken\" :\" token\" }" ;
195
196
when (response .getHttpContent ()).thenReturn (content .getBytes ());
196
197
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
197
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
198
- .willReturn (response );
198
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
199
199
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
200
200
fetcher .setRoleName ("role" );
201
201
fetcher .fetch ();
@@ -206,11 +206,11 @@ public void fetchThrowClientException2() throws IOException, ClientException {
206
206
thrown .expect (ClientException .class );
207
207
HttpResponse response = mock (HttpResponse .class );
208
208
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
209
- String content = "{\" Code\" :\" Failed\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
209
+ String content = "{\" Code\" :\" Failed\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
210
+ + "\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
210
211
when (response .getHttpContent ()).thenReturn (content .getBytes ());
211
212
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
212
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
213
- .willReturn (response );
213
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
214
214
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
215
215
fetcher .setRoleName ("role" );
216
216
fetcher .fetch ();
@@ -220,11 +220,11 @@ public void fetchThrowClientException2() throws IOException, ClientException {
220
220
public void fetch3TimesNormal () throws IOException , ClientException {
221
221
HttpResponse response = mock (HttpResponse .class );
222
222
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
223
- String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
223
+ String content = "{\" Code\" :\" Success\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
224
+ + "\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
224
225
when (response .getHttpContent ()).thenReturn (content .getBytes ());
225
226
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
226
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
227
- .willReturn (response );
227
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
228
228
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
229
229
fetcher .setRoleName ("role" );
230
230
InstanceProfileCredentials credentials = fetcher .fetch (3 );
@@ -236,11 +236,11 @@ public void fetch3TimesThrowClientException() throws IOException, ClientExceptio
236
236
thrown .expect (ClientException .class );
237
237
HttpResponse response = mock (HttpResponse .class );
238
238
when (response .getStatus ()).thenReturn (HttpURLConnection .HTTP_OK );
239
- String content = "{\" Code\" :\" Failed\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
239
+ String content = "{\" Code\" :\" Failed\" ,\" AccessKeyId\" :\" ak\" ,\" AccessKeySecret\" :\" sk\" ,"
240
+ + "\" SecurityToken\" :\" token\" ,\" Expiration\" :\" 2020-11-11 11:11:11\" }" ;
240
241
when (response .getHttpContent ()).thenReturn (content .getBytes ());
241
242
PowerMockito .mockStatic (CompatibleUrlConnClient .class );
242
- BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class )))
243
- .willReturn (response );
243
+ BDDMockito .given (CompatibleUrlConnClient .compatibleGetResponse (any (HttpRequest .class ))).willReturn (response );
244
244
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher ();
245
245
fetcher .setRoleName ("role" );
246
246
fetcher .fetch (3 );
0 commit comments