1+ package org .baeldung .httpclient ;
2+
3+ import static org .hamcrest .Matchers .equalTo ;
4+ import static org .junit .Assert .assertFalse ;
5+ import static org .junit .Assert .assertThat ;
6+
7+ import java .io .File ;
8+ import java .io .IOException ;
9+ import java .util .ArrayList ;
10+ import java .util .List ;
11+
12+ import org .apache .http .HttpEntity ;
13+ import org .apache .http .HttpResponse ;
14+ import org .apache .http .NameValuePair ;
15+ import org .apache .http .auth .AuthenticationException ;
16+ import org .apache .http .auth .UsernamePasswordCredentials ;
17+ import org .apache .http .client .ClientProtocolException ;
18+ import org .apache .http .client .entity .UrlEncodedFormEntity ;
19+ import org .apache .http .client .fluent .Form ;
20+ import org .apache .http .client .fluent .Request ;
21+ import org .apache .http .client .methods .CloseableHttpResponse ;
22+ import org .apache .http .client .methods .HttpPost ;
23+ import org .apache .http .entity .ContentType ;
24+ import org .apache .http .entity .StringEntity ;
25+ import org .apache .http .entity .mime .MultipartEntityBuilder ;
26+ import org .apache .http .impl .auth .BasicScheme ;
27+ import org .apache .http .impl .client .CloseableHttpClient ;
28+ import org .apache .http .impl .client .HttpClients ;
29+ import org .apache .http .message .BasicNameValuePair ;
30+ import org .junit .Test ;
31+
32+ public class HttpClientPostingTest {
33+ private static final String SAMPLE_URL = "http://localhost:8080/spring-rest/users" ;
34+ private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php" ;
35+ private static final String DEFAULT_USER = "test" ;
36+ private static final String DEFAULT_PASS = "test" ;
37+
38+ @ Test
39+ public void whenSendPostRequestUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException {
40+ final CloseableHttpClient client = HttpClients .createDefault ();
41+ final HttpPost httpPost = new HttpPost (SAMPLE_URL );
42+
43+ final List <NameValuePair > params = new ArrayList <NameValuePair >();
44+ params .add (new BasicNameValuePair ("username" , DEFAULT_USER ));
45+ params .add (new BasicNameValuePair ("password" , DEFAULT_PASS ));
46+ httpPost .setEntity (new UrlEncodedFormEntity (params ));
47+
48+ final CloseableHttpResponse response = client .execute (httpPost );
49+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
50+ client .close ();
51+ }
52+
53+ @ Test
54+ public void whenSendPostRequestWithAuthorizationUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException , AuthenticationException {
55+ final CloseableHttpClient client = HttpClients .createDefault ();
56+ final HttpPost httpPost = new HttpPost (URL_SECURED_BY_BASIC_AUTHENTICATION );
57+
58+ httpPost .setEntity (new StringEntity ("test post" ));
59+ final UsernamePasswordCredentials creds = new UsernamePasswordCredentials (DEFAULT_USER , DEFAULT_PASS );
60+ httpPost .addHeader (new BasicScheme ().authenticate (creds , httpPost , null ));
61+
62+ final CloseableHttpResponse response = client .execute (httpPost );
63+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
64+ client .close ();
65+ }
66+
67+ @ Test
68+ public void whenPostJsonUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException {
69+ final CloseableHttpClient client = HttpClients .createDefault ();
70+ final HttpPost httpPost = new HttpPost (SAMPLE_URL + "/detail" );
71+
72+ final String json = "{\" id\" :1,\" name\" :\" John\" }" ;
73+ final StringEntity entity = new StringEntity (json );
74+ httpPost .setEntity (entity );
75+ httpPost .setHeader ("Accept" , "application/json" );
76+ httpPost .setHeader ("Content-type" , "application/json" );
77+
78+ final CloseableHttpResponse response = client .execute (httpPost );
79+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
80+ client .close ();
81+ }
82+
83+ @ Test
84+ public void whenPostFormUsingHttpClientFluentAPI_thenCorrect () throws ClientProtocolException , IOException {
85+ final HttpResponse response = Request .Post (SAMPLE_URL ).bodyForm (Form .form ().add ("username" , DEFAULT_USER ).add ("password" , DEFAULT_PASS ).build ()).execute ().returnResponse ();
86+
87+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
88+ }
89+
90+ @ Test
91+ public void whenSendMultipartRequestUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException {
92+ final CloseableHttpClient client = HttpClients .createDefault ();
93+ final HttpPost httpPost = new HttpPost (SAMPLE_URL + "/multipart" );
94+
95+ final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
96+ builder .addTextBody ("username" , DEFAULT_USER );
97+ builder .addTextBody ("password" , DEFAULT_PASS );
98+ builder .addBinaryBody ("file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
99+ final HttpEntity multipart = builder .build ();
100+
101+ httpPost .setEntity (multipart );
102+
103+ final CloseableHttpResponse response = client .execute (httpPost );
104+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
105+ client .close ();
106+ }
107+
108+ @ Test
109+ public void whenUploadFileUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException {
110+ final CloseableHttpClient client = HttpClients .createDefault ();
111+ final HttpPost httpPost = new HttpPost (SAMPLE_URL + "/upload" );
112+
113+ final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
114+ builder .addBinaryBody ("file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
115+ final HttpEntity multipart = builder .build ();
116+
117+ httpPost .setEntity (multipart );
118+
119+ final CloseableHttpResponse response = client .execute (httpPost );
120+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
121+ client .close ();
122+ }
123+
124+ @ Test
125+ public void whenGetUploadFileProgressUsingHttpClient_thenCorrect () throws ClientProtocolException , IOException {
126+ final CloseableHttpClient client = HttpClients .createDefault ();
127+ final HttpPost httpPost = new HttpPost (SAMPLE_URL + "/upload" );
128+
129+ final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
130+ builder .addBinaryBody ("file" , new File ("src/test/resources/test.in" ), ContentType .APPLICATION_OCTET_STREAM , "file.ext" );
131+ final HttpEntity multipart = builder .build ();
132+
133+ final ProgressEntityWrapper .ProgressListener pListener = new ProgressEntityWrapper .ProgressListener () {
134+ @ Override
135+ public void progress (final float percentage ) {
136+ assertFalse (Float .compare (percentage , 100 ) > 0 );
137+ }
138+ };
139+
140+ httpPost .setEntity (new ProgressEntityWrapper (multipart , pListener ));
141+
142+ final CloseableHttpResponse response = client .execute (httpPost );
143+ assertThat (response .getStatusLine ().getStatusCode (), equalTo (200 ));
144+ client .close ();
145+ }
146+
147+ }
0 commit comments