Skip to content

Commit 07ff56f

Browse files
AXIS2-5948 Add org.apache.axis2.transport.http.HTTPProxyConfiguratorTest
1 parent 2741a14 commit 07ff56f

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.axis2.transport.http;
21+
22+
import org.apache.axiom.om.OMElement;
23+
import org.apache.axiom.om.impl.llom.AxiomElementImpl;
24+
import org.apache.axis2.AxisFault;
25+
import org.apache.axis2.context.ConfigurationContext;
26+
import org.apache.axis2.context.MessageContext;
27+
import org.apache.axis2.description.Parameter;
28+
import org.apache.axis2.engine.AxisConfiguration;
29+
import org.apache.axis2.transport.http.HTTPTransportConstants;
30+
import org.apache.hc.core5.http.HttpHost;
31+
import org.apache.hc.client5.http.auth.AuthScope;
32+
import org.apache.hc.client5.http.auth.Credentials;
33+
import org.apache.hc.client5.http.auth.CredentialsProvider;
34+
import org.apache.hc.client5.http.config.RequestConfig;
35+
import org.apache.hc.client5.http.protocol.HttpClientContext;
36+
import org.junit.Test;
37+
38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertNotNull;
40+
41+
import org.apache.axis2.transport.http.impl.httpclient5.HTTPProxyConfigurator;
42+
43+
// See AXIS2-5948: Proxy settings ignored if username not specified
44+
public class HTTPProxyConfiguratorTest {
45+
46+
@Test
47+
public void testProxyWithCredentials() throws AxisFault {
48+
final OMElement configurationElement = new AxiomElementImpl();
49+
50+
final String hostname = "http://host";
51+
final OMElement host = new AxiomElementImpl();
52+
host.setText(hostname);
53+
host.setLocalName(HTTPTransportConstants.PROXY_HOST_ELEMENT);
54+
configurationElement.addChild(host);
55+
56+
final int portNumber = 8080;
57+
final OMElement port = new AxiomElementImpl();
58+
port.setText(String.valueOf(portNumber));
59+
port.setLocalName(HTTPTransportConstants.PROXY_PORT_ELEMENT);
60+
configurationElement.addChild(port);
61+
62+
final String user = "user";
63+
final OMElement username = new AxiomElementImpl();
64+
username.setText(user);
65+
username.setLocalName(HTTPTransportConstants.PROXY_USER_ELEMENT);
66+
configurationElement.addChild(username);
67+
68+
final String pass = "password";
69+
final OMElement password = new AxiomElementImpl();
70+
password.setText(pass);
71+
password.setLocalName(HTTPTransportConstants.PROXY_PASSWORD_ELEMENT);
72+
configurationElement.addChild(password);
73+
74+
final OMElement element = new AxiomElementImpl();
75+
element.addChild(configurationElement);
76+
final Parameter param = new Parameter();
77+
param.setParameterElement(element);
78+
param.setName(HTTPTransportConstants.ATTR_PROXY);
79+
final AxisConfiguration configuration = new AxisConfiguration();
80+
configuration.addParameter(param);
81+
final MessageContext messageContext = new MessageContext();
82+
final ConfigurationContext configurationContext = new ConfigurationContext(configuration);
83+
messageContext.setConfigurationContext(configurationContext);
84+
final RequestConfig.Builder builder = RequestConfig.custom();
85+
86+
final HttpClientContext clientContext = new HttpClientContext();
87+
HTTPProxyConfigurator.configure(messageContext, builder, clientContext);
88+
final RequestConfig config = builder.build();
89+
final HttpHost proxyHost = config.getProxy();
90+
assertNotNull(proxyHost);
91+
assertEquals(hostname, proxyHost.getHostName());
92+
assertEquals(portNumber, proxyHost.getPort());
93+
94+
final CredentialsProvider provider = clientContext.getCredentialsProvider();
95+
assertNotNull(provider);
96+
final Credentials credentials = provider.getCredentials(new AuthScope(null, -1), clientContext);
97+
assertNotNull(credentials);
98+
assertEquals(user, credentials.getUserPrincipal().getName());
99+
assertEquals(pass, new String(credentials.getPassword()));
100+
}
101+
102+
@Test
103+
public void testProxyWithoutCredentials() throws AxisFault {
104+
final OMElement configurationElement = new AxiomElementImpl();
105+
106+
final String hostname = "http://host";
107+
final OMElement host = new AxiomElementImpl();
108+
host.setText(hostname);
109+
host.setLocalName(HTTPTransportConstants.PROXY_HOST_ELEMENT);
110+
configurationElement.addChild(host);
111+
112+
final int portNumber = 8080;
113+
final OMElement port = new AxiomElementImpl();
114+
port.setText(String.valueOf(portNumber));
115+
port.setLocalName(HTTPTransportConstants.PROXY_PORT_ELEMENT);
116+
configurationElement.addChild(port);
117+
118+
final OMElement element = new AxiomElementImpl();
119+
element.addChild(configurationElement);
120+
final Parameter param = new Parameter();
121+
param.setParameterElement(element);
122+
param.setName(HTTPTransportConstants.ATTR_PROXY);
123+
final AxisConfiguration configuration = new AxisConfiguration();
124+
configuration.addParameter(param);
125+
final MessageContext messageContext = new MessageContext();
126+
final ConfigurationContext configurationContext = new ConfigurationContext(configuration);
127+
messageContext.setConfigurationContext(configurationContext);
128+
final RequestConfig.Builder builder = RequestConfig.custom();
129+
130+
final HttpClientContext clientContext = new HttpClientContext();
131+
HTTPProxyConfigurator.configure(messageContext, builder, clientContext);
132+
final RequestConfig config = builder.build();
133+
final HttpHost proxyHost = config.getProxy();
134+
assertNotNull(proxyHost);
135+
assertEquals(hostname, proxyHost.getHostName());
136+
assertEquals(portNumber, proxyHost.getPort());
137+
System.out.println("testProxyWithoutCredentials() passed");
138+
}
139+
}
140+

0 commit comments

Comments
 (0)