diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99cfcca00..403e73988 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad
## [Unreleased]
+### Fixed
+
+- `UploadFile` doesn't throw an exception when the file size exceeds the maximum allowed size. Reported by [@MorneZaayman](https://github.com/MorneZaayman) in [#1503](https://github.com/bUnit-dev/bUnit/issues/1503). Fixed by [@linkdotnet](https://github.com/linkdotnet).
+
## [1.29.5] - 2024-07-05
### Fixed
diff --git a/Directory.Build.props b/Directory.Build.props
index 765ddd563..d9a96bfbf 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -52,7 +52,7 @@
-
+
-
+
diff --git a/docs/samples/tests/Directory.Build.props b/docs/samples/tests/Directory.Build.props
index 8422a0457..e5b3bbe6b 100644
--- a/docs/samples/tests/Directory.Build.props
+++ b/docs/samples/tests/Directory.Build.props
@@ -13,7 +13,7 @@
-
+
diff --git a/docs/samples/tests/razor/bunit.docs.razor.samples.csproj b/docs/samples/tests/razor/bunit.docs.razor.samples.csproj
index 601c8a4c3..9dff4b342 100644
--- a/docs/samples/tests/razor/bunit.docs.razor.samples.csproj
+++ b/docs/samples/tests/razor/bunit.docs.razor.samples.csproj
@@ -9,7 +9,7 @@
-
+
all
diff --git a/docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj b/docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj
index be96270c2..96ed4edf4 100644
--- a/docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj
+++ b/docs/samples/tests/xunit/bunit.docs.xunit.samples.csproj
@@ -11,7 +11,7 @@
-
+
all
diff --git a/src/bunit.template/template/Company.BlazorTests1.csproj b/src/bunit.template/template/Company.BlazorTests1.csproj
index 36d78fdd7..4a443a344 100644
--- a/src/bunit.template/template/Company.BlazorTests1.csproj
+++ b/src/bunit.template/template/Company.BlazorTests1.csproj
@@ -25,7 +25,7 @@
-
+
all
runtime; build; native; contentfiles; analyzers
diff --git a/src/bunit.web/Extensions/InputFile/BUnitBrowserFile.cs b/src/bunit.web/Extensions/InputFile/BUnitBrowserFile.cs
index 76e5dc8ae..a50a89176 100644
--- a/src/bunit.web/Extensions/InputFile/BUnitBrowserFile.cs
+++ b/src/bunit.web/Extensions/InputFile/BUnitBrowserFile.cs
@@ -27,7 +27,12 @@ public BUnitBrowserFile(
public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken cancellationToken = default)
{
+ if (Size > maxAllowedSize)
+ {
+ throw new IOException($"Supplied file with size {Size} bytes exceeds the maximum of {maxAllowedSize} bytes.");
+ }
+
return new MemoryStream(Content);
}
}
-#endif
\ No newline at end of file
+#endif
diff --git a/src/bunit.web/Extensions/InputFile/InputFileExtensions.cs b/src/bunit.web/Extensions/InputFile/InputFileExtensions.cs
index 3eb0de24c..39c6d7395 100644
--- a/src/bunit.web/Extensions/InputFile/InputFileExtensions.cs
+++ b/src/bunit.web/Extensions/InputFile/InputFileExtensions.cs
@@ -1,4 +1,5 @@
#if NET5_0_OR_GREATER
+using System.Runtime.ExceptionServices;
using Microsoft.AspNetCore.Components.Forms;
namespace Bunit;
@@ -36,6 +37,11 @@ public static void UploadFiles(
{
uploadTask.GetAwaiter().GetResult();
}
+
+ if (uploadTask.Exception is { InnerException: not null } e)
+ {
+ ExceptionDispatchInfo.Capture(e.InnerException).Throw();
+ }
}
}
-#endif
\ No newline at end of file
+#endif
diff --git a/src/bunit.web/bunit.web.csproj b/src/bunit.web/bunit.web.csproj
index 9b453fc70..86401722d 100644
--- a/src/bunit.web/bunit.web.csproj
+++ b/src/bunit.web/bunit.web.csproj
@@ -31,6 +31,9 @@
+
+
+
@@ -41,6 +44,9 @@
+
+
+
@@ -52,6 +58,9 @@
+
+
+
@@ -63,6 +72,9 @@
+
+
+
@@ -74,6 +86,9 @@
+
+
+
diff --git a/tests/Directory.Build.props b/tests/Directory.Build.props
index e30b4b0b1..5c1e49b76 100644
--- a/tests/Directory.Build.props
+++ b/tests/Directory.Build.props
@@ -13,6 +13,14 @@
true
true
+
+
+
+ NU1903
+
diff --git a/tests/bunit.core.tests/bunit.core.tests.csproj b/tests/bunit.core.tests/bunit.core.tests.csproj
index 3fd9831b1..62e64bfdd 100644
--- a/tests/bunit.core.tests/bunit.core.tests.csproj
+++ b/tests/bunit.core.tests/bunit.core.tests.csproj
@@ -13,7 +13,7 @@
-
+
all
diff --git a/tests/bunit.generators.tests/bunit.generators.tests.csproj b/tests/bunit.generators.tests/bunit.generators.tests.csproj
index e0cdb9754..236bac0a1 100644
--- a/tests/bunit.generators.tests/bunit.generators.tests.csproj
+++ b/tests/bunit.generators.tests/bunit.generators.tests.csproj
@@ -14,7 +14,6 @@
-
@@ -25,8 +24,8 @@
-
-
+
+
all
runtime; build; native; contentfiles; analyzers
diff --git a/tests/bunit.testassets/SampleComponents/PersistentComponentStateSample.razor.cs b/tests/bunit.testassets/SampleComponents/PersistentComponentStateSample.razor.cs
index ae422117c..6f4532ff7 100644
--- a/tests/bunit.testassets/SampleComponents/PersistentComponentStateSample.razor.cs
+++ b/tests/bunit.testassets/SampleComponents/PersistentComponentStateSample.razor.cs
@@ -32,7 +32,7 @@ private WeatherForecast[] CreateForecasts()
{
return new WeatherForecast[]
{
- new WeatherForecast{ Temperature = 42 },
+ new WeatherForecast{ Temperature = 42 },
};
}
#endif
diff --git a/tests/bunit.testassets/_Imports.razor b/tests/bunit.testassets/_Imports.razor
index 53e2952cd..80f583e3a 100644
--- a/tests/bunit.testassets/_Imports.razor
+++ b/tests/bunit.testassets/_Imports.razor
@@ -1,4 +1,3 @@
-@using System.Net.Http
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
diff --git a/tests/bunit.testassets/bunit.testassets.csproj b/tests/bunit.testassets/bunit.testassets.csproj
index 50e8110cc..d9746eafc 100644
--- a/tests/bunit.testassets/bunit.testassets.csproj
+++ b/tests/bunit.testassets/bunit.testassets.csproj
@@ -16,16 +16,16 @@
-
+
-
+
-
+
diff --git a/tests/bunit.web.query.tests/bunit.web.query.tests.csproj b/tests/bunit.web.query.tests/bunit.web.query.tests.csproj
index de2310ea6..f37f4ff1d 100644
--- a/tests/bunit.web.query.tests/bunit.web.query.tests.csproj
+++ b/tests/bunit.web.query.tests/bunit.web.query.tests.csproj
@@ -14,7 +14,7 @@
-
+
all
diff --git a/tests/bunit.web.tests/Extensions/InputFile/InputFileTests.cs b/tests/bunit.web.tests/Extensions/InputFile/InputFileTests.cs
index 465813e21..e0e63ad83 100644
--- a/tests/bunit.web.tests/Extensions/InputFile/InputFileTests.cs
+++ b/tests/bunit.web.tests/Extensions/InputFile/InputFileTests.cs
@@ -102,9 +102,23 @@ public void Test008()
act.ShouldNotThrow();
}
+
+ [Fact(DisplayName = "Uploading file exceeding the maximum file size will throw an exception")]
+ public void Test009()
+ {
+ var cut = RenderComponent(ps => ps.Add(p => p.MaxFileSize, 512));
+ var file = InputFileContent.CreateFromText(new string('a', 513));
+
+ Action act = () => cut.FindComponent().UploadFiles(file);
+
+ act.ShouldThrow();
+ }
private sealed class InputFileComponent : ComponentBase
{
+ [Parameter]
+ public long MaxFileSize { get; set; } = 512000;
+
public string? Filename { get; private set; }
public string? Content { get; private set; }
public DateTimeOffset? LastChanged { get; private set; }
@@ -127,7 +141,7 @@ private void OnChange(InputFileChangeEventArgs args)
Filename = file.Name;
LastChanged = file.LastModified;
Size = file.Size;
- using var stream = new StreamReader(file.OpenReadStream());
+ using var stream = new StreamReader(file.OpenReadStream(MaxFileSize));
Content = stream.ReadToEnd();
}
}
diff --git a/tests/bunit.web.tests/bunit.web.tests.csproj b/tests/bunit.web.tests/bunit.web.tests.csproj
index 386eb90b2..09885c8f4 100644
--- a/tests/bunit.web.tests/bunit.web.tests.csproj
+++ b/tests/bunit.web.tests/bunit.web.tests.csproj
@@ -13,7 +13,7 @@
-
+
all