Skip to content

Commit 6b45feb

Browse files
author
eugenp
committed
Dependencies and custom logic
1 parent 2448ea7 commit 6b45feb

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎httpclient/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
<version>${commons-lang3.version}</version>
2424
</dependency>
2525

26-
<!-- web -->
26+
<!-- http client -->
27+
28+
<dependency>
29+
<groupId>org.apache.httpcomponents</groupId>
30+
<artifactId>httpclient</artifactId>
31+
<version>${httpclient.version}</version>
32+
</dependency>
2733

2834
<!-- test scoped -->
2935

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
package org.baeldung.mockito;
22

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+
320
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+
566
}

0 commit comments

Comments
 (0)