11package com .baeldung .httpclient ;
22
3- import org .apache .http .HttpEntity ;
4- import org .apache .http .HttpResponse ;
5- import org .apache .http .NameValuePair ;
6- import org .apache .http .auth .AuthenticationException ;
7- import org .apache .http .auth .UsernamePasswordCredentials ;
8- import org .apache .http .client .entity .UrlEncodedFormEntity ;
9- import org .apache .http .client .fluent .Form ;
10- import org .apache .http .client .fluent .Request ;
11- import org .apache .http .client .methods .CloseableHttpResponse ;
12- import org .apache .http .client .methods .HttpPost ;
13- import org .apache .http .entity .ContentType ;
14- import org .apache .http .entity .StringEntity ;
15- import org .apache .http .entity .mime .MultipartEntityBuilder ;
16- import org .apache .http .impl .auth .BasicScheme ;
17- import org .apache .http .impl .client .CloseableHttpClient ;
18- import org .apache .http .impl .client .HttpClients ;
19- import org .apache .http .message .BasicNameValuePair ;
20- import org .junit .Test ;
3+ import static org .hamcrest .MatcherAssert .assertThat ;
4+ import static org .hamcrest .Matchers .equalTo ;
5+ import static org .junit .jupiter .api .Assertions .assertFalse ;
6+
7+ import org .junit .jupiter .api .Test ;
8+
9+ import org .apache .hc .client5 .http .auth .AuthScope ;
10+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
11+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
12+ import org .apache .hc .client5 .http .entity .UrlEncodedFormEntity ;
13+ import org .apache .hc .client5 .http .entity .mime .MultipartEntityBuilder ;
14+ import org .apache .hc .client5 .http .fluent .Form ;
15+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
16+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
17+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
18+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
19+ import org .apache .hc .core5 .http .ContentType ;
20+ import org .apache .hc .core5 .http .HttpEntity ;
21+ import org .apache .hc .core5 .http .HttpResponse ;
22+ import org .apache .hc .core5 .http .HttpStatus ;
23+ import org .apache .hc .core5 .http .NameValuePair ;
24+ import org .apache .hc .client5 .http .fluent .Request ;
25+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
26+ import org .apache .hc .core5 .http .message .BasicNameValuePair ;
2127
2228import java .io .File ;
2329import java .io .IOException ;
2430import java .util .ArrayList ;
2531import java .util .List ;
2632
27- import static org .hamcrest .Matchers .equalTo ;
28- import static org .junit .Assert .assertFalse ;
29- import static org .junit .Assert .assertThat ;
33+ import com .baeldung .handler .CustomHttpClientResponseHandler ;
3034
3135/*
3236 * NOTE : Need module spring-rest to be running
3337 */
34- public class HttpClientPostingLiveTest {
38+ class HttpClientPostingLiveTest {
3539 private static final String SAMPLE_URL = "http://www.example.com" ;
3640 private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php" ;
3741 private static final String DEFAULT_USER = "test" ;
3842 private static final String DEFAULT_PASS = "test" ;
3943
4044 @ Test
41- public void whenSendPostRequestUsingHttpClient_thenCorrect () throws IOException {
42- final CloseableHttpClient client = HttpClients .createDefault ();
45+ void whenSendPostRequestUsingHttpClient_thenCorrect () throws IOException {
4346 final HttpPost httpPost = new HttpPost (SAMPLE_URL );
44-
4547 final List <NameValuePair > params = new ArrayList <NameValuePair >();
4648 params .add (new BasicNameValuePair ("username" , DEFAULT_USER ));
4749 params .add (new BasicNameValuePair ("password" , DEFAULT_PASS ));
4850 httpPost .setEntity (new UrlEncodedFormEntity (params ));
4951
50- final CloseableHttpResponse response = client .execute (httpPost );
51- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
52- client .close ();
52+ try (CloseableHttpClient client = HttpClients .createDefault ();
53+ CloseableHttpResponse response = (CloseableHttpResponse ) client
54+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
55+
56+ final int statusCode = response .getCode ();
57+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
58+ }
5359 }
5460
5561 @ Test
56- public void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect () throws IOException , AuthenticationException {
57- final CloseableHttpClient client = HttpClients .createDefault ();
62+ void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect () throws IOException {
5863 final HttpPost httpPost = new HttpPost (URL_SECURED_BY_BASIC_AUTHENTICATION );
59-
6064 httpPost .setEntity (new StringEntity ("test post" ));
61- final UsernamePasswordCredentials creds = new UsernamePasswordCredentials (DEFAULT_USER , DEFAULT_PASS );
62- httpPost .addHeader (new BasicScheme ().authenticate (creds , httpPost , null ));
6365
64- final CloseableHttpResponse response = client .execute (httpPost );
65- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
66- client .close ();
66+ final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider ();
67+ final UsernamePasswordCredentials credentials =
68+ new UsernamePasswordCredentials (DEFAULT_USER , DEFAULT_PASS .toCharArray ());
69+
70+ credsProvider .setCredentials (new AuthScope (URL_SECURED_BY_BASIC_AUTHENTICATION , 80 ) ,credentials );
71+
72+ try (CloseableHttpClient client = HttpClients .custom ()
73+ .setDefaultCredentialsProvider (credsProvider )
74+ .build ();
75+
76+ CloseableHttpResponse response = (CloseableHttpResponse ) client
77+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
78+
79+ final int statusCode = response .getCode ();
80+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
81+ }
6782 }
6883
6984 @ Test
70- public void whenPostJsonUsingHttpClient_thenCorrect () throws IOException {
71- final CloseableHttpClient client = HttpClients .createDefault ();
85+ void whenPostJsonUsingHttpClient_thenCorrect () throws IOException {
7286 final HttpPost httpPost = new HttpPost (SAMPLE_URL );
7387
7488 final String json = "{\" id\" :1,\" name\" :\" John\" }" ;
@@ -77,68 +91,91 @@ public void whenPostJsonUsingHttpClient_thenCorrect() throws IOException {
7791 httpPost .setHeader ("Accept" , "application/json" );
7892 httpPost .setHeader ("Content-type" , "application/json" );
7993
80- final CloseableHttpResponse response = client .execute (httpPost );
81- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
82- client .close ();
94+ try (CloseableHttpClient client = HttpClients .createDefault ();
95+ CloseableHttpResponse response = (CloseableHttpResponse ) client
96+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
97+
98+ final int statusCode = response .getCode ();
99+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
100+ }
83101 }
84102
85103 @ Test
86- public void whenPostFormUsingHttpClientFluentAPI_thenCorrect () throws IOException {
87- final HttpResponse response = Request .Post (SAMPLE_URL ).bodyForm (Form .form ().add ("username" , DEFAULT_USER ).add ("password" , DEFAULT_PASS ).build ()).execute ().returnResponse ();
88-
89- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
104+ void whenPostFormUsingHttpClientFluentAPI_thenCorrect () throws IOException {
105+ Request request = Request .post (SAMPLE_URL )
106+ .bodyForm (Form .form ()
107+ .add ("username" , DEFAULT_USER )
108+ .add ("password" , DEFAULT_PASS )
109+ .build ());
110+
111+ HttpResponse response = request .execute ()
112+ .returnResponse ();
113+ assertThat (response .getCode (), equalTo (HttpStatus .SC_OK ));
90114 }
91115
92116 @ Test
93- public void whenSendMultipartRequestUsingHttpClient_thenCorrect () throws IOException {
94- final CloseableHttpClient client = HttpClients .createDefault ();
117+ void whenSendMultipartRequestUsingHttpClient_thenCorrect () throws IOException {
95118 final HttpPost httpPost = new HttpPost (SAMPLE_URL );
96119
97120 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
98121 builder .addTextBody ("username" , DEFAULT_USER );
99122 builder .addTextBody ("password" , DEFAULT_PASS );
100- builder .addBinaryBody ("file" , new File ( "src/test/resources/test.in" ), ContentType . APPLICATION_OCTET_STREAM , "file.ext" );
101- final HttpEntity multipart = builder . build ( );
123+ builder .addBinaryBody (
124+ "file" , new File ( "src/test/resources/test.in" ), ContentType . APPLICATION_OCTET_STREAM , "file.ext" );
102125
126+ final HttpEntity multipart = builder .build ();
103127 httpPost .setEntity (multipart );
104128
105- final CloseableHttpResponse response = client .execute (httpPost );
106- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
107- client .close ();
129+ try (CloseableHttpClient client = HttpClients .createDefault ();
130+ CloseableHttpResponse response = (CloseableHttpResponse ) client
131+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
132+
133+ final int statusCode = response .getCode ();
134+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
135+ }
108136 }
109137
110138 @ Test
111- public void whenUploadFileUsingHttpClient_thenCorrect () throws IOException {
112- final CloseableHttpClient client = HttpClients .createDefault ();
139+ void whenUploadFileUsingHttpClient_thenCorrect () throws IOException {
113140 final HttpPost httpPost = new HttpPost (SAMPLE_URL );
114141
115142 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
116- builder .addBinaryBody ("file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
143+ builder .addBinaryBody (
144+ "file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
117145 final HttpEntity multipart = builder .build ();
118146
119147 httpPost .setEntity (multipart );
120148
121- final CloseableHttpResponse response = client .execute (httpPost );
122- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
123- client .close ();
149+ try (CloseableHttpClient client = HttpClients .createDefault ();
150+ CloseableHttpResponse response = (CloseableHttpResponse ) client
151+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
152+
153+ final int statusCode = response .getCode ();
154+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
155+ }
124156 }
125157
126158 @ Test
127- public void whenGetUploadFileProgressUsingHttpClient_thenCorrect () throws IOException {
128- final CloseableHttpClient client = HttpClients .createDefault ();
159+ void whenGetUploadFileProgressUsingHttpClient_thenCorrect () throws IOException {
129160 final HttpPost httpPost = new HttpPost (SAMPLE_URL );
130161
131162 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
132- builder .addBinaryBody ("file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
163+ builder .addBinaryBody (
164+ "file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
133165 final HttpEntity multipart = builder .build ();
134166
135- final ProgressEntityWrapper .ProgressListener pListener = percentage -> assertFalse (Float .compare (percentage , 100 ) > 0 );
167+ final ProgressEntityWrapper .ProgressListener pListener =
168+ percentage -> assertFalse (Float .compare (percentage , 100 ) > 0 );
136169
137170 httpPost .setEntity (new ProgressEntityWrapper (multipart , pListener ));
138171
139- final CloseableHttpResponse response = client .execute (httpPost );
140- assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
141- client .close ();
172+ try (CloseableHttpClient client = HttpClients .createDefault ();
173+ CloseableHttpResponse response = (CloseableHttpResponse ) client
174+ .execute (httpPost , new CustomHttpClientResponseHandler ())) {
175+
176+ final int statusCode = response .getCode ();
177+ assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
178+ }
142179 }
143180
144181}
0 commit comments