From b76928dcf554d25a49fc9c2dc729b047fb662792 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 24 Dec 2013 18:09:27 +0200 Subject: [PATCH] cleanup and testing work --- .../httpclient/HttpClientBasicLiveTest.java | 93 +++++++++++++++++++ .../httpclient/HttpClientLiveTest.java | 41 -------- 2 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java new file mode 100644 index 0000000000..d11af39d34 --- /dev/null +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientBasicLiveTest.java @@ -0,0 +1,93 @@ +package org.baeldung.httpclient; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HttpClientBasicLiveTest { + + private static final String SAMPLE_URL = "http://www.github.com"; + + private CloseableHttpClient instance; + + private CloseableHttpResponse response; + + @Before + public final void before() { + instance = HttpClientBuilder.create().build(); + } + + @After + public final void after() throws IllegalStateException, IOException { + if (response == null) { + return; + } + + try { + final HttpEntity entity = response.getEntity(); + if (entity != null) { + final InputStream instream = entity.getContent(); + instream.close(); + } + } finally { + response.close(); + } + } + + // tests + + // simple request - response + + @Test + public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { + response = instance.execute(new HttpGet(SAMPLE_URL)); + } + + @Test + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { + response = instance.execute(new HttpGet(SAMPLE_URL)); + int statusCode = response.getStatusLine().getStatusCode(); + assertThat(statusCode, equalTo(HttpStatus.SC_OK)); + } + + @Test + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { + response = instance.execute(new HttpGet(SAMPLE_URL)); + final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); + + assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); + } + + @Test + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException { + response = instance.execute(new HttpGet(SAMPLE_URL)); + final String bodyAsString = EntityUtils.toString(response.getEntity()); + + assertThat(bodyAsString, notNullValue()); + } + + // tests - non-GET + + @Test + public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException { + instance.execute(new HttpPost(SAMPLE_URL)); + } + +} diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java index 3356ba92c3..e2c7a139f5 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java @@ -1,9 +1,7 @@ package org.baeldung.httpclient; import static org.hamcrest.Matchers.emptyArray; -import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertThat; import java.io.IOException; @@ -16,9 +14,7 @@ import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; -import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClientBuilder; @@ -26,7 +22,6 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.BasicHttpClientConnectionManager; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.HttpProtocolParams; -import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -63,35 +58,6 @@ public class HttpClientLiveTest { // tests - // simple request - response - - @Test - public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); - - assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - final String bodyAsString = EntityUtils.toString(response.getEntity()); - - assertThat(bodyAsString, notNullValue()); - } - @Test(expected = ConnectTimeoutException.class) public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build(); @@ -100,13 +66,6 @@ public class HttpClientLiveTest { response = instance.execute(request); } - // tests - non-GET - - @Test - public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException { - instance.execute(new HttpPost(SAMPLE_URL)); - } - // tests - configs @Test