httpclient cleanup work

This commit is contained in:
eugenp 2014-05-23 23:38:04 +03:00
parent 19281407c1
commit 83ccbf07c0
5 changed files with 10 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
@ -39,7 +40,7 @@ public class HttpClientMultipartTest {
private CloseableHttpResponse response; private CloseableHttpResponse response;
@Before @Before
public final void Before() { public final void before() {
client = HttpClientBuilder.create().build(); client = HttpClientBuilder.create().build();
post = new HttpPost(SERVER); post = new HttpPost(SERVER);
textFileName = "temp.txt"; textFileName = "temp.txt";
@ -53,13 +54,11 @@ public class HttpClientMultipartTest {
try { try {
client.close(); client.close();
} catch (final IOException e1) { } catch (final IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
try { try {
rd.close(); rd.close();
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
@ -73,9 +72,12 @@ public class HttpClientMultipartTest {
} }
} }
// tests
@Test @Test
public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException { public final void givenFileandMultipleTextParts_whenUploadwithAddPart_thenNoExceptions() throws IOException {
final File file = new File(textFileName); final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + textFileName);
final File file = new File(url.getPath());
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);
@ -163,7 +165,9 @@ public class HttpClientMultipartTest {
System.out.println("POST Content Type: " + contentTypeInHeader); System.out.println("POST Content Type: " + contentTypeInHeader);
} }
public String getContent() throws IOException { // UTIL
final String getContent() throws IOException {
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = ""; String body = "";
String content = ""; String content = "";
@ -173,7 +177,7 @@ public class HttpClientMultipartTest {
return content.trim(); return content.trim();
} }
public String getContentTypeHeader() throws IOException { final String getContentTypeHeader() throws IOException {
return post.getEntity().getContentType().toString(); return post.getEntity().getContentType().toString();
} }

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB