From 6b45feb185a2570131d42c3cee5afd8381055c11 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 19 Nov 2013 18:02:45 +0200 Subject: [PATCH] Dependencies and custom logic --- httpclient/pom.xml | 8 ++- .../baeldung/mockito/HttpClientLiveTest.java | 63 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/httpclient/pom.xml b/httpclient/pom.xml index 7ce13c1b81..3f3f0f3cde 100644 --- a/httpclient/pom.xml +++ b/httpclient/pom.xml @@ -23,7 +23,13 @@ ${commons-lang3.version} - + + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + diff --git a/httpclient/src/test/java/org/baeldung/mockito/HttpClientLiveTest.java b/httpclient/src/test/java/org/baeldung/mockito/HttpClientLiveTest.java index 81e6fb8b25..997f5a012a 100644 --- a/httpclient/src/test/java/org/baeldung/mockito/HttpClientLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/mockito/HttpClientLiveTest.java @@ -1,5 +1,66 @@ package org.baeldung.mockito; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.IOException; +import java.net.SocketTimeoutException; + +import org.apache.http.HttpResponse; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.BasicHttpClientConnectionManager; +import org.junit.Before; +import org.junit.Test; + public class HttpClientLiveTest { - // + + private static final String SAMPLE_URL = "http://www.google.com"; + + private HttpClient instance; + + @Before + public final void before() { + instance = HttpClientBuilder.create().build(); + } + + // tests + + @Test + public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { + instance.execute(new HttpGet(SAMPLE_URL)); + } + + @Test + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { + final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); + } + + @Test + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { + final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); + final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); + + assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); + } + + @Test(expected = SocketTimeoutException.class) + public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { + final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build(); + final HttpGet request = new HttpGet(SAMPLE_URL); + request.setConfig(requestConfig); + instance.execute(request); + } + + @Test + public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException { + instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build(); + instance.execute(new HttpGet(SAMPLE_URL)); + } + }