Skip to content

Commit b76928d

Browse files
author
eugenp
committed
cleanup and testing work
1 parent 62fd8f8 commit b76928d

2 files changed

Lines changed: 93 additions & 41 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.baeldung.httpclient;
2+
3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.hamcrest.Matchers.notNullValue;
5+
import static org.junit.Assert.assertThat;
6+
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
10+
import org.apache.http.HttpEntity;
11+
import org.apache.http.HttpStatus;
12+
import org.apache.http.client.ClientProtocolException;
13+
import org.apache.http.client.methods.CloseableHttpResponse;
14+
import org.apache.http.client.methods.HttpGet;
15+
import org.apache.http.client.methods.HttpPost;
16+
import org.apache.http.entity.ContentType;
17+
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.HttpClientBuilder;
19+
import org.apache.http.util.EntityUtils;
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
public class HttpClientBasicLiveTest {
25+
26+
private static final String SAMPLE_URL = "http://www.github.com";
27+
28+
private CloseableHttpClient instance;
29+
30+
private CloseableHttpResponse response;
31+
32+
@Before
33+
public final void before() {
34+
instance = HttpClientBuilder.create().build();
35+
}
36+
37+
@After
38+
public final void after() throws IllegalStateException, IOException {
39+
if (response == null) {
40+
return;
41+
}
42+
43+
try {
44+
final HttpEntity entity = response.getEntity();
45+
if (entity != null) {
46+
final InputStream instream = entity.getContent();
47+
instream.close();
48+
}
49+
} finally {
50+
response.close();
51+
}
52+
}
53+
54+
// tests
55+
56+
// simple request - response
57+
58+
@Test
59+
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
60+
response = instance.execute(new HttpGet(SAMPLE_URL));
61+
}
62+
63+
@Test
64+
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
65+
response = instance.execute(new HttpGet(SAMPLE_URL));
66+
int statusCode = response.getStatusLine().getStatusCode();
67+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
68+
}
69+
70+
@Test
71+
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException {
72+
response = instance.execute(new HttpGet(SAMPLE_URL));
73+
final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
74+
75+
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
76+
}
77+
78+
@Test
79+
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException {
80+
response = instance.execute(new HttpGet(SAMPLE_URL));
81+
final String bodyAsString = EntityUtils.toString(response.getEntity());
82+
83+
assertThat(bodyAsString, notNullValue());
84+
}
85+
86+
// tests - non-GET
87+
88+
@Test
89+
public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException {
90+
instance.execute(new HttpPost(SAMPLE_URL));
91+
}
92+
93+
}

‎httpclient/src/test/java/org/baeldung/httpclient/HttpClientLiveTest.java‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package org.baeldung.httpclient;
22

33
import static org.hamcrest.Matchers.emptyArray;
4-
import static org.hamcrest.Matchers.equalTo;
54
import static org.hamcrest.Matchers.not;
6-
import static org.hamcrest.Matchers.notNullValue;
75
import static org.junit.Assert.assertThat;
86

97
import java.io.IOException;
@@ -16,17 +14,14 @@
1614
import org.apache.http.client.config.RequestConfig;
1715
import org.apache.http.client.methods.CloseableHttpResponse;
1816
import org.apache.http.client.methods.HttpGet;
19-
import org.apache.http.client.methods.HttpPost;
2017
import org.apache.http.conn.ConnectTimeoutException;
21-
import org.apache.http.entity.ContentType;
2218
import org.apache.http.impl.client.CloseableHttpClient;
2319
import org.apache.http.impl.client.DefaultHttpClient;
2420
import org.apache.http.impl.client.HttpClientBuilder;
2521
import org.apache.http.impl.client.HttpClients;
2622
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
2723
import org.apache.http.params.CoreProtocolPNames;
2824
import org.apache.http.params.HttpProtocolParams;
29-
import org.apache.http.util.EntityUtils;
3025
import org.junit.After;
3126
import org.junit.Before;
3227
import org.junit.Test;
@@ -63,35 +58,6 @@ public final void after() throws IllegalStateException, IOException {
6358

6459
// tests
6560

66-
// simple request - response
67-
68-
@Test
69-
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
70-
response = instance.execute(new HttpGet(SAMPLE_URL));
71-
}
72-
73-
@Test
74-
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
75-
response = instance.execute(new HttpGet(SAMPLE_URL));
76-
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
77-
}
78-
79-
@Test
80-
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException {
81-
response = instance.execute(new HttpGet(SAMPLE_URL));
82-
final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
83-
84-
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
85-
}
86-
87-
@Test
88-
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException {
89-
response = instance.execute(new HttpGet(SAMPLE_URL));
90-
final String bodyAsString = EntityUtils.toString(response.getEntity());
91-
92-
assertThat(bodyAsString, notNullValue());
93-
}
94-
9561
@Test(expected = ConnectTimeoutException.class)
9662
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
9763
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
@@ -100,13 +66,6 @@ public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException(
10066
response = instance.execute(request);
10167
}
10268

103-
// tests - non-GET
104-
105-
@Test
106-
public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException {
107-
instance.execute(new HttpPost(SAMPLE_URL));
108-
}
109-
11069
// tests - configs
11170

11271
@Test

0 commit comments

Comments
 (0)