|
1 | 1 | package org.baeldung.mockito; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.emptyArray; |
3 | 4 | import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.not; |
| 6 | +import static org.hamcrest.Matchers.notNullValue; |
4 | 7 | import static org.junit.Assert.assertThat; |
5 | 8 |
|
6 | 9 | import java.io.IOException; |
| 10 | +import java.io.InputStream; |
7 | 11 | import java.net.SocketTimeoutException; |
8 | 12 |
|
9 | | -import org.apache.http.HttpResponse; |
| 13 | +import org.apache.http.Header; |
| 14 | +import org.apache.http.HttpEntity; |
| 15 | +import org.apache.http.HttpHeaders; |
10 | 16 | import org.apache.http.client.ClientProtocolException; |
11 | | -import org.apache.http.client.HttpClient; |
12 | 17 | import org.apache.http.client.config.RequestConfig; |
| 18 | +import org.apache.http.client.methods.CloseableHttpResponse; |
13 | 19 | import org.apache.http.client.methods.HttpGet; |
| 20 | +import org.apache.http.client.methods.HttpPost; |
14 | 21 | import org.apache.http.entity.ContentType; |
| 22 | +import org.apache.http.impl.client.CloseableHttpClient; |
15 | 23 | import org.apache.http.impl.client.HttpClientBuilder; |
16 | 24 | import org.apache.http.impl.conn.BasicHttpClientConnectionManager; |
| 25 | +import org.apache.http.util.EntityUtils; |
| 26 | +import org.junit.After; |
17 | 27 | import org.junit.Before; |
18 | 28 | import org.junit.Test; |
19 | 29 |
|
20 | 30 | public class HttpClientLiveTest { |
21 | 31 |
|
22 | 32 | private static final String SAMPLE_URL = "http://www.google.com"; |
23 | 33 |
|
24 | | - private HttpClient instance; |
| 34 | + private CloseableHttpClient instance; |
| 35 | + |
| 36 | + private CloseableHttpResponse response; |
25 | 37 |
|
26 | 38 | @Before |
27 | 39 | public final void before() { |
28 | 40 | instance = HttpClientBuilder.create().build(); |
29 | 41 | } |
30 | 42 |
|
| 43 | + @After |
| 44 | + public final void after() throws IllegalStateException, IOException { |
| 45 | + if (response == null) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + try { |
| 50 | + final HttpEntity entity = response.getEntity(); |
| 51 | + if (entity != null) { |
| 52 | + final InputStream instream = entity.getContent(); |
| 53 | + instream.close(); |
| 54 | + } |
| 55 | + } finally { |
| 56 | + response.close(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
31 | 60 | // tests |
32 | 61 |
|
| 62 | + // simple request - response |
| 63 | + |
33 | 64 | @Test |
34 | 65 | public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
35 | 66 | instance.execute(new HttpGet(SAMPLE_URL)); |
36 | 67 | } |
37 | 68 |
|
38 | 69 | @Test |
39 | 70 | public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { |
40 | | - final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 71 | + response = instance.execute(new HttpGet(SAMPLE_URL)); |
41 | 72 | assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); |
42 | 73 | } |
43 | 74 |
|
44 | 75 | @Test |
45 | 76 | public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { |
46 | | - final HttpResponse response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 77 | + response = instance.execute(new HttpGet(SAMPLE_URL)); |
47 | 78 | final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); |
48 | 79 |
|
49 | 80 | assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); |
50 | 81 | } |
51 | 82 |
|
| 83 | + @Test |
| 84 | + public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException { |
| 85 | + response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 86 | + final String bodyAsString = EntityUtils.toString(response.getEntity()); |
| 87 | + |
| 88 | + assertThat(bodyAsString, notNullValue()); |
| 89 | + } |
| 90 | + |
52 | 91 | @Test(expected = SocketTimeoutException.class) |
53 | 92 | public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException { |
54 | 93 | final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build(); |
55 | 94 | final HttpGet request = new HttpGet(SAMPLE_URL); |
56 | 95 | request.setConfig(requestConfig); |
57 | | - instance.execute(request); |
| 96 | + response = instance.execute(request); |
| 97 | + } |
| 98 | + |
| 99 | + // tests - non-GET |
| 100 | + |
| 101 | + @Test |
| 102 | + public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
| 103 | + instance.execute(new HttpPost(SAMPLE_URL)); |
58 | 104 | } |
59 | 105 |
|
| 106 | + // tests - configs |
| 107 | + |
60 | 108 | @Test |
61 | 109 | public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
62 | 110 | instance = HttpClientBuilder.create().setConnectionManager(new BasicHttpClientConnectionManager()).build(); |
63 | | - instance.execute(new HttpGet(SAMPLE_URL)); |
| 111 | + response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public final void givenRedirectsAreDisabled_whenConsumingUrlWhichRedirects_thenNotRedirected() throws ClientProtocolException, IOException { |
| 116 | + instance = HttpClientBuilder.create().disableRedirectHandling().build(); |
| 117 | + response = instance.execute(new HttpGet("http://t.co/I5YYd9tddw")); |
| 118 | + assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public final void givenCustomHeaderIsSet_whenSendingRequest_thenNoExceptions() throws ClientProtocolException, IOException { |
| 123 | + final HttpGet request = new HttpGet(SAMPLE_URL); |
| 124 | + request.addHeader(HttpHeaders.ACCEPT, "application/xml"); |
| 125 | + response = instance.execute(request); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public final void givenRequestWasSet_whenAnalyzingTheHeadersOfTheResponse_thenCorrect() throws ClientProtocolException, IOException { |
| 130 | + response = instance.execute(new HttpGet(SAMPLE_URL)); |
| 131 | + |
| 132 | + final Header[] headers = response.getHeaders(HttpHeaders.CONTENT_TYPE); |
| 133 | + assertThat(headers, not(emptyArray())); |
64 | 134 | } |
65 | 135 |
|
66 | 136 | } |
0 commit comments