Merge pull request #13 from egmp777/master

HttpClient Multi Part Form Upload Tests
This commit is contained in:
Eugen 2014-05-23 10:03:27 +03:00
commit 3d7ca2e81c
4 changed files with 101 additions and 63 deletions

BIN
httpclient/image.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -1,5 +1,8 @@
package org.baeldung.httpclient; package org.baeldung.httpclient;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -9,62 +12,89 @@ import java.io.InputStreamReader;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody; import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.BeforeClass; import org.junit.After;
import org.junit.Ignore; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class HttpClientMultipartTest { public class HttpClientMultipartTest {
private static final String SERVER = "http://cgi-lib.berkeley.edu/ex/fup.cgi"; private static final String SERVER = "http://echo.200please.com";
private static final String SERVER2 = "http://posttestserver.com/post.php"; private CloseableHttpClient client;
private static final String SERVER3 = "http://postcatcher.in/catchers/53765b0349c306020000077b"; private HttpPost post;
private static final String SERVER4 = "http://echo.200please.com"; private String textFileName;
private static final String SERVER5 = "http://greensuisse.zzl.org/product/dump/dump.php"; private String imageFileName;
private static final String SERVER6 = "http://www.newburghschools.org/testfolder/dump.php"; private String zipFileName;
private static HttpClient client; private BufferedReader rd;
private static HttpPost post; private CloseableHttpResponse response;
private static String textFileName;
private static String imageFileName;
private static String zipFileName;
@BeforeClass @Before
public static void setUpBeforeClass() throws Exception { public final void Before() {
client = HttpClientBuilder.create().build(); client = HttpClientBuilder.create().build();
post = new HttpPost(SERVER2 /*"fup.cgi"*/); post = new HttpPost(SERVER);
textFileName = ".\temp.txt"; textFileName = "temp.txt";
imageFileName = "image.jpg"; imageFileName = "image.jpg";
zipFileName = "zipFile.zip"; zipFileName = "zipFile.zip";
} }
@After
public final void after() throws IllegalStateException, IOException {
post.completed();
try {
client.close();
} catch (final IOException e1) {
e1.printStackTrace();
}
try {
rd.close();
} catch (final IOException e) {
e.printStackTrace();
}
try {
final HttpEntity entity = response.getEntity();
if (entity != null) {
final InputStream instream = entity.getContent();
instream.close();
}
} finally {
response.close();
}
}
@Test @Test
@Ignore
public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
final File file = new File(textFileName); final File file = new File(textFileName);
final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA); final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("submitted", fileBody); builder.addPart("upfile", fileBody);
builder.addPart("note", stringBody1); builder.addPart("text1", stringBody1);
builder.addPart("note2", stringBody2); builder.addPart("text2", stringBody2);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); response = client.execute(post);
System.out.println(getContent(response)); final int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
System.out.println(getContent());
final Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
assertThat(headers.length, equalTo(5));
for (final Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
@ -72,92 +102,99 @@ public class HttpClientMultipartTest {
} }
@Test @Test
@Ignore
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final File file = new File(textFileName); final File file = new File(textFileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, textFileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); response = client.execute(post);
System.out.println(getContent(response)); final int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
System.out.println(getContent());
final Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
assertThat(headers.length, equalTo(5));
for (final Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
} }
@Test @Test
@Ignore public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
public final void whenUploadWithAddBinaryBody_NoType_andAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
final InputStream inputStream = new FileInputStream(zipFileName);
final File file = new File(imageFileName); final File file = new File(imageFileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("submitted", file, ContentType.DEFAULT_BINARY, textFileName); builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, imageFileName);
// builder.addBinaryBody("upfile", fileBin); builder.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), zipFileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN); builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); response = client.execute(post);
System.out.println(getContent(response)); final int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
System.out.println(getContent());
final Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
assertThat(headers.length, equalTo(5));
for (final Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
inputStream.close();
} }
@Test @Test
@Ignore public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
public final void whenUploadWithAddBinaryBody_InputStream_andTextBody_ThenNoException() throws ClientProtocolException, IOException {
final InputStream inputStream = new FileInputStream(zipFileName);
final String message = "This is a multipart post"; final String message = "This is a multipart post";
final byte[] bytes = "binary code".getBytes();
final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// builder.addBinaryBody("submitted", inputStream, ContentType.create("application/zip"), "zipFileName"); builder.addBinaryBody("upfile", bytes, ContentType.DEFAULT_BINARY, textFileName);
builder.addBinaryBody("upfile", inputStream, ContentType.create("application/zip"), "zipFileName"); builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build(); final HttpEntity entity = builder.build();
post.setEntity(entity); post.setEntity(entity);
final HttpResponse response = client.execute(post); response = client.execute(post);
final int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
System.out.println(getContent());
System.out.println(getContent(response));
final Header[] headers = response.getAllHeaders(); final Header[] headers = response.getAllHeaders();
assertThat(headers.length, equalTo(5));
for (final Header thisHeader : headers) { for (final Header thisHeader : headers) {
System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); System.out.println(thisHeader.getName() + ":" + thisHeader.getValue());
} }
} }
// BUG public String getContent() throws IOException {
@Test rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
public final void whenFluentRequestWithBody_ThenNoException() throws IOException {
final String fileName = ".\temp.txt";
final File fileBin = new File(fileName);
final String message = "This is a multipart post";
final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("upfile", fileBin, ContentType.DEFAULT_BINARY, fileName);
builder.addTextBody("note", message, ContentType.TEXT_PLAIN);
final HttpEntity entity = builder.build();
final Response response = Request.Post(SERVER).body(entity).execute();
}
public static String getContent(final HttpResponse response) throws IOException {
final BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = ""; String body = "";
String content = ""; String content = "";
while ((body = rd.readLine()) != null) { while ((body = rd.readLine()) != null) {
content += body + "\n"; content += body + "\n";
} }
return content.trim(); return content.trim();
} }
} }

1
httpclient/temp.txt Normal file
View File

@ -0,0 +1 @@
Hello theren

BIN
httpclient/zipFile.zip Normal file

Binary file not shown.