Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6d5351

Browse files
committed
1. assert http header for proxy 2. Test auth proxy in local
1 parent aeb1361 commit c6d5351

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ branches: # build only on these branches
1313

1414
install:
1515
# Install Proxy
16-
- sudo rm -rf ~/.nvm - curl -sL "https://deb.nodesource.com/setup_7.x" | sudo -E bash -
16+
- sudo rm -rf ~/.nvm - curl -sL "https://deb.nodesource.com/setup_11.x" | sudo -E bash -
1717
- sudo apt-get install -y nodejs
1818
- sudo ln -s /usr/bin/nodejs /usr/bin/node
1919
- sudo apt-get install -y npm
20+
- sudo npm i -g n --force -g --registry=https://registry.npm.taobao.org
21+
- sudo n latest
2022
- sudo npm install o_o -g --registry=https://registry.npm.taobao.org
21-
2223
script:
2324
# Start Proxy
2425
- sudo o_o &

aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ private HttpUriRequest parseToHttpRequest(HttpRequest apiReq) throws IOException
190190

191191
builder.addHeader(ACCEPT_ENCODING, "identity");
192192

193+
// calcProxy will modify the "Proxy-Authorization" header of the request
194+
HttpHost proxy = calcProxy(apiReq);
195+
193196
for (Map.Entry<String, String> entry : apiReq.getSysHeaders().entrySet()) {
194197
if ("Content-Length".equalsIgnoreCase(entry.getKey())) {
195198
continue;
@@ -208,7 +211,6 @@ private HttpUriRequest parseToHttpRequest(HttpRequest apiReq) throws IOException
208211
} else {
209212
readTimeout = (int) clientConfig.getReadTimeoutMillis();
210213
}
211-
HttpHost proxy = calcProxy(apiReq);
212214
RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).setConnectTimeout(connectTimeout).setSocketTimeout(
213215
readTimeout).setConnectionRequestTimeout((int) clientConfig.getWriteTimeoutMillis()).build();
214216
builder.setConfig(requestConfig);

java-sdk-function-test/src/test/java/com/aliyuncs/ProxyTest.java

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
55
import com.aliyuncs.exceptions.ClientException;
66
import com.aliyuncs.http.HttpClientConfig;
7+
import com.aliyuncs.http.HttpResponse;
78
import com.aliyuncs.http.clients.ApacheHttpClient;
89
import com.aliyuncs.profile.DefaultProfile;
910
import com.aliyuncs.profile.IClientProfile;
1011
import org.junit.Assert;
12+
import org.junit.Ignore;
1113
import org.junit.Rule;
1214
import org.junit.Test;
1315
import org.junit.rules.ExpectedException;
@@ -22,8 +24,8 @@ private DefaultAcsClient getRightApacheProxyClientWithRegionId(String regionId)
2224
ApacheHttpClient.getInstance(HttpClientConfig.getDefault()).close();
2325
IClientProfile profile = DefaultProfile.getProfile(regionId, accesskeyId, accesskeySecret);
2426
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");
2729
profile.setHttpClientConfig(clientConfig);
2830
return new DefaultAcsClient(profile);
2931
}
@@ -69,6 +71,46 @@ private DefaultAcsClient getIgnoreWrongApacheProxyClientWithRegionId(String regi
6971
return new DefaultAcsClient(profile);
7072
}
7173

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+
72114
@Test
73115
public void apacheRightProxyTest() throws ClientException, IOException {
74116
DescribeInstancesRequest request = new DescribeInstancesRequest();
@@ -77,6 +119,15 @@ public void apacheRightProxyTest() throws ClientException, IOException {
77119
Assert.assertTrue(0 <= response.getTotalCount());
78120
}
79121

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+
80131
@Test
81132
public void apacheWrongProxyTest() throws ClientException, IOException {
82133
thrown.expect(ClientException.class);
@@ -87,6 +138,14 @@ public void apacheWrongProxyTest() throws ClientException, IOException {
87138
Assert.assertTrue(0 <= response.getTotalCount());
88139
}
89140

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+
90149
@Test
91150
public void compatibleRightProxyTest() throws ClientException, IOException {
92151
DescribeInstancesRequest request = new DescribeInstancesRequest();
@@ -113,4 +172,50 @@ public void ignoreWrongApacheProxyTest() throws IOException, ClientException {
113172
Assert.assertTrue(0 <= response.getTotalCount());
114173
}
115174

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+
116221
}

0 commit comments

Comments
 (0)