Skip to content

Commit 3d7ca2e

Browse files
author
Eugen
committed
Merge pull request eugenp#13 from egmp777/master
HttpClient Multi Part Form Upload Tests
2 parents f410435 + d1d71b6 commit 3d7ca2e

File tree

4 files changed

+102
-64
lines changed

4 files changed

+102
-64
lines changed

httpclient/image.jpg

4.23 KB
Loading
Lines changed: 101 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.baeldung.httpclient;
22

3+
import static org.hamcrest.Matchers.equalTo;
4+
import static org.junit.Assert.assertThat;
5+
36
import java.io.BufferedReader;
47
import java.io.File;
58
import java.io.FileInputStream;
@@ -9,155 +12,189 @@
912

1013
import org.apache.http.Header;
1114
import org.apache.http.HttpEntity;
12-
import org.apache.http.HttpResponse;
15+
import org.apache.http.HttpStatus;
1316
import org.apache.http.client.ClientProtocolException;
14-
import org.apache.http.client.HttpClient;
15-
import org.apache.http.client.fluent.Request;
16-
import org.apache.http.client.fluent.Response;
17+
import org.apache.http.client.methods.CloseableHttpResponse;
1718
import org.apache.http.client.methods.HttpPost;
1819
import org.apache.http.entity.ContentType;
1920
import org.apache.http.entity.mime.HttpMultipartMode;
2021
import org.apache.http.entity.mime.MultipartEntityBuilder;
2122
import org.apache.http.entity.mime.content.FileBody;
2223
import org.apache.http.entity.mime.content.StringBody;
24+
import org.apache.http.impl.client.CloseableHttpClient;
2325
import org.apache.http.impl.client.HttpClientBuilder;
24-
import org.junit.BeforeClass;
25-
import org.junit.Ignore;
26+
import org.junit.After;
27+
import org.junit.Before;
2628
import org.junit.Test;
2729

2830
public class HttpClientMultipartTest {
2931

30-
private static final String SERVER = "http://cgi-lib.berkeley.edu/ex/fup.cgi";
31-
private static final String SERVER2 = "http://posttestserver.com/post.php";
32-
private static final String SERVER3 = "http://postcatcher.in/catchers/53765b0349c306020000077b";
33-
private static final String SERVER4 = "http://echo.200please.com";
34-
private static final String SERVER5 = "http://greensuisse.zzl.org/product/dump/dump.php";
35-
private static final String SERVER6 = "http://www.newburghschools.org/testfolder/dump.php";
36-
private static HttpClient client;
37-
private static HttpPost post;
38-
private static String textFileName;
39-
private static String imageFileName;
40-
private static String zipFileName;
41-
42-
@BeforeClass
43-
public static void setUpBeforeClass() throws Exception {
32+
private static final String SERVER = "http://echo.200please.com";
33+
private CloseableHttpClient client;
34+
private HttpPost post;
35+
private String textFileName;
36+
private String imageFileName;
37+
private String zipFileName;
38+
private BufferedReader rd;
39+
private CloseableHttpResponse response;
40+
41+
@Before
42+
public final void Before() {
4443
client = HttpClientBuilder.create().build();
45-
post = new HttpPost(SERVER2 /*"fup.cgi"*/);
46-
textFileName = ".\temp.txt";
44+
post = new HttpPost(SERVER);
45+
textFileName = "temp.txt";
4746
imageFileName = "image.jpg";
4847
zipFileName = "zipFile.zip";
4948
}
5049

50+
@After
51+
public final void after() throws IllegalStateException, IOException {
52+
post.completed();
53+
try {
54+
client.close();
55+
} catch (final IOException e1) {
56+
57+
e1.printStackTrace();
58+
}
59+
try {
60+
rd.close();
61+
} catch (final IOException e) {
62+
63+
e.printStackTrace();
64+
}
65+
try {
66+
final HttpEntity entity = response.getEntity();
67+
if (entity != null) {
68+
final InputStream instream = entity.getContent();
69+
instream.close();
70+
}
71+
} finally {
72+
response.close();
73+
}
74+
}
75+
5176
@Test
52-
@Ignore
5377
public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
78+
5479
final File file = new File(textFileName);
5580
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
5681
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
5782
final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
5883
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
5984
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
60-
builder.addPart("submitted", fileBody);
61-
builder.addPart("note", stringBody1);
62-
builder.addPart("note2", stringBody2);
85+
builder.addPart("upfile", fileBody);
86+
builder.addPart("text1", stringBody1);
87+
builder.addPart("text2", stringBody2);
6388
final HttpEntity entity = builder.build();
6489
post.setEntity(entity);
65-
final HttpResponse response = client.execute(post);
66-
System.out.println(getContent(response));
90+
response = client.execute(post);
91+
final int statusCode = response.getStatusLine().getStatusCode();
92+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
93+
94+
System.out.println(getContent());
95+
6796
final Header[] headers = response.getAllHeaders();
97+
assertThat(headers.length, equalTo(5));
6898

6999
for (final Header thisHeader : headers) {
70100
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
71101
}
72102
}
73103

74104
@Test
75-
@Ignore
76105
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
106+
77107
final File file = new File(textFileName);
78108
final String message = "This is a multipart post";
79109
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
80110
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
81-
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
82-
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
111+
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, textFileName);
112+
builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
83113
final HttpEntity entity = builder.build();
84114
post.setEntity(entity);
85-
final HttpResponse response = client.execute(post);
86-
System.out.println(getContent(response));
115+
response = client.execute(post);
116+
final int statusCode = response.getStatusLine().getStatusCode();
117+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
118+
119+
System.out.println(getContent());
120+
87121
final Header[] headers = response.getAllHeaders();
122+
assertThat(headers.length, equalTo(5));
88123

89124
for (final Header thisHeader : headers) {
90125
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
91126
}
127+
92128
}
93129

94130
@Test
95-
@Ignore
96-
public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
131+
public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
132+
133+
final InputStream inputStream = new FileInputStream(zipFileName);
97134
final File file = new File(imageFileName);
98135
final String message = "This is a multipart post";
99136
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
100137
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
101-
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName);
102-
// builder.addBinaryBody("upfile", fileBin);
103-
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
138+
builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, imageFileName);
139+
builder.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), zipFileName);
140+
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
104141
final HttpEntity entity = builder.build();
105142
post.setEntity(entity);
106-
final HttpResponse response = client.execute(post);
107-
System.out.println(getContent(response));
143+
response = client.execute(post);
144+
final int statusCode = response.getStatusLine().getStatusCode();
145+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
146+
147+
System.out.println(getContent());
148+
108149
final Header[] headers = response.getAllHeaders();
150+
assertThat(headers.length, equalTo(5));
109151

110152
for (final Header thisHeader : headers) {
111153
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
112154
}
155+
156+
inputStream.close();
157+
113158
}
114159

115160
@Test
116-
@Ignore
117-
public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
118-
final InputStream inputStream = new FileInputStream(zipFileName);
161+
public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
162+
119163
final String message = "This is a multipart post";
164+
final byte[] bytes = "binary code".getBytes();
120165
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
121166
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
122-
// builder.addBinaryBody("submitted", inputStream, ContentType.create("application/zip"), "zipFileName");
123-
builder.addBinaryBody("upfile", inputStream, ContentType.create("application/zip"), "zipFileName");
124-
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
167+
builder.addBinaryBody("upfile", bytes, ContentType.DEFAULT_BINARY, textFileName);
168+
builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
125169
final HttpEntity entity = builder.build();
126170
post.setEntity(entity);
127-
final HttpResponse response = client.execute(post);
171+
response = client.execute(post);
172+
final int statusCode = response.getStatusLine().getStatusCode();
173+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
174+
175+
System.out.println(getContent());
128176

129-
System.out.println(getContent(response));
130177
final Header[] headers = response.getAllHeaders();
178+
assertThat(headers.length, equalTo(5));
131179

132180
for (final Header thisHeader : headers) {
133181
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
134182
}
135-
}
136-
137-
// BUG
138183

139-
@Test
140-
public final void whenFluentRequestWithBody_ThenNoException() throws IOException {
141-
final String fileName = ".\temp.txt";
142-
final File fileBin = new File(fileName);
143-
final String message = "This is a multipart post";
144-
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
145-
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
146-
builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName);
147-
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
148-
final HttpEntity entity = builder.build();
149-
final Response response = Request.Post(SERVER).body(entity).execute();
150184
}
151185

152-
public static String getContent(final HttpResponse response) throws IOException {
153-
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
186+
public String getContent() throws IOException {
187+
188+
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
154189
String body = "";
155190
String content = "";
156191

157192
while ((body = rd.readLine()) != null) {
158193
content += body + "\n";
159194
}
195+
160196
return content.trim();
197+
161198
}
162199

163200
}

httpclient/temp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello theren

httpclient/zipFile.zip

282 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)