4242import com .vaadin .flow .server .VaadinSession ;
4343
4444public class InputStreamDownloadHandlerTest {
45+ private static final int SIMULATED_DOWNLOAD_SIZE = 165000 ;
46+
4547 private VaadinRequest request ;
4648 private VaadinResponse response ;
4749 private VaadinSession session ;
@@ -93,7 +95,8 @@ public void transferProgressListener_addListener_listenersInvoked()
9395 }, new TransferProgressListener () {
9496 @ Override
9597 public void onStart (TransferContext context ) {
96- Assert .assertEquals (-1 , context .contentLength ());
98+ Assert .assertEquals (SIMULATED_DOWNLOAD_SIZE ,
99+ context .contentLength ());
97100 Assert .assertEquals ("download" , context .fileName ());
98101 invocations .add ("onStart" );
99102 }
@@ -102,16 +105,17 @@ public void onStart(TransferContext context) {
102105 public void onProgress (TransferContext context ,
103106 long transferredBytes , long totalBytes ) {
104107 transferredBytesRecords .add (transferredBytes );
105- Assert .assertEquals (- 1 , totalBytes );
108+ Assert .assertEquals (SIMULATED_DOWNLOAD_SIZE , totalBytes );
106109 Assert .assertEquals ("download" , context .fileName ());
107110 invocations .add ("onProgress" );
108111 }
109112
110113 @ Override
111114 public void onComplete (TransferContext context ,
112115 long transferredBytes ) {
113- Assert .assertEquals (-1 , context .contentLength ());
114- Assert .assertEquals (165000 , transferredBytes );
116+ Assert .assertEquals (SIMULATED_DOWNLOAD_SIZE ,
117+ context .contentLength ());
118+ Assert .assertEquals (SIMULATED_DOWNLOAD_SIZE , transferredBytes );
115119 Assert .assertEquals ("download" , context .fileName ());
116120 invocations .add ("onComplete" );
117121 }
@@ -150,7 +154,6 @@ public void transferProgressListener_addListener_errorOccurred_errorListenerInvo
150154 AtomicReference <Boolean > whenCompleteResult = new AtomicReference <>();
151155 InvocationTrackingTransferProgressListener transferListener = new InvocationTrackingTransferProgressListener ();
152156 DownloadHandler handler = DownloadHandler .fromInputStream (req -> {
153- // Simulate a download of 165000 bytes
154157 byte [] data = getBytes ();
155158 ByteArrayInputStream inputStream = new ByteArrayInputStream (data );
156159 return new DownloadResponse (inputStream , "download" ,
@@ -403,9 +406,29 @@ public void handleSetToInline_contentTypeIsInline() throws IOException {
403406 Mockito .verify (response ).setHeader ("Content-Disposition" , "inline" );
404407 }
405408
409+ @ Test
410+ public void contentLengthProvided_contentLengthHeaderSet ()
411+ throws IOException {
412+ long expectedContentLength = 12345L ;
413+ InputStream stream = Mockito .mock (InputStream .class );
414+ Mockito .when (
415+ stream .read (Mockito .any (), Mockito .anyInt (), Mockito .anyInt ()))
416+ .thenReturn (-1 );
417+
418+ InputStreamDownloadHandler handler = new InputStreamDownloadHandler (
419+ event -> new DownloadResponse (stream , "report.pdf" ,
420+ "application/pdf" , expectedContentLength ));
421+
422+ DownloadEvent event = new DownloadEvent (request , response , session ,
423+ new Element ("div" ));
424+
425+ handler .handleDownloadRequest (event );
426+
427+ Mockito .verify (response ).setContentLengthLong (expectedContentLength );
428+ }
429+
406430 private static byte [] getBytes () {
407- // Simulate a download of 165000 bytes
408- byte [] data = new byte [165000 ];
431+ byte [] data = new byte [SIMULATED_DOWNLOAD_SIZE ];
409432 for (int i = 0 ; i < data .length ; i ++) {
410433 data [i ] = (byte ) (i % 256 );
411434 }
0 commit comments