@@ -2305,114 +2305,70 @@ private void testSendFileWithFailure(BiFunction<HttpServerResponse, File, Future
2305
2305
2306
2306
@ Test
2307
2307
public void testSendFileWithFileChannel () throws Exception {
2308
- int expected = 16 * 1024 * 1024 ;
2309
- File file = TestUtils .tmpFile (".dat" , expected );
2310
- FileChannel channel = FileChannel .open (file .toPath ());
2311
- server .requestHandler (
2312
- req -> {
2313
- req .response ().sendFile (channel ).onFailure (
2314
- t -> {
2315
- if (HttpTest .this instanceof Http2Test ) {
2316
- req .response ().end ();
2317
- }
2318
- }
2319
- );
2320
- });
2321
- startServer (testAddress );
2322
- Object [] res = {0 , "" };
2323
- Object [] r = client .request (requestOptions )
2324
- .compose (req -> req .send ()
2325
- .compose (resp -> {
2326
- resp .handler (buff -> {
2327
- Integer length = (Integer ) res [0 ];
2328
- length += buff .length ();
2329
- res [0 ] = length ;
2330
- });
2331
- res [1 ] = resp .getHeader ("Content-Type" );
2332
- resp .exceptionHandler (this ::fail );
2333
- return resp .end ();
2334
- }))
2335
- .map (v -> res ).await ();
2336
- if (this instanceof Http1xTest ){
2337
- assertEquals ((int ) r [0 ], file .length ());
2338
- assertEquals ("application/octet-stream" , r [1 ]);
2339
- }
2308
+ int fileLength = 16 * 1024 * 1024 ;
2309
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response .sendFile (file .getChannel ());
2310
+ testSendFileWithFileChannel (fileLength , sender , "application/octet-stream" , fileLength );
2340
2311
}
2341
2312
2342
2313
@ Test
2343
2314
public void testSendFileWithFileChannelAndExtension () throws Exception {
2344
- int expected = 16 * 1024 * 1024 ;
2345
- File file = TestUtils .tmpFile (".dat" , expected );
2346
- FileChannel channel = FileChannel .open (file .toPath ());
2347
- server .requestHandler (
2348
- req -> {
2349
- req .response ()
2350
- .putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" )
2351
- .sendFile (channel ).onFailure (
2352
- t -> {
2353
- if (HttpTest .this instanceof Http2Test ) {
2354
- req .response ().end ();
2355
- }
2356
- }
2357
- );
2358
- });
2359
- startServer (testAddress );
2360
- Object [] res = {0 , "" };
2361
- Object [] r = client .request (requestOptions )
2362
- .compose (req -> req .send ()
2363
- .compose (resp -> {
2364
- resp .handler (buff -> {
2365
- Integer length = (Integer ) res [0 ];
2366
- length += buff .length ();
2367
- res [0 ] = length ;
2368
- });
2369
- res [1 ] = resp .getHeader ("Content-Type" );
2370
- resp .exceptionHandler (this ::fail );
2371
- return resp .end ();
2372
- }))
2373
- .map (v -> res ).await ();
2374
- if (this instanceof Http1xTest ){
2375
- assertEquals ((int )r [0 ], file .length ());
2376
- assertEquals (r [1 ], "video/mp4" );
2377
- }
2315
+ int fileLength = 16 * 1024 * 1024 ;
2316
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response
2317
+ .putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" )
2318
+ .sendFile (file .getChannel ());
2319
+ testSendFileWithFileChannel (fileLength , sender , "video/mp4" , fileLength );
2378
2320
}
2379
2321
2380
2322
@ Test
2381
2323
public void testSendFileWithFileChannelRange () throws Exception {
2382
2324
int fileLength = 16 * 1024 * 1024 ;
2383
- File file = TestUtils .tmpFile (".dat" , fileLength );
2384
- FileChannel channel = FileChannel .open (file .toPath ());
2385
2325
int offset = 1024 * 4 ;
2386
2326
int expectedRange = fileLength - offset ;
2387
- server .requestHandler (
2388
- req -> {
2389
- req .response ().putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" );
2390
- req .response ().sendFile (channel , offset , expectedRange ).onFailure (
2391
- t -> {
2392
- if (HttpTest .this instanceof Http2Test ) {
2393
- req .response ().end ();
2394
- }
2395
- }
2396
- );
2397
- });
2398
- startServer (testAddress );
2399
- Object [] res = {0 , "" };
2400
- Object [] r = client .request (requestOptions )
2401
- .compose (req -> req .send ()
2402
- .compose (resp -> {
2403
- resp .handler (buff -> {
2404
- Integer length = (Integer ) res [0 ];
2405
- length += buff .length ();
2406
- res [0 ] = length ;
2407
- });
2408
- res [1 ] = resp .getHeader ("Content-Type" );
2409
- resp .exceptionHandler (this ::fail );
2410
- return resp .end ();
2411
- }))
2412
- .map (v -> res ).await ();
2413
- if (this instanceof Http1xTest ) {
2414
- assertEquals (expectedRange , (int ) r [0 ]);
2415
- assertEquals ("video/mp4" , r [1 ]);
2327
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response
2328
+ .putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" )
2329
+ .sendFile (file .getChannel (), offset , expectedRange );
2330
+ testSendFileWithFileChannel (fileLength , sender , "video/mp4" , expectedRange );
2331
+ }
2332
+
2333
+ @ Test
2334
+ public void testSendFileWithRandomAccessFile () throws Exception {
2335
+ int fileLength = 16 * 1024 * 1024 ;
2336
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response .sendFile (file );
2337
+ testSendFileWithFileChannel (fileLength , sender , "application/octet-stream" , fileLength );
2338
+ }
2339
+
2340
+ @ Test
2341
+ public void testSendFileWithRandomAccessFileAndExtension () throws Exception {
2342
+ int fileLength = 16 * 1024 * 1024 ;
2343
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response
2344
+ .putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" )
2345
+ .sendFile (file );
2346
+ testSendFileWithFileChannel (fileLength , sender , "video/mp4" , fileLength );
2347
+ }
2348
+
2349
+ @ Test
2350
+ public void testSendFileWithRandomAccessFileRange () throws Exception {
2351
+ int fileLength = 16 * 1024 * 1024 ;
2352
+ int offset = 1024 * 4 ;
2353
+ int expectedRange = fileLength - offset ;
2354
+ BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender = (file , response ) -> response
2355
+ .putHeader (HttpHeaders .CONTENT_TYPE , "video/mp4" )
2356
+ .sendFile (file , offset , expectedRange );
2357
+ testSendFileWithFileChannel (fileLength , sender , "video/mp4" , expectedRange );
2358
+ }
2359
+
2360
+ private void testSendFileWithFileChannel (int flen , BiFunction <RandomAccessFile , HttpServerResponse , Future <?>> sender , String expectedContentType , long expectedLength ) throws Exception {
2361
+ Assume .assumeTrue (this instanceof Http1xTest );
2362
+ File file = TestUtils .tmpFile (".dat" , flen );
2363
+ try (RandomAccessFile raf = new RandomAccessFile (file , "r" )) {
2364
+ server .requestHandler (req -> sender .apply (raf , req .response ()).onComplete (onSuccess (v -> testComplete ())));
2365
+ startServer (testAddress );
2366
+ Buffer body = client .request (requestOptions )
2367
+ .compose (req -> req .send ()
2368
+ .expecting (HttpResponseExpectation .contentType (expectedContentType ))
2369
+ .compose (HttpClientResponse ::body )).await ();
2370
+ assertEquals (body .length (), expectedLength );
2371
+ await ();
2416
2372
}
2417
2373
}
2418
2374
0 commit comments