File tree Expand file tree Collapse file tree 3 files changed +28
-3
lines changed
src/bunit.web/Extensions/InputFile
tests/bunit.web.tests/Extensions/InputFile Expand file tree Collapse file tree 3 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,12 @@ public BUnitBrowserFile(
2727
2828 public Stream OpenReadStream ( long maxAllowedSize = 512000 , CancellationToken cancellationToken = default )
2929 {
30+ if ( Size > maxAllowedSize )
31+ {
32+ throw new IOException ( $ "Supplied file with size { Size } bytes exceeds the maximum of { maxAllowedSize } bytes.") ;
33+ }
34+
3035 return new MemoryStream ( Content ) ;
3136 }
3237}
33- #endif
38+ #endif
Original file line number Diff line number Diff line change 11#if NET5_0_OR_GREATER
2+ using System . Runtime . ExceptionServices ;
23using Microsoft . AspNetCore . Components . Forms ;
34
45namespace Bunit ;
@@ -36,6 +37,11 @@ public static void UploadFiles(
3637 {
3738 uploadTask . GetAwaiter ( ) . GetResult ( ) ;
3839 }
40+
41+ if ( uploadTask . Exception is { InnerException : not null } e )
42+ {
43+ ExceptionDispatchInfo . Capture ( e . InnerException ) . Throw ( ) ;
44+ }
3945 }
4046}
41- #endif
47+ #endif
Original file line number Diff line number Diff line change @@ -102,9 +102,23 @@ public void Test008()
102102
103103 act . ShouldNotThrow ( ) ;
104104 }
105+
106+ [ Fact ( DisplayName = "Uploading file exceeding the maximum file size will throw an exception" ) ]
107+ public void Test009 ( )
108+ {
109+ var cut = RenderComponent < InputFileComponent > ( ps => ps . Add ( p => p . MaxFileSize , 512 ) ) ;
110+ var file = InputFileContent . CreateFromText ( new string ( 'a' , 513 ) ) ;
111+
112+ Action act = ( ) => cut . FindComponent < InputFile > ( ) . UploadFiles ( file ) ;
113+
114+ act . ShouldThrow < IOException > ( ) ;
115+ }
105116
106117 private sealed class InputFileComponent : ComponentBase
107118 {
119+ [ Parameter ]
120+ public long MaxFileSize { get ; set ; } = 512000 ;
121+
108122 public string ? Filename { get ; private set ; }
109123 public string ? Content { get ; private set ; }
110124 public DateTimeOffset ? LastChanged { get ; private set ; }
@@ -127,7 +141,7 @@ private void OnChange(InputFileChangeEventArgs args)
127141 Filename = file . Name ;
128142 LastChanged = file . LastModified ;
129143 Size = file . Size ;
130- using var stream = new StreamReader ( file . OpenReadStream ( ) ) ;
144+ using var stream = new StreamReader ( file . OpenReadStream ( MaxFileSize ) ) ;
131145 Content = stream . ReadToEnd ( ) ;
132146 }
133147 }
You can’t perform that action at this time.
0 commit comments