start implementation of a new advanced http client, in order to fix issue #709

This commit is contained in:
Sebastian Sdorra
2015-04-30 07:17:52 +02:00
parent 518bf3fdab
commit 4407c7ce9e
25 changed files with 3215 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
/**
* 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;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Sebastian Sdorra
*/
public class AdvancedHttpClientTest {
private final AdvancedHttpClient client = new AdvancedHttpClient()
{
@Override
protected AdvancedHttpResponse request(
BaseHttpRequest<?> request) throws IOException
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
};
private static final String URL = "https://www.scm-manager.org";
@Test
public void testGet()
{
AdvancedHttpRequest request = client.get(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.GET, request.getMethod());
}
@Test
public void testDelete()
{
AdvancedHttpRequestWithBody request = client.delete(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.DELETE, request.getMethod());
}
@Test
public void testPut()
{
AdvancedHttpRequestWithBody request = client.put(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.PUT, request.getMethod());
}
@Test
public void testPost()
{
AdvancedHttpRequestWithBody request = client.post(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.POST, request.getMethod());
}
@Test
public void testOptions()
{
AdvancedHttpRequestWithBody request = client.options(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.OPTIONS, request.getMethod());
}
@Test
public void testHead()
{
AdvancedHttpRequest request = client.head(URL);
assertEquals(URL, request.getUrl());
assertEquals(HttpMethod.HEAD, request.getMethod());
}
}

View File

@@ -0,0 +1,57 @@
/**
* 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;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpRequestTest {
@Mock
private AdvancedHttpClient ahc;
@Test
public void testSelf()
{
AdvancedHttpRequest ahr = new AdvancedHttpRequest(ahc, HttpMethod.GET, "https://www.scm-manager.org");
assertEquals(AdvancedHttpRequest.class, ahr.self().getClass());
}
}

View File

@@ -0,0 +1,127 @@
/**
* 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;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpRequestWithBodyTest {
@Mock
private AdvancedHttpClient ahc;
private AdvancedHttpRequestWithBody request;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Before
public void before(){
request = new AdvancedHttpRequestWithBody(ahc, HttpMethod.PUT, "https://www.scm-manager.org");
}
@Test
public void testContentLength()
{
request.contentLength(12l);
assertEquals("12", request.getHeaders().get("Content-Length").iterator().next());
}
@Test
public void testContentType(){
request.contentType("text/plain");
assertEquals("text/plain", request.getHeaders().get("Content-Type").iterator().next());
}
@Test
public void testFileContent() throws IOException{
File file = tempFolder.newFile();
request.fileContent(file);
assertThat(request.getContent(), instanceOf(FileContent.class));
}
@Test
public void testRawContent() throws IOException {
request.rawContent("test".getBytes(Charsets.UTF_8));
assertThat(request.getContent(), instanceOf(RawContent.class));
}
@Test
public void testRawContentWithByteSource() throws IOException {
ByteSource bs = ByteSource.wrap("test".getBytes(Charsets.UTF_8));
request.rawContent(bs);
assertThat(request.getContent(), instanceOf(ByteSourceContent.class));
}
@Test
public void testFormContent(){
FormContentBuilder builder = request.formContent();
assertNotNull(builder);
builder.build();
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testStringContent(){
request.stringContent("test");
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testStringContentWithCharset(){
request.stringContent("test", Charsets.UTF_8);
assertThat(request.getContent(), instanceOf(StringContent.class));
}
@Test
public void testSelf()
{
assertEquals(AdvancedHttpRequestWithBody.class, request.self().getClass());
}
}

View File

@@ -0,0 +1,91 @@
/**
* 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;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class AdvancedHttpResponseTest {
@Mock(answer = Answers.CALLS_REAL_METHODS)
private AdvancedHttpResponse response;
@Test
public void testContent() throws IOException
{
ByteSource bs = ByteSource.wrap("test123".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
byte[] data = response.content();
assertEquals("test123", new String(data, Charsets.UTF_8));
}
@Test
public void testContentAsString() throws IOException
{
ByteSource bs = ByteSource.wrap("123test".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
assertEquals("123test", response.contentAsString());
}
@Test
public void testContentAsReader() throws IOException
{
ByteSource bs = ByteSource.wrap("abc123".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
assertEquals("abc123", CharStreams.toString(response.contentAsReader()));
}
@Test
public void testContentAsStream() throws IOException
{
ByteSource bs = ByteSource.wrap("cde456".getBytes(Charsets.UTF_8));
when(response.contentAsByteSource()).thenReturn(bs);
byte[] data = ByteStreams.toByteArray(response.contentAsStream());
assertEquals("cde456", new String(data, Charsets.UTF_8));
}
}

View File

@@ -0,0 +1,142 @@
/**
* 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;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import java.io.IOException;
import java.util.Collection;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class BaseHttpRequestTest {
@Mock
private AdvancedHttpClient ahc;
private BaseHttpRequest<AdvancedHttpRequest> request;
@Before
public void before(){
request = new AdvancedHttpRequest(ahc, HttpMethod.GET, "https://www.scm-manager.org");
}
@Test
public void testBasicAuth()
{
request.basicAuth("tricia", "mcmillian123");
Multimap<String,String> headers = request.getHeaders();
assertEquals("Basic dHJpY2lhOm1jbWlsbGlhbjEyMw==", headers.get("Authorization").iterator().next());
}
@Test
public void testQueryString(){
request.queryString("a", "b");
assertEquals("https://www.scm-manager.org?a=b", request.getUrl());
}
@Test
public void testQueryStringMultiple(){
request.queryString("a", "b");
request.queryString("c", "d", "e");
assertEquals("https://www.scm-manager.org?a=b&c=d&c=e", request.getUrl());
}
@Test
public void testQueryStringEncoded(){
request.queryString("a", "äüö");
assertEquals("https://www.scm-manager.org?a=%C3%A4%C3%BC%C3%B6", request.getUrl());
}
@Test
public void testQueryStrings(){
Iterable<? extends Object> i1 = Lists.newArrayList("b");
Iterable<? extends Object> i2 = Lists.newArrayList("d", "e");
request.queryStrings("a", i1);
request.queryStrings("c", i2);
assertEquals("https://www.scm-manager.org?a=b&c=d&c=e", request.getUrl());
}
@Test
public void testQuerqStringNullValue(){
request.queryString("a", null, "b");
assertEquals("https://www.scm-manager.org?a=&a=b", request.getUrl());
}
@Test
public void testHeader(){
request.header("a", "b");
assertEquals("b", request.getHeaders().get("a").iterator().next());
}
@Test
public void testHeaderMultiple(){
request.header("a", "b", "c", "d");
Collection<String> values = request.getHeaders().get("a");
assertThat(values, containsInAnyOrder("b", "c", "d"));
}
@Test
public void testRequest() throws IOException{
request.request();
verify(ahc).request(request);
}
@Test
public void testBuilderMethods(){
Iterable<? extends Object> i1 = Lists.newArrayList("b");
assertThat(request.decodeGZip(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDecodeGZip());
assertThat(request.disableCertificateValidation(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDisableCertificateValidation());
assertThat(request.disableHostnameValidation(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isDisableHostnameValidation());
assertThat(request.ignoreProxySettings(true), instanceOf(AdvancedHttpRequest.class));
assertTrue(request.isIgnoreProxySettings());
assertThat(request.header("a", "b"), instanceOf(AdvancedHttpRequest.class));
assertThat(request.headers("a", i1), instanceOf(AdvancedHttpRequest.class));
assertThat(request.queryString("a", "b"), instanceOf(AdvancedHttpRequest.class));
assertThat(request.queryStrings("a", i1), instanceOf(AdvancedHttpRequest.class));
}
}

View File

@@ -0,0 +1,74 @@
/**
* 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;
import com.google.common.base.Charsets;
import com.google.common.io.ByteSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class ByteSourceContentTest {
@Mock
private AdvancedHttpRequestWithBody request;
@Test
public void testPrepareRequest() throws IOException
{
ByteSource source = ByteSource.wrap("abc".getBytes(Charsets.UTF_8));
ByteSourceContent content = new ByteSourceContent(source);
content.prepare(request);
verify(request).contentLength(3l);
}
@Test
public void testProcess() throws IOException{
ByteSource source = ByteSource.wrap("abc".getBytes(Charsets.UTF_8));
ByteSourceContent content = new ByteSourceContent(source);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
content.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
}

View File

@@ -0,0 +1,116 @@
/**
* 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.io.Files;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
//~--- JDK imports ------------------------------------------------------------
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class FileContentTest
{
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testPrepareRequest() throws IOException
{
FileContent content = create("abc");
content.prepare(request);
verify(request).contentLength(3l);
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testProcess() throws IOException
{
FileContent content = create("abc");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
content.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
private FileContent create(String value) throws IOException
{
File file = temp.newFile();
Files.write("abc", file, Charsets.UTF_8);
return new FileContent(file);
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Rule
public TemporaryFolder temp = new TemporaryFolder();
/** Field description */
@Mock
private AdvancedHttpRequestWithBody request;
}

View File

@@ -0,0 +1,95 @@
/**
* 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;
import com.google.common.collect.Lists;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class FormContentBuilderTest {
@Mock
private AdvancedHttpRequestWithBody request;
@InjectMocks
private FormContentBuilder builder;
@Test
public void testFieldEncoding()
{
builder.field("a", "ü", "ä", "ö").build();
assertContent("a=%C3%BC&a=%C3%A4&a=%C3%B6");
}
@Test
public void testBuild()
{
builder.field("a", "b").build();
assertContent("a=b");
verify(request).contentType("application/x-www-form-urlencoded");
}
@Test
public void testFieldWithArray()
{
builder.field("a", "b").field("c", "d", "e").build();
assertContent("a=b&c=d&c=e");
}
@Test
public void testFieldWithIterable()
{
Iterable<? extends Object> i1 = Lists.newArrayList("b");
builder.fields("a", i1)
.fields("c", Lists.newArrayList("d", "e"))
.build();
assertContent("a=b&c=d&c=e");
}
private void assertContent(String content){
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(request).stringContent(captor.capture());
assertEquals(content, captor.getValue());
}
}

View File

@@ -0,0 +1,72 @@
/**
* 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;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
/**
*
* @author Sebastian Sdorra
*/
@RunWith(MockitoJUnitRunner.class)
public class RawContentTest {
@Mock
private AdvancedHttpRequestWithBody request;
@Test
public void testPrepareRequest()
{
RawContent raw = new RawContent("abc".getBytes(Charsets.UTF_8));
raw.prepare(request);
verify(request).contentLength(3);
}
@Test
public void testProcess() throws IOException
{
RawContent raw = new RawContent("abc".getBytes(Charsets.UTF_8));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
raw.process(baos);
assertEquals("abc", baos.toString("UTF-8"));
}
}

View File

@@ -0,0 +1,59 @@
/**
* 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;
import com.google.common.base.Charsets;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Sebastian Sdorra
*/
public class StringContentTest {
@Test
public void testStringContent()
{
StringContent sc = new StringContent("abc", Charsets.UTF_8);
assertEquals("abc", new String(sc.getData()));
}
@Test
public void testStringContentWithCharset()
{
StringContent sc = new StringContent("üäö", Charsets.ISO_8859_1);
assertEquals("üäö", new String(sc.getData(), Charsets.ISO_8859_1));
}
}