mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 09:25:43 +01:00
more unit tests for ahc
This commit is contained in:
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.net.ahc;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import sonia.scm.config.ScmConfiguration;
|
||||
import sonia.scm.net.TrustAllHostnameVerifier;
|
||||
import sonia.scm.util.HttpUtil;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultAdvancedHttpClientTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testApplyBaseSettings() throws IOException
|
||||
{
|
||||
new AdvancedHttpRequest(client, HttpMethod.GET,
|
||||
"https://www.scm-manager.org").request();
|
||||
verify(connection).setRequestMethod(HttpMethod.GET);
|
||||
verify(connection).setReadTimeout(DefaultAdvancedHttpClient.TIMEOUT_RAED);
|
||||
verify(connection).setConnectTimeout(
|
||||
DefaultAdvancedHttpClient.TIMEOUT_CONNECTION);
|
||||
verify(connection).addRequestProperty(HttpUtil.HEADER_CONTENT_LENGTH, "0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testApplyContent() throws IOException
|
||||
{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
when(connection.getOutputStream()).thenReturn(baos);
|
||||
|
||||
AdvancedHttpRequestWithBody request =
|
||||
new AdvancedHttpRequestWithBody(client, HttpMethod.PUT,
|
||||
"https://www.scm-manager.org");
|
||||
|
||||
request.stringContent("test").request();
|
||||
verify(connection).setDoOutput(true);
|
||||
verify(connection).addRequestProperty(HttpUtil.HEADER_CONTENT_LENGTH, "4");
|
||||
assertEquals("test", baos.toString("UTF-8"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testApplyHeaders() throws IOException
|
||||
{
|
||||
AdvancedHttpRequest request = new AdvancedHttpRequest(client,
|
||||
HttpMethod.POST,
|
||||
"http://www.scm-manager.org");
|
||||
|
||||
request.header("Header-One", "One").header("Header-Two", "Two").request();
|
||||
verify(connection).setRequestMethod(HttpMethod.POST);
|
||||
verify(connection).addRequestProperty("Header-One", "One");
|
||||
verify(connection).addRequestProperty("Header-Two", "Two");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testApplyMultipleHeaders() throws IOException
|
||||
{
|
||||
AdvancedHttpRequest request = new AdvancedHttpRequest(client,
|
||||
HttpMethod.POST,
|
||||
"http://www.scm-manager.org");
|
||||
|
||||
request.header("Header-One", "One").header("Header-One", "Two").request();
|
||||
verify(connection).setRequestMethod(HttpMethod.POST);
|
||||
verify(connection).addRequestProperty("Header-One", "One");
|
||||
verify(connection).addRequestProperty("Header-One", "Two");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testBodyRequestWithoutContent() throws IOException
|
||||
{
|
||||
AdvancedHttpRequestWithBody request =
|
||||
new AdvancedHttpRequestWithBody(client, HttpMethod.PUT,
|
||||
"https://www.scm-manager.org");
|
||||
|
||||
request.request();
|
||||
verify(connection).addRequestProperty(HttpUtil.HEADER_CONTENT_LENGTH, "0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testDisableCertificateValidation() throws IOException
|
||||
{
|
||||
AdvancedHttpRequest request = new AdvancedHttpRequest(client,
|
||||
HttpMethod.GET,
|
||||
"https://www.scm-manager.org");
|
||||
|
||||
request.disableCertificateValidation(true).request();
|
||||
verify(connection).setSSLSocketFactory(any(SSLSocketFactory.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testDisableHostnameValidation() throws IOException
|
||||
{
|
||||
AdvancedHttpRequest request = new AdvancedHttpRequest(client,
|
||||
HttpMethod.GET,
|
||||
"https://www.scm-manager.org");
|
||||
|
||||
request.disableHostnameValidation(true).request();
|
||||
verify(connection).setHostnameVerifier(any(TrustAllHostnameVerifier.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testIgnoreProxy() throws IOException
|
||||
{
|
||||
configuration.setProxyServer("proxy.scm-manager.org");
|
||||
configuration.setProxyPort(8090);
|
||||
configuration.setEnableProxy(true);
|
||||
new AdvancedHttpRequest(client, HttpMethod.GET,
|
||||
"https://www.scm-manager.org").ignoreProxySettings(true).request();
|
||||
assertFalse(client.proxyConnection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testProxyConnection() throws IOException
|
||||
{
|
||||
configuration.setProxyServer("proxy.scm-manager.org");
|
||||
configuration.setProxyPort(8090);
|
||||
configuration.setEnableProxy(true);
|
||||
new AdvancedHttpRequest(client, HttpMethod.GET,
|
||||
"https://www.scm-manager.org").request();
|
||||
assertTrue(client.proxyConnection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testProxyWithAuthentication() throws IOException
|
||||
{
|
||||
configuration.setProxyServer("proxy.scm-manager.org");
|
||||
configuration.setProxyPort(8090);
|
||||
configuration.setProxyUser("tricia");
|
||||
configuration.setProxyPassword("tricias secret");
|
||||
configuration.setEnableProxy(true);
|
||||
new AdvancedHttpRequest(client, HttpMethod.GET,
|
||||
"https://www.scm-manager.org").request();
|
||||
assertTrue(client.proxyConnection);
|
||||
verify(connection).addRequestProperty(
|
||||
DefaultAdvancedHttpClient.HEADER_PROXY_AUTHORIZATION,
|
||||
"Basic dHJpY2lhOnRyaWNpYXMgc2VjcmV0");
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Before
|
||||
public void setUp()
|
||||
{
|
||||
configuration = new ScmConfiguration();
|
||||
client = new TestingAdvacedHttpClient(configuration);
|
||||
}
|
||||
|
||||
//~--- inner classes --------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Class description
|
||||
*
|
||||
*
|
||||
* @version Enter version here..., 15/05/01
|
||||
* @author Enter your name here...
|
||||
*/
|
||||
public class TestingAdvacedHttpClient extends DefaultAdvancedHttpClient
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param configuration
|
||||
*/
|
||||
public TestingAdvacedHttpClient(ScmConfiguration configuration)
|
||||
{
|
||||
super(configuration);
|
||||
}
|
||||
|
||||
//~--- methods ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
protected HttpURLConnection createConnection(URL url) throws IOException
|
||||
{
|
||||
return connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
* @param address
|
||||
*
|
||||
* @return
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
protected HttpURLConnection createProxyConnecton(URL url,
|
||||
SocketAddress address)
|
||||
throws IOException
|
||||
{
|
||||
proxyConnection = true;
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
//~--- fields -------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private boolean proxyConnection = false;
|
||||
}
|
||||
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private TestingAdvacedHttpClient client;
|
||||
|
||||
/** Field description */
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
/** Field description */
|
||||
@Mock
|
||||
private HttpsURLConnection connection;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Copyright (c) 2014, Sebastian Sdorra
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of SCM-Manager; nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.net.ahc;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.io.ByteSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultAdvancedHttpResponseTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
public void testContentAsByteSource() throws IOException
|
||||
{
|
||||
ByteArrayInputStream bais =
|
||||
new ByteArrayInputStream("test".getBytes(Charsets.UTF_8));
|
||||
|
||||
when(connection.getInputStream()).thenReturn(bais);
|
||||
|
||||
AdvancedHttpResponse response = new DefaultAdvancedHttpResponse(connection,
|
||||
200, "OK");
|
||||
ByteSource content = response.contentAsByteSource();
|
||||
|
||||
assertEquals("test", content.asCharSource(Charsets.UTF_8).read());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testGetHeaders()
|
||||
{
|
||||
LinkedHashMap<String, List<String>> map = Maps.newLinkedHashMap();
|
||||
List<String> test = Lists.newArrayList("One", "Two");
|
||||
|
||||
map.put("Test", test);
|
||||
when(connection.getHeaderFields()).thenReturn(map);
|
||||
|
||||
AdvancedHttpResponse response = new DefaultAdvancedHttpResponse(connection,
|
||||
200, "OK");
|
||||
Multimap<String, String> headers = response.getHeaders();
|
||||
|
||||
assertThat(headers.get("Test"), contains("One", "Two"));
|
||||
assertTrue(headers.get("Test-2").isEmpty());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
@Mock
|
||||
private HttpURLConnection connection;
|
||||
}
|
||||
Reference in New Issue
Block a user