4
4
import com .aliyuncs .ecs .model .v20140526 .DescribeInstancesResponse ;
5
5
import com .aliyuncs .exceptions .ClientException ;
6
6
import com .aliyuncs .http .HttpClientConfig ;
7
+ import com .aliyuncs .http .HttpResponse ;
7
8
import com .aliyuncs .http .clients .ApacheHttpClient ;
8
9
import com .aliyuncs .profile .DefaultProfile ;
9
10
import com .aliyuncs .profile .IClientProfile ;
10
11
import org .junit .Assert ;
12
+ import org .junit .Ignore ;
11
13
import org .junit .Rule ;
12
14
import org .junit .Test ;
13
15
import org .junit .rules .ExpectedException ;
@@ -22,8 +24,8 @@ private DefaultAcsClient getRightApacheProxyClientWithRegionId(String regionId)
22
24
ApacheHttpClient .getInstance (HttpClientConfig .getDefault ()).close ();
23
25
IClientProfile profile = DefaultProfile .getProfile (regionId , accesskeyId , accesskeySecret );
24
26
HttpClientConfig clientConfig = HttpClientConfig .getDefault ();
25
- clientConfig .setHttpProxy ("http://localhost:8989" );
26
- clientConfig .setHttpsProxy ("http://localhost:8989" );
27
+ clientConfig .setHttpProxy ("http://test:test@ localhost:8989" );
28
+ clientConfig .setHttpsProxy ("http://test:test@ localhost:8989" );
27
29
profile .setHttpClientConfig (clientConfig );
28
30
return new DefaultAcsClient (profile );
29
31
}
@@ -69,6 +71,46 @@ private DefaultAcsClient getIgnoreWrongApacheProxyClientWithRegionId(String regi
69
71
return new DefaultAcsClient (profile );
70
72
}
71
73
74
+ private DefaultAcsClient getRightAuthProxyApacheClientWithRegionId (String regionId ) throws ClientException , IOException {
75
+ ApacheHttpClient .getInstance (HttpClientConfig .getDefault ()).close ();
76
+ IClientProfile profile = DefaultProfile .getProfile (regionId , accesskeyId , accesskeySecret );
77
+ HttpClientConfig clientConfig = HttpClientConfig .getDefault ();
78
+ clientConfig .setHttpProxy ("http://username:right@localhost:3000" );
79
+ clientConfig .setHttpsProxy ("http://username:right@localhost:3000" );
80
+ profile .setHttpClientConfig (clientConfig );
81
+ return new DefaultAcsClient (profile );
82
+ }
83
+
84
+ private DefaultAcsClient getWrongAuthProxyApacheClientWithRegionId (String regionId ) throws ClientException , IOException {
85
+ ApacheHttpClient .getInstance (HttpClientConfig .getDefault ()).close ();
86
+ IClientProfile profile = DefaultProfile .getProfile (regionId , accesskeyId , accesskeySecret );
87
+ HttpClientConfig clientConfig = HttpClientConfig .getDefault ();
88
+ clientConfig .setHttpProxy ("http://username:wrong@localhost:3000" );
89
+ clientConfig .setHttpsProxy ("http://username:wrong@localhost:3000" );
90
+ profile .setHttpClientConfig (clientConfig );
91
+ return new DefaultAcsClient (profile );
92
+ }
93
+
94
+ private DefaultAcsClient getRightAuthCompatibleProxyClientWithRegionId (String regionId ) throws ClientException , IOException {
95
+ IClientProfile profile = DefaultProfile .getProfile (regionId , accesskeyId , accesskeySecret );
96
+ HttpClientConfig clientConfig = HttpClientConfig .getDefault ();
97
+ clientConfig .setCompatibleMode (true );
98
+ clientConfig .setHttpProxy ("http://username:right@localhost:3000" );
99
+ clientConfig .setHttpsProxy ("http://username:right@localhost:3000" );
100
+ profile .setHttpClientConfig (clientConfig );
101
+ return new DefaultAcsClient (profile );
102
+ }
103
+
104
+ private DefaultAcsClient getWrongAuthCompatibleProxyClientWithRegionId (String regionId ) throws ClientException , IOException {
105
+ IClientProfile profile = DefaultProfile .getProfile (regionId , accesskeyId , accesskeySecret );
106
+ HttpClientConfig clientConfig = HttpClientConfig .getDefault ();
107
+ clientConfig .setCompatibleMode (true );
108
+ clientConfig .setHttpProxy ("http://username:wrong@localhost:3000" );
109
+ clientConfig .setHttpsProxy ("http://username:wrong@localhost:3000" );
110
+ profile .setHttpClientConfig (clientConfig );
111
+ return new DefaultAcsClient (profile );
112
+ }
113
+
72
114
@ Test
73
115
public void apacheRightProxyTest () throws ClientException , IOException {
74
116
DescribeInstancesRequest request = new DescribeInstancesRequest ();
@@ -77,6 +119,15 @@ public void apacheRightProxyTest() throws ClientException, IOException {
77
119
Assert .assertTrue (0 <= response .getTotalCount ());
78
120
}
79
121
122
+ @ Test
123
+ public void apacheRightProxyAssertHeaderTest () throws ClientException , IOException {
124
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
125
+ HttpResponse response = getRightApacheProxyClientWithRegionId (regionId ).doAction (request );
126
+ Assert .assertNotNull (response );
127
+ Assert .assertEquals ("HTTP/1.1 o_o" , response .getHeaderValue ("Via" ));
128
+
129
+ }
130
+
80
131
@ Test
81
132
public void apacheWrongProxyTest () throws ClientException , IOException {
82
133
thrown .expect (ClientException .class );
@@ -87,6 +138,14 @@ public void apacheWrongProxyTest() throws ClientException, IOException {
87
138
Assert .assertTrue (0 <= response .getTotalCount ());
88
139
}
89
140
141
+ @ Test
142
+ public void compatibleRightProxyAssertHeaderTest () throws ClientException , IOException {
143
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
144
+ HttpResponse response = getRightCompatibleProxyClientWithRegionId (regionId ).doAction (request );
145
+ Assert .assertNotNull (response );
146
+ Assert .assertEquals ("HTTP/1.1 o_o" , response .getHeaderValue ("Via" ));
147
+ }
148
+
90
149
@ Test
91
150
public void compatibleRightProxyTest () throws ClientException , IOException {
92
151
DescribeInstancesRequest request = new DescribeInstancesRequest ();
@@ -113,4 +172,50 @@ public void ignoreWrongApacheProxyTest() throws IOException, ClientException {
113
172
Assert .assertTrue (0 <= response .getTotalCount ());
114
173
}
115
174
175
+ @ Test
176
+ public void ignoreWrongApacheProxyAssertHeaderTest () throws IOException , ClientException {
177
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
178
+ HttpResponse response = getIgnoreWrongApacheProxyClientWithRegionId (regionId ).doAction (request );
179
+ Assert .assertNotNull (response );
180
+ Assert .assertNull (response .getHeaderValue ("Via" ));
181
+ }
182
+
183
+ // only test auth proxy server in local environment
184
+ @ Ignore
185
+ @ Test
186
+ public void apacheRightAuthLocalProxyTest () throws ClientException , IOException {
187
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
188
+ HttpResponse response = getRightAuthProxyApacheClientWithRegionId (regionId ).doAction (request );
189
+ Assert .assertEquals (200 , response .getStatus ());
190
+
191
+ }
192
+
193
+ // only test auth proxy server in local environment
194
+ @ Ignore
195
+ @ Test
196
+ public void apacheWrongAuthLocalProxyTest () throws ClientException , IOException {
197
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
198
+ HttpResponse response = getWrongAuthProxyApacheClientWithRegionId (regionId ).doAction (request );
199
+ Assert .assertEquals (401 , response .getStatus ());
200
+
201
+ }
202
+
203
+ // only test auth proxy server in local environment
204
+ @ Ignore
205
+ @ Test
206
+ public void compatibleRightAuthLocalProxyTest () throws ClientException , IOException {
207
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
208
+ HttpResponse response = getRightAuthCompatibleProxyClientWithRegionId (regionId ).doAction (request );
209
+ Assert .assertEquals (200 , response .getStatus ());
210
+ }
211
+
212
+ // only test auth proxy server in local environment
213
+ @ Ignore
214
+ @ Test
215
+ public void compatibleWrongAuthLocalProxyTest () throws ClientException , IOException {
216
+ DescribeInstancesRequest request = new DescribeInstancesRequest ();
217
+ HttpResponse response = getWrongAuthCompatibleProxyClientWithRegionId (regionId ).doAction (request );
218
+ Assert .assertEquals (401 , response .getStatus ());
219
+ }
220
+
116
221
}
0 commit comments