File tree Expand file tree Collapse file tree 4 files changed +53
-3
lines changed
jooby/src/main/java/io/jooby
main/java/io/jooby/internal/utow
test/java/io/jooby/internal/utow Expand file tree Collapse file tree 4 files changed +53
-3
lines changed Original file line number Diff line number Diff line change @@ -66,9 +66,9 @@ public interface FileUpload {
6666 @ Nonnull Path path ();
6767
6868 /**
69- * File size.
69+ * File size or <code>-1</code> when unknown .
7070 *
71- * @return File size.
71+ * @return File size or <code>-1</code> when unknown .
7272 */
7373 long getFileSize ();
7474
Original file line number Diff line number Diff line change 4444 <classifier >runtime</classifier >
4545 <scope >test</scope >
4646 </dependency >
47+
48+ <dependency >
49+ <groupId >org.mockito</groupId >
50+ <artifactId >mockito-core</artifactId >
51+ <scope >test</scope >
52+ </dependency >
4753 </dependencies >
4854</project >
Original file line number Diff line number Diff line change @@ -59,7 +59,11 @@ public UtowFileUpload(FormData.FormValue upload) {
5959 }
6060
6161 @ Override public long getFileSize () {
62- return Long .parseLong (upload .getHeaders ().getFirst (Headers .CONTENT_LENGTH ));
62+ try {
63+ return upload .getFileItem ().getFileSize ();
64+ } catch (IOException x ) {
65+ return -1 ;
66+ }
6367 }
6468
6569 @ Override public void destroy () {
Original file line number Diff line number Diff line change 1+ package io .jooby .internal .utow ;
2+
3+ import io .undertow .server .handlers .form .FormData ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import java .io .IOException ;
7+
8+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9+ import static org .mockito .Mockito .doThrow ;
10+ import static org .mockito .Mockito .mock ;
11+ import static org .mockito .Mockito .when ;
12+
13+ public class UtowFileUploadTest {
14+
15+ @ Test
16+ public void shouldGetFileSize () throws IOException {
17+ long fileSize = 678 ;
18+
19+ FormData .FileItem fileItem = mock (FormData .FileItem .class );
20+ when (fileItem .getFileSize ()).thenReturn (fileSize );
21+
22+ FormData .FormValue upload = mock (FormData .FormValue .class );
23+ when (upload .getFileItem ()).thenReturn (fileItem );
24+
25+ assertEquals (fileSize , new UtowFileUpload (upload ).getFileSize ());
26+ }
27+
28+ @ Test
29+ public void shouldGetUnknownFileSize () throws IOException {
30+ long fileSize = -1 ;
31+
32+ FormData .FileItem fileItem = mock (FormData .FileItem .class );
33+ doThrow (new IOException ()).when (fileItem ).getFileSize ();
34+
35+ FormData .FormValue upload = mock (FormData .FormValue .class );
36+ when (upload .getFileItem ()).thenReturn (fileItem );
37+
38+ assertEquals (fileSize , new UtowFileUpload (upload ).getFileSize ());
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments