cleanup and testing work
This commit is contained in:
parent
62fd8f8978
commit
b76928dcf5
|
@ -0,0 +1,93 @@
|
|||
package org.baeldung.httpclient;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HttpClientBasicLiveTest {
|
||||
|
||||
private static final String SAMPLE_URL = "http://www.github.com";
|
||||
|
||||
private CloseableHttpClient instance;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
instance = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() throws IllegalStateException, IOException {
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
final InputStream instream = entity.getContent();
|
||||
instream.close();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||
|
||||
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
final String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||
|
||||
assertThat(bodyAsString, notNullValue());
|
||||
}
|
||||
|
||||
// tests - non-GET
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
instance.execute(new HttpPost(SAMPLE_URL));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,7 @@
|
|||
package org.baeldung.httpclient;
|
||||
|
||||
import static org.hamcrest.Matchers.emptyArray;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -16,9 +14,7 @@ import org.apache.http.client.ClientProtocolException;
|
|||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.conn.ConnectTimeoutException;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
|
@ -26,7 +22,6 @@ import org.apache.http.impl.client.HttpClients;
|
|||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
import org.apache.http.params.CoreProtocolPNames;
|
||||
import org.apache.http.params.HttpProtocolParams;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -63,35 +58,6 @@ public class HttpClientLiveTest {
|
|||
|
||||
// tests
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
|
||||
|
||||
assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException {
|
||||
response = instance.execute(new HttpGet(SAMPLE_URL));
|
||||
final String bodyAsString = EntityUtils.toString(response.getEntity());
|
||||
|
||||
assertThat(bodyAsString, notNullValue());
|
||||
}
|
||||
|
||||
@Test(expected = ConnectTimeoutException.class)
|
||||
public final void givenLowTimeout_whenExecutingRequestWithTimeout_thenException() throws ClientProtocolException, IOException {
|
||||
final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50).setSocketTimeout(20).build();
|
||||
|
@ -100,13 +66,6 @@ public class HttpClientLiveTest {
|
|||
response = instance.execute(request);
|
||||
}
|
||||
|
||||
// tests - non-GET
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
instance.execute(new HttpPost(SAMPLE_URL));
|
||||
}
|
||||
|
||||
// tests - configs
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue