From 3c6306059f35b009a3ddb97a2b6f0d14cb13dc78 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 1 May 2015 12:36:49 +0200 Subject: [PATCH] more unit tests for ahc --- .../sonia/scm/net/ahc/BaseHttpRequest.java | 3 +- .../scm/net/ahc/AdvancedHttpResponseTest.java | 39 ++ .../scm/net/ahc/BaseHttpRequestTest.java | 2 +- .../net/ahc/DefaultAdvancedHttpClient.java | 83 ++++- .../net/ahc/DefaultAdvancedHttpResponse.java | 38 +- .../ahc/DefaultAdvancedHttpClientTest.java | 352 ++++++++++++++++++ .../ahc/DefaultAdvancedHttpResponseTest.java | 121 ++++++ 7 files changed, 591 insertions(+), 47 deletions(-) create mode 100644 scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpClientTest.java create mode 100644 scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponseTest.java diff --git a/scm-core/src/main/java/sonia/scm/net/ahc/BaseHttpRequest.java b/scm-core/src/main/java/sonia/scm/net/ahc/BaseHttpRequest.java index f48e819ddc..c80ae9b1c4 100644 --- a/scm-core/src/main/java/sonia/scm/net/ahc/BaseHttpRequest.java +++ b/scm-core/src/main/java/sonia/scm/net/ahc/BaseHttpRequest.java @@ -36,6 +36,7 @@ package sonia.scm.net.ahc; import com.google.common.base.Charsets; import com.google.common.base.Strings; import com.google.common.collect.HashMultimap; +import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import org.apache.shiro.codec.Base64; @@ -386,7 +387,7 @@ public abstract class BaseHttpRequest protected final AdvancedHttpClient client; /** http header */ - private final Multimap headers = HashMultimap.create(); + private final Multimap headers = LinkedHashMultimap.create(); /** http method */ private final String method; diff --git a/scm-core/src/test/java/sonia/scm/net/ahc/AdvancedHttpResponseTest.java b/scm-core/src/test/java/sonia/scm/net/ahc/AdvancedHttpResponseTest.java index 9b3ccaa348..277bda856d 100644 --- a/scm-core/src/test/java/sonia/scm/net/ahc/AdvancedHttpResponseTest.java +++ b/scm-core/src/test/java/sonia/scm/net/ahc/AdvancedHttpResponseTest.java @@ -32,6 +32,8 @@ package sonia.scm.net.ahc; import com.google.common.base.Charsets; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; import com.google.common.io.ByteSource; import com.google.common.io.ByteStreams; import com.google.common.io.CharStreams; @@ -87,5 +89,42 @@ public class AdvancedHttpResponseTest { byte[] data = ByteStreams.toByteArray(response.contentAsStream()); assertEquals("cde456", new String(data, Charsets.UTF_8)); } + + @Test + public void testGetFirstHeader() throws IOException + { + Multimap mm = LinkedHashMultimap.create(); + mm.put("Test", "One"); + mm.put("Test-2", "One"); + mm.put("Test-2", "Two"); + when(response.getHeaders()).thenReturn(mm); + assertEquals("One", response.getFirstHeader("Test")); + assertEquals("One", response.getFirstHeader("Test-2")); + assertNull(response.getFirstHeader("Test-3")); + } + + @Test + public void testIsSuccessful() throws IOException + { + // successful + when(response.getStatus()).thenReturn(200); + assertTrue(response.isSuccessful()); + when(response.getStatus()).thenReturn(201); + assertTrue(response.isSuccessful()); + when(response.getStatus()).thenReturn(204); + assertTrue(response.isSuccessful()); + when(response.getStatus()).thenReturn(301); + assertTrue(response.isSuccessful()); + + // not successful + when(response.getStatus()).thenReturn(400); + assertFalse(response.isSuccessful()); + when(response.getStatus()).thenReturn(404); + assertFalse(response.isSuccessful()); + when(response.getStatus()).thenReturn(500); + assertFalse(response.isSuccessful()); + when(response.getStatus()).thenReturn(199); + assertFalse(response.isSuccessful()); + } } \ No newline at end of file diff --git a/scm-core/src/test/java/sonia/scm/net/ahc/BaseHttpRequestTest.java b/scm-core/src/test/java/sonia/scm/net/ahc/BaseHttpRequestTest.java index 7adab5aa84..9b3ce995f5 100644 --- a/scm-core/src/test/java/sonia/scm/net/ahc/BaseHttpRequestTest.java +++ b/scm-core/src/test/java/sonia/scm/net/ahc/BaseHttpRequestTest.java @@ -113,7 +113,7 @@ public class BaseHttpRequestTest { public void testHeaderMultiple(){ request.header("a", "b", "c", "d"); Collection values = request.getHeaders().get("a"); - assertThat(values, containsInAnyOrder("b", "c", "d")); + assertThat(values, contains("b", "c", "d")); } @Test diff --git a/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpClient.java b/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpClient.java index 1aadef143c..6b80d6bdae 100644 --- a/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpClient.java +++ b/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpClient.java @@ -33,6 +33,7 @@ package sonia.scm.net.ahc; //~--- non-JDK imports -------------------------------------------------------- +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.Multimap; import com.google.common.io.Closeables; @@ -69,6 +70,8 @@ import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; /** + * Default implementation of the {@link AdvancedHttpClient}. The default + * implementation uses {@link HttpURLConnection}. * * @author Sebastian Sdorra * @since 1.46 @@ -76,20 +79,23 @@ import javax.net.ssl.TrustManager; public class DefaultAdvancedHttpClient extends AdvancedHttpClient { - /** Field description */ - public static final String CREDENTIAL_SEPARATOR = ":"; + /** credential separator */ + private static final String CREDENTIAL_SEPARATOR = ":"; - /** Field description */ - public static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization"; + /** proxy authorization header */ + @VisibleForTesting + static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization"; - /** Field description */ - public static final String PREFIX_BASIC_AUTHENTICATION = "Basic "; + /** basic authentication prefix */ + private static final String PREFIX_BASIC_AUTHENTICATION = "Basic "; - /** Field description */ - public static final int TIMEOUT_CONNECTION = 30000; + /** connection timeout */ + @VisibleForTesting + static final int TIMEOUT_CONNECTION = 30000; - /** Field description */ - public static final int TIMEOUT_RAED = 1200000; + /** read timeout */ + @VisibleForTesting + static final int TIMEOUT_RAED = 1200000; /** * the logger for DefaultAdvancedHttpClient @@ -100,10 +106,10 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient //~--- constructors --------------------------------------------------------- /** - * Constructs ... + * Constructs a new {@link DefaultAdvancedHttpClient}. * * - * @param configuration + * @param configuration scm-manager main configuration */ @Inject public DefaultAdvancedHttpClient(ScmConfiguration configuration) @@ -114,12 +120,50 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient //~--- methods -------------------------------------------------------------- /** - * Method description + * Creates a new {@link HttpURLConnection} from the given {@link URL}. The + * method is visible for testing. * * - * @param request + * @param url url * - * @return + * @return new {@link HttpURLConnection} + * + * @throws IOException + */ + @VisibleForTesting + protected HttpURLConnection createConnection(URL url) throws IOException + { + return (HttpURLConnection) url.openConnection(); + } + + /** + * Creates a new proxy {@link HttpURLConnection} from the given {@link URL} + * and {@link SocketAddress}. The method is visible for testing. + * + * + * @param url url + * @param address proxy socket address + * + * @return new proxy {@link HttpURLConnection} + * + * @throws IOException + */ + @VisibleForTesting + protected HttpURLConnection createProxyConnecton(URL url, + SocketAddress address) + throws IOException + { + return (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, + address)); + } + + /** + * Executes the given request and returns the server response. + * + * + * @param request http request + * + * @return server response * * @throws IOException */ @@ -223,7 +267,7 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient { for (String value : headers.get(key)) { - connection.setRequestProperty(key, value); + connection.addRequestProperty(key, value); } } } @@ -280,7 +324,7 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient logger.debug("fetch {}", url.toExternalForm()); - connection = (HttpURLConnection) url.openConnection(); + connection = createConnection(url); } return connection; @@ -300,8 +344,7 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient new InetSocketAddress(configuration.getProxyServer(), configuration.getProxyPort()); - return (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, - address)); + return createProxyConnecton(url, address); } //~--- get methods ---------------------------------------------------------- @@ -314,6 +357,6 @@ public class DefaultAdvancedHttpClient extends AdvancedHttpClient //~--- fields --------------------------------------------------------------- - /** Field description */ + /** scm-manager main configuration */ private final ScmConfiguration configuration; } diff --git a/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponse.java b/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponse.java index a679995a44..d5eb7d1727 100644 --- a/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponse.java +++ b/scm-webapp/src/main/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponse.java @@ -34,6 +34,7 @@ package sonia.scm.net.ahc; //~--- non-JDK imports -------------------------------------------------------- import com.google.common.collect.HashMultimap; +import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import com.google.common.io.ByteSource; @@ -48,6 +49,7 @@ import java.util.List; import java.util.Map.Entry; /** + * Http server response object of {@link DefaultAdvancedHttpClient}. * * @author Sebastian Sdorra * @since 1.46 @@ -56,12 +58,12 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse { /** - * Constructs ... + * Constructs a new {@link DefaultAdvancedHttpResponse}. * * - * @param connection - * @param status - * @param statusText + * @param connection http connection + * @param status response status code + * @param statusText response status text */ DefaultAdvancedHttpResponse(HttpURLConnection connection, int status, String statusText) @@ -74,12 +76,7 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse //~--- methods -------------------------------------------------------------- /** - * Method description - * - * - * @return - * - * @throws IOException + * {@inheritDoc} */ @Override public ByteSource contentAsByteSource() throws IOException @@ -90,17 +87,14 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse //~--- get methods ---------------------------------------------------------- /** - * Method description - * - * - * @return + * {@inheritDoc} */ @Override public Multimap getHeaders() { if (headers == null) { - headers = HashMultimap.create(); + headers = LinkedHashMultimap.create(); for (Entry> e : connection.getHeaderFields().entrySet()) @@ -113,10 +107,7 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse } /** - * Method description - * - * - * @return + * {@inheritDoc} */ @Override public int getStatus() @@ -125,10 +116,7 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse } /** - * Method description - * - * - * @return + * {@inheritDoc} */ @Override public String getStatusText() @@ -183,10 +171,10 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse /** http connection */ private final HttpURLConnection connection; - /** Field description */ + /** server response status */ private final int status; - /** Field description */ + /** server response text */ private final String statusText; /** http headers */ diff --git a/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpClientTest.java b/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpClientTest.java new file mode 100644 index 0000000000..451c07b5cc --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpClientTest.java @@ -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; +} diff --git a/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponseTest.java b/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponseTest.java new file mode 100644 index 0000000000..470f1d14ae --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/net/ahc/DefaultAdvancedHttpResponseTest.java @@ -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> map = Maps.newLinkedHashMap(); + List test = Lists.newArrayList("One", "Two"); + + map.put("Test", test); + when(connection.getHeaderFields()).thenReturn(map); + + AdvancedHttpResponse response = new DefaultAdvancedHttpResponse(connection, + 200, "OK"); + Multimap headers = response.getHeaders(); + + assertThat(headers.get("Test"), contains("One", "Two")); + assertTrue(headers.get("Test-2").isEmpty()); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @Mock + private HttpURLConnection connection; +}