|
1 | 1 | package org.baeldung.httpclient; |
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | + |
3 | 6 | import java.io.BufferedReader; |
4 | 7 | import java.io.File; |
5 | 8 | import java.io.FileInputStream; |
|
9 | 12 |
|
10 | 13 | import org.apache.http.Header; |
11 | 14 | import org.apache.http.HttpEntity; |
12 | | -import org.apache.http.HttpResponse; |
| 15 | +import org.apache.http.HttpStatus; |
13 | 16 | 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; |
17 | 18 | import org.apache.http.client.methods.HttpPost; |
18 | 19 | import org.apache.http.entity.ContentType; |
19 | 20 | import org.apache.http.entity.mime.HttpMultipartMode; |
20 | 21 | import org.apache.http.entity.mime.MultipartEntityBuilder; |
21 | 22 | import org.apache.http.entity.mime.content.FileBody; |
22 | 23 | import org.apache.http.entity.mime.content.StringBody; |
| 24 | +import org.apache.http.impl.client.CloseableHttpClient; |
23 | 25 | 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; |
26 | 28 | import org.junit.Test; |
27 | 29 |
|
28 | 30 | public class HttpClientMultipartTest { |
29 | 31 |
|
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() { |
44 | 43 | client = HttpClientBuilder.create().build(); |
45 | | - post = new HttpPost(SERVER2 /*"fup.cgi"*/); |
46 | | - textFileName = ".\temp.txt"; |
| 44 | + post = new HttpPost(SERVER); |
| 45 | + textFileName = "temp.txt"; |
47 | 46 | imageFileName = "image.jpg"; |
48 | 47 | zipFileName = "zipFile.zip"; |
49 | 48 | } |
50 | 49 |
|
| 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 | + |
51 | 76 | @Test |
52 | | - @Ignore |
53 | 77 | public final void whenUploadWithAddPart_thenNoExceptions() throws IOException { |
| 78 | + |
54 | 79 | final File file = new File(textFileName); |
55 | 80 | final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY); |
56 | 81 | final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA); |
57 | 82 | final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA); |
58 | 83 | final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
59 | 84 | 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); |
63 | 88 | final HttpEntity entity = builder.build(); |
64 | 89 | 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 | + |
67 | 96 | final Header[] headers = response.getAllHeaders(); |
| 97 | + assertThat(headers.length, equalTo(5)); |
68 | 98 |
|
69 | 99 | for (final Header thisHeader : headers) { |
70 | 100 | System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
71 | 101 | } |
72 | 102 | } |
73 | 103 |
|
74 | 104 | @Test |
75 | | - @Ignore |
76 | 105 | public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException { |
| 106 | + |
77 | 107 | final File file = new File(textFileName); |
78 | 108 | final String message = "This is a multipart post"; |
79 | 109 | final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
80 | 110 | 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); |
83 | 113 | final HttpEntity entity = builder.build(); |
84 | 114 | 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 | + |
87 | 121 | final Header[] headers = response.getAllHeaders(); |
| 122 | + assertThat(headers.length, equalTo(5)); |
88 | 123 |
|
89 | 124 | for (final Header thisHeader : headers) { |
90 | 125 | System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
91 | 126 | } |
| 127 | + |
92 | 128 | } |
93 | 129 |
|
94 | 130 | @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); |
97 | 134 | final File file = new File(imageFileName); |
98 | 135 | final String message = "This is a multipart post"; |
99 | 136 | final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
100 | 137 | 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); |
104 | 141 | final HttpEntity entity = builder.build(); |
105 | 142 | 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 | + |
108 | 149 | final Header[] headers = response.getAllHeaders(); |
| 150 | + assertThat(headers.length, equalTo(5)); |
109 | 151 |
|
110 | 152 | for (final Header thisHeader : headers) { |
111 | 153 | System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
112 | 154 | } |
| 155 | + |
| 156 | + inputStream.close(); |
| 157 | + |
113 | 158 | } |
114 | 159 |
|
115 | 160 | @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 | + |
119 | 163 | final String message = "This is a multipart post"; |
| 164 | + final byte[] bytes = "binary code".getBytes(); |
120 | 165 | final MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
121 | 166 | 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); |
125 | 169 | final HttpEntity entity = builder.build(); |
126 | 170 | 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()); |
128 | 176 |
|
129 | | - System.out.println(getContent(response)); |
130 | 177 | final Header[] headers = response.getAllHeaders(); |
| 178 | + assertThat(headers.length, equalTo(5)); |
131 | 179 |
|
132 | 180 | for (final Header thisHeader : headers) { |
133 | 181 | System.out.println(thisHeader.getName() + ":" + thisHeader.getValue()); |
134 | 182 | } |
135 | | - } |
136 | | - |
137 | | - // BUG |
138 | 183 |
|
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(); |
150 | 184 | } |
151 | 185 |
|
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())); |
154 | 189 | String body = ""; |
155 | 190 | String content = ""; |
156 | 191 |
|
157 | 192 | while ((body = rd.readLine()) != null) { |
158 | 193 | content += body + "\n"; |
159 | 194 | } |
| 195 | + |
160 | 196 | return content.trim(); |
| 197 | + |
161 | 198 | } |
162 | 199 |
|
163 | 200 | } |
0 commit comments