2
2
3
3
import static org .hamcrest .Matchers .equalTo ;
4
4
import static org .junit .Assert .assertThat ;
5
+ import static org .junit .Assert .assertTrue ;
5
6
6
7
import java .io .BufferedReader ;
7
8
import java .io .File ;
10
11
import java .io .InputStream ;
11
12
import java .io .InputStreamReader ;
12
13
13
- import org .apache .http .Header ;
14
14
import org .apache .http .HttpEntity ;
15
15
import org .apache .http .HttpStatus ;
16
16
import org .apache .http .client .ClientProtocolException ;
@@ -74,8 +74,7 @@ public final void after() throws IllegalStateException, IOException {
74
74
}
75
75
76
76
@ Test
77
- public final void whenUploadWithAddPart_thenNoExceptions () throws IOException {
78
-
77
+ public final void givenFileandMultipleTextParts_whenUploadWithAddPart_thenNoExceptions () throws IOException {
79
78
final File file = new File (textFileName );
80
79
final FileBody fileBody = new FileBody (file , ContentType .DEFAULT_BINARY );
81
80
final StringBody stringBody1 = new StringBody ("This is message 1" , ContentType .MULTIPART_FORM_DATA );
@@ -89,21 +88,17 @@ public final void whenUploadWithAddPart_thenNoExceptions() throws IOException {
89
88
post .setEntity (entity );
90
89
response = client .execute (post );
91
90
final int statusCode = response .getStatusLine ().getStatusCode ();
91
+ final String responseString = getContent ();
92
+ final String contentTypeInHeader = getContentTypeHeader ();
92
93
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 );
102
98
}
103
99
104
100
@ Test
105
101
public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption () throws ClientProtocolException , IOException {
106
-
107
102
final File file = new File (textFileName );
108
103
final String message = "This is a multipart post" ;
109
104
final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -114,22 +109,17 @@ public final void whenUploadWithAddBinaryBodyandAddTextBody_ThenNoExeption() thr
114
109
post .setEntity (entity );
115
110
response = client .execute (post );
116
111
final int statusCode = response .getStatusLine ().getStatusCode ();
112
+ final String responseString = getContent ();
113
+ final String contentTypeInHeader = getContentTypeHeader ();
117
114
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 );
128
119
}
129
120
130
121
@ Test
131
122
public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody_ThenNoException () throws ClientProtocolException , IOException {
132
-
133
123
final InputStream inputStream = new FileInputStream (zipFileName );
134
124
final File file = new File (imageFileName );
135
125
final String message = "This is a multipart post" ;
@@ -142,24 +132,18 @@ public final void whenUploadWithAddBinaryBody_withInputStreamAndFile_andTextBody
142
132
post .setEntity (entity );
143
133
response = client .execute (post );
144
134
final int statusCode = response .getStatusLine ().getStatusCode ();
135
+ final String responseString = getContent ();
136
+ final String contentTypeInHeader = getContentTypeHeader ();
145
137
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 );
156
142
inputStream .close ();
157
-
158
143
}
159
144
160
145
@ Test
161
146
public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoException () throws ClientProtocolException , IOException {
162
-
163
147
final String message = "This is a multipart post" ;
164
148
final byte [] bytes = "binary code" .getBytes ();
165
149
final MultipartEntityBuilder builder = MultipartEntityBuilder .create ();
@@ -170,31 +154,27 @@ public final void whenUploadWithAddBinaryBody_withCharArray_andTextBody_ThenNoEx
170
154
post .setEntity (entity );
171
155
response = client .execute (post );
172
156
final int statusCode = response .getStatusLine ().getStatusCode ();
157
+ final String responseString = getContent ();
158
+ final String contentTypeInHeader = getContentTypeHeader ();
173
159
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 );
184
164
}
185
165
186
166
public String getContent () throws IOException {
187
-
188
167
rd = new BufferedReader (new InputStreamReader (response .getEntity ().getContent ()));
189
168
String body = "" ;
190
169
String content = "" ;
191
-
192
170
while ((body = rd .readLine ()) != null ) {
193
171
content += body + "\n " ;
194
172
}
195
-
196
173
return content .trim ();
174
+ }
197
175
176
+ public String getContentTypeHeader () throws IOException {
177
+ return post .getEntity ().getContentType ().toString ();
198
178
}
199
179
200
180
}
0 commit comments