23
23
import java .io .OutputStream ;
24
24
import java .io .PrintWriter ;
25
25
import java .net .URI ;
26
+ import java .net .URL ;
26
27
import java .nio .ByteBuffer ;
27
28
import java .nio .channels .SeekableByteChannel ;
28
29
import java .nio .file .FileStore ;
@@ -185,6 +186,7 @@ public void startServer() throws Exception
185
186
context .setBaseResource (new PathResource (staticBase ));
186
187
187
188
context .addServlet (PostServlet .class , "/post" );
189
+ context .addServlet (ChunkedServlet .class , "/chunked/*" );
188
190
189
191
String location = multipartTempDir .toString ();
190
192
long maxFileSize = Long .MAX_VALUE ;
@@ -224,7 +226,7 @@ public void stopClient() throws Exception
224
226
225
227
@ ParameterizedTest
226
228
@ MethodSource ("staticFiles" )
227
- public void testDownload (String filename , long expectedSize ) throws Exception
229
+ public void testDownloadStatic (String filename , long expectedSize ) throws Exception
228
230
{
229
231
URI destUri = server .getURI ().resolve ("/" + filename );
230
232
InputStreamResponseListener responseListener = new InputStreamResponseListener ();
@@ -251,7 +253,33 @@ public void testDownload(String filename, long expectedSize) throws Exception
251
253
252
254
@ ParameterizedTest
253
255
@ MethodSource ("staticFiles" )
254
- public void testHead (String filename , long expectedSize ) throws Exception
256
+ public void testDownloadChunked (String filename , long expectedSize ) throws Exception
257
+ {
258
+ URI destUri = server .getURI ().resolve ("/chunked/" + filename );
259
+ InputStreamResponseListener responseListener = new InputStreamResponseListener ();
260
+
261
+ Request request = client .newRequest (destUri )
262
+ .method (HttpMethod .GET );
263
+ request .send (responseListener );
264
+ Response response = responseListener .get (5 , TimeUnit .SECONDS );
265
+
266
+ assertThat ("HTTP Response Code" , response .getStatus (), is (200 ));
267
+ // dumpResponse(response);
268
+
269
+ String transferEncoding = response .getHeaders ().get (HttpHeader .TRANSFER_ENCODING );
270
+ assertThat ("Http Response Header: \" Transfer-Encoding\" " , transferEncoding , is ("chunked" ));
271
+
272
+ try (ByteCountingOutputStream out = new ByteCountingOutputStream ();
273
+ InputStream in = responseListener .getInputStream ())
274
+ {
275
+ IO .copy (in , out );
276
+ assertThat ("Downloaded Files Size: " + filename , out .getCount (), is (expectedSize ));
277
+ }
278
+ }
279
+
280
+ @ ParameterizedTest
281
+ @ MethodSource ("staticFiles" )
282
+ public void testHeadStatic (String filename , long expectedSize ) throws Exception
255
283
{
256
284
URI destUri = server .getURI ().resolve ("/" + filename );
257
285
InputStreamResponseListener responseListener = new InputStreamResponseListener ();
@@ -274,6 +302,30 @@ public void testHead(String filename, long expectedSize) throws Exception
274
302
assertThat ("Http Response Header: \" Content-Length: " + contentLength + "\" " , contentLengthLong , is (expectedSize ));
275
303
}
276
304
305
+ @ ParameterizedTest
306
+ @ MethodSource ("staticFiles" )
307
+ public void testHeadChunked (String filename , long expectedSize ) throws Exception
308
+ {
309
+ URI destUri = server .getURI ().resolve ("/chunked/" + filename );
310
+ InputStreamResponseListener responseListener = new InputStreamResponseListener ();
311
+
312
+ Request request = client .newRequest (destUri )
313
+ .method (HttpMethod .HEAD );
314
+ request .send (responseListener );
315
+ Response response = responseListener .get (5 , TimeUnit .SECONDS );
316
+
317
+ try (InputStream in = responseListener .getInputStream ())
318
+ {
319
+ assertThat (in .read (), is (-1 ));
320
+ }
321
+
322
+ assertThat ("HTTP Response Code" , response .getStatus (), is (200 ));
323
+ // dumpResponse(response);
324
+
325
+ String transferEncoding = response .getHeaders ().get (HttpHeader .TRANSFER_ENCODING );
326
+ assertThat ("Http Response Header: \" Transfer-Encoding\" " , transferEncoding , is ("chunked" ));
327
+ }
328
+
277
329
@ ParameterizedTest
278
330
@ MethodSource ("staticFiles" )
279
331
public void testUpload (String filename , long expectedSize ) throws Exception
@@ -359,6 +411,22 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
359
411
}
360
412
}
361
413
414
+ public static class ChunkedServlet extends HttpServlet
415
+ {
416
+ @ Override
417
+ protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws IOException
418
+ {
419
+ URL resource = req .getServletContext ().getResource (req .getPathInfo ());
420
+ OutputStream output = resp .getOutputStream ();
421
+ try (InputStream input = resource .openStream ())
422
+ {
423
+ resp .setContentType ("application/octet-stream" );
424
+ resp .flushBuffer ();
425
+ IO .copy (input , output );
426
+ }
427
+ }
428
+ }
429
+
362
430
public static class MultipartServlet extends HttpServlet
363
431
{
364
432
@ Override
0 commit comments