22
33import static org .hamcrest .Matchers .equalTo ;
44import static org .junit .Assert .assertThat ;
5+ import static org .junit .Assert .assertTrue ;
56
67import java .io .BufferedReader ;
78import java .io .File ;
1011import java .io .InputStream ;
1112import java .io .InputStreamReader ;
1213
13- import org .apache .http .Header ;
1414import org .apache .http .HttpEntity ;
1515import org .apache .http .HttpStatus ;
1616import org .apache .http .client .ClientProtocolException ;
@@ -74,8 +74,7 @@ public final void after() throws IllegalStateException, IOException {
7474 }
7575
7676 @ Test
77- public final void whenUploadWithAddPart_thenNoExceptions () throws IOException {
78-
77+ public final void givenFileandMultipleTextParts_whenUploadWithAddPart_thenNoExceptions () throws IOException {
7978 final File file = new File (textFileName );
8079 final FileBody fileBody = new FileBody (file , ContentType .DEFAULT_BINARY );
8180 final StringBody stringBody1 = new StringBody ("This is message 1" , ContentType .MULTIPART_FORM_DATA );
@@ -89,21 +88,17 @@ public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
8988 post .setEntity (entity );
9089 response = client .execute (post );
9190 final int statusCode = response .getStatusLine ().getStatusCode ();
91+ final String responseString = getContent ();
92+ final String contentTypeInHeader = getContentTypeHeader ();
9293 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
93-
94- System .out .println (getContent ());
95-
96- final Header [] headers = response .getAllHeaders ();
97- assertThat (headers .length , equalTo (5 ));
98-
99- for (final Header thisHeader : headers ) {
100- System .out .println (thisHeader .getName () + ":" + thisHeader .getValue ());
101- }
94+ assertTrue (responseString .contains ("Content-Type: multipart/form-data;" ));
95+ assertTrue (contentTypeInHeader .contains ("Content-Type: multipart/form-data;" ));
96+ System .out .println (responseString );
97+ System .out .println ("POST Content Type: " + contentTypeInHeader );
10298 }
10399
104100 @ Test
105101 public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption () throws ClientProtocolException , IOException {
106-
107102 final File file = new File (textFileName );
108103 final String message = "This is a multipart post" ;
109104 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -114,22 +109,17 @@ public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() thr
114109 post .setEntity (entity );
115110 response = client .execute (post );
116111 final int statusCode = response .getStatusLine ().getStatusCode ();
112+ final String responseString = getContent ();
113+ final String contentTypeInHeader = getContentTypeHeader ();
117114 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
118-
119- System .out .println (getContent ());
120-
121- final Header [] headers = response .getAllHeaders ();
122- assertThat (headers .length , equalTo (5 ));
123-
124- for (final Header thisHeader : headers ) {
125- System .out .println (thisHeader .getName () + ":" + thisHeader .getValue ());
126- }
127-
115+ assertTrue (responseString .contains ("Content-Type: multipart/form-data;" ));
116+ assertTrue (contentTypeInHeader .contains ("Content-Type: multipart/form-data;" ));
117+ System .out .println (responseString );
118+ System .out .println ("POST Content Type: " + contentTypeInHeader );
128119 }
129120
130121 @ Test
131122 public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody_ThenNoException () throws ClientProtocolException , IOException {
132-
133123 final InputStream inputStream = new FileInputStream (zipFileName );
134124 final File file = new File (imageFileName );
135125 final String message = "This is a multipart post" ;
@@ -142,24 +132,18 @@ public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody
142132 post .setEntity (entity );
143133 response = client .execute (post );
144134 final int statusCode = response .getStatusLine ().getStatusCode ();
135+ final String responseString = getContent ();
136+ final String contentTypeInHeader = getContentTypeHeader ();
145137 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
146-
147- System .out .println (getContent ());
148-
149- final Header [] headers = response .getAllHeaders ();
150- assertThat (headers .length , equalTo (5 ));
151-
152- for (final Header thisHeader : headers ) {
153- System .out .println (thisHeader .getName () + ":" + thisHeader .getValue ());
154- }
155-
138+ assertTrue (responseString .contains ("Content-Type: multipart/form-data;" ));
139+ assertTrue (contentTypeInHeader .contains ("Content-Type: multipart/form-data;" ));
140+ System .out .println (responseString );
141+ System .out .println ("POST Content Type: " + contentTypeInHeader );
156142 inputStream .close ();
157-
158143 }
159144
160145 @ Test
161146 public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoException () throws ClientProtocolException , IOException {
162-
163147 final String message = "This is a multipart post" ;
164148 final byte [] bytes = "binary code" .getBytes ();
165149 final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -170,31 +154,28 @@ public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoEx
170154 post .setEntity (entity );
171155 response = client .execute (post );
172156 final int statusCode = response .getStatusLine ().getStatusCode ();
157+ final String responseString = getContent ();
158+ final String contentTypeInHeader = getContentTypeHeader ();
173159 assertThat (statusCode , equalTo (HttpStatus .SC_OK ));
174-
175- System .out .println (getContent ());
176-
177- final Header [] headers = response .getAllHeaders ();
178- assertThat (headers .length , equalTo (5 ));
179-
180- for (final Header thisHeader : headers ) {
181- System .out .println (thisHeader .getName () + ":" + thisHeader .getValue ());
182- }
183-
160+ assertTrue (responseString .contains ("Content-Type: multipart/form-data;" ));
161+ assertTrue (contentTypeInHeader .contains ("Content-Type: multipart/form-data;" ));
162+ System .out .println (responseString );
163+ System .out .println ("POST Content Type: " + contentTypeInHeader );
184164 }
185165
186166 public String getContent () throws IOException {
187-
188167 rd = new BufferedReader (new InputStreamReader (response .getEntity ().getContent ()));
189168 String body = "" ;
190169 String content = "" ;
191-
192170 while ((body = rd .readLine ()) != null ) {
193171 content += body + "\n " ;
194172 }
195-
173+ rd . close ();
196174 return content .trim ();
175+ }
197176
177+ public String getContentTypeHeader () throws IOException {
178+ return post .getEntity ().getContentType ().toString ();
198179 }
199180
200181}
0 commit comments