|
1 | 1 | package org.baeldung.mockito; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.SocketTimeoutException; |
| 8 | + |
| 9 | +import org.apache.http.HttpResponse; |
| 10 | +import org.apache.http.client.ClientProtocolException; |
| 11 | +import org.apache.http.client.HttpClient; |
| 12 | +import org.apache.http.client.config.RequestConfig; |
| 13 | +import org.apache.http.client.methods.HttpGet; |
| 14 | +import org.apache.http.entity.ContentType; |
| 15 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 16 | +import org.apache.http.impl.conn.BasicHttpClientConnectionManager; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | + |
3 | 20 | public class HttpClientLiveTest { |
4 | | - // |
| 21 | + |
| 22 | + private static final String SAMPLE_URL = "http://www.google.com"; |
| 23 | + |
| 24 | + private HttpClient instance; |
| 25 | + |
| 26 | + @Before |
| 27 | + public final void before() { |
| 28 | + instance = HttpClientBuilder.create().build(); |
| 29 | + } |
| 30 | + |
| 31 | + // tests |
| 32 | + |
| 33 | + @Test |
| 34 | + public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
| 35 | + instance.execute(new HttpGet(SAMPLE_URL)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { |
| 40 | + final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 41 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { |
| 46 | + final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 47 | + final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); |
| 48 | + |
| 49 | + assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); |
| 50 | + } |
| 51 | + |
| 52 | + @Test(expected = SocketTimeoutException.class) |
| 53 | + public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { |
| 54 | + final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build(); |
| 55 | + final HttpGet request = new HttpGet(SAMPLE_URL); |
| 56 | + request.setConfig(requestConfig); |
| 57 | + instance.execute(request); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
| 62 | + instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build(); |
| 63 | + instance.execute(new HttpGet(SAMPLE_URL)); |
| 64 | + } |
| 65 | + |
5 | 66 | } |
0 commit comments