Skip to content

Commit 1ea85b4

Browse files
author
eugenp
committed
new examples for cookbook
1 parent 6b45feb commit 1ea85b4

1 file changed

Lines changed: 77 additions & 7 deletions

File tree

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,136 @@
11
package org.baeldung.mockito;
22

3+
import static org.hamcrest.Matchers.emptyArray;
34
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.not;
6+
import static org.hamcrest.Matchers.notNullValue;
47
import static org.junit.Assert.assertThat;
58

69
import java.io.IOException;
10+
import java.io.InputStream;
711
import java.net.SocketTimeoutException;
812

9-
import org.apache.http.HttpResponse;
13+
import org.apache.http.Header;
14+
import org.apache.http.HttpEntity;
15+
import org.apache.http.HttpHeaders;
1016
import org.apache.http.client.ClientProtocolException;
11-
import org.apache.http.client.HttpClient;
1217
import org.apache.http.client.config.RequestConfig;
18+
import org.apache.http.client.methods.CloseableHttpResponse;
1319
import org.apache.http.client.methods.HttpGet;
20+
import org.apache.http.client.methods.HttpPost;
1421
import org.apache.http.entity.ContentType;
22+
import org.apache.http.impl.client.CloseableHttpClient;
1523
import org.apache.http.impl.client.HttpClientBuilder;
1624
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
25+
import org.apache.http.util.EntityUtils;
26+
import org.junit.After;
1727
import org.junit.Before;
1828
import org.junit.Test;
1929

2030
public class HttpClientLiveTest {
2131

2232
private static final String SAMPLE_URL = "http://www.google.com";
2333

24-
private HttpClient instance;
34+
private CloseableHttpClient instance;
35+
36+
private CloseableHttpResponse response;
2537

2638
@Before
2739
public final void before() {
2840
instance = HttpClientBuilder.create().build();
2941
}
3042

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+
3160
// tests
3261

62+
// simple request - response
63+
3364
@Test
3465
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
3566
instance.execute(new HttpGet(SAMPLE_URL));
3667
}
3768

3869
@Test
3970
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));
4172
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
4273
}
4374

4475
@Test
4576
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));
4778
final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
4879

4980
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
5081
}
5182

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+
5291
@Test(expected = SocketTimeoutException.class)
5392
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
5493
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(50).build();
5594
final HttpGet request = new HttpGet(SAMPLE_URL);
5695
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));
58104
}
59105

106+
// tests - configs
107+
60108
@Test
61109
public final void givenHttpClientIsConfiguredWithCustomConnectionManager_whenExecutingRequest_thenNoExceptions() throws ClientProtocolException, IOException {
62110
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()));
64134
}
65135

66136
}

0 commit comments

Comments
 (0)