Skip to content

Commit d4464a2

Browse files
Update E2E tests
1 parent 9357d8f commit d4464a2

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

src/Components/Web/src/Forms/InputFile/BrowserFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Stream OpenReadStream(long maxAllowedSize = 512000, CancellationToken can
4141
{
4242
if (Size > maxAllowedSize)
4343
{
44-
throw new IOException($"File size '{Size} is large than the maximum allowed size '{maxAllowedSize}'.");
44+
throw new IOException($"Supplied file with size {Size} bytes exceeds the maximum of {maxAllowedSize} bytes.");
4545
}
4646

4747
return Owner.OpenReadStream(this, cancellationToken);

src/Components/test/E2ETest/ServerExecutionTests/TestSubclasses.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,12 @@ public ServerKeyTest(BrowserFixture browserFixture, ToggleExecutionModeServerFix
7575
{
7676
}
7777
}
78+
79+
public class ServerInputFileTest : InputFileTest
80+
{
81+
public ServerInputFileTest(BrowserFixture browserFixture, ToggleExecutionModeServerFixture<Program> serverFixture, ITestOutputHelper output)
82+
: base(browserFixture, serverFixture.WithServerExecution(), output)
83+
{
84+
}
85+
}
7886
}

src/Components/test/E2ETest/Tests/InputFileTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using Xunit;
1717
using Xunit.Abstractions;
1818

19-
namespace Microsoft.AspNetCore.Components.E2ETests.Tests
19+
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
2020
{
2121
public class InputFileTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>, IDisposable
2222
{
@@ -157,7 +157,7 @@ public void ThrowsWhenTooManyFilesAreSelected()
157157

158158
// Validate that the proper exception is thrown
159159
var exceptionMessage = Browser.FindElement(By.Id("exception-message"));
160-
Browser.Equal("Expected a maximum of 1 files, but got 2.", () => exceptionMessage.Text);
160+
Browser.Equal("The maximum number of files accepted is 1, but 2 were supplied.", () => exceptionMessage.Text);
161161
}
162162

163163
[Fact]
@@ -176,7 +176,7 @@ public void ThrowsWhenOversizedFileIsSelected()
176176

177177
// Validate that the proper exception is thrown
178178
var exceptionMessage = Browser.FindElement(By.Id("exception-message"));
179-
Browser.Equal("File with size 32 bytes exceeded the maximum of 10 bytes.", () => exceptionMessage.Text);
179+
Browser.Equal("Supplied file with size 32 bytes exceeds the maximum of 10 bytes.", () => exceptionMessage.Text);
180180
}
181181

182182
public void Dispose()

src/Components/test/testassets/BasicTestApp/FormsTest/InputFileComponent.razor

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
@using System.IO;
2-
@using Microsoft.AspNetCore.Components.Forms
2+
@using Microsoft.AspNetCore.Components.Forms
33

44
<h1>File preview</h1>
55

6-
Max file size:<br />
7-
<input type="number" id="max-file-size" @bind-value="@maxFileSize" /><br />
6+
Max file size:
7+
<br />
8+
<input type="number" id="max-file-size" @bind-value="@maxFileSize" />
9+
<br />
810

9-
Max allowed files:<br />
10-
<input type="number" id="max-allowed-files" @bind-value="@maxAllowedFiles" /><br />
11+
Max allowed files:
12+
<br />
13+
<input type="number" id="max-allowed-files" @bind-value="@maxAllowedFiles" />
14+
<br />
1115

12-
<InputFile OnChange="LoadFiles" MaxFileSize="maxFileSize" MaxAllowedFiles="maxAllowedFiles" id="input-file" multiple /><br />
16+
<InputFile OnChange="LoadFiles" id="input-file" multiple />
17+
<br />
1318

1419
<span id="exception-message">@exceptionMessage</span>
1520

1621
@if (isLoading)
1722
{
18-
<p>Loading...</p><br />
23+
<p>Loading...</p>
24+
<br />
1925
}
2026

2127
@foreach (var (file, content) in loadedFiles)
@@ -29,7 +35,8 @@ Max allowed files:<br />
2935

3036
<h1>Image upload</h1>
3137

32-
<InputFile OnChange="LoadImage" id="input-image" /><br />
38+
<InputFile OnChange="LoadImage" id="input-image" />
39+
<br />
3340

3441
@if (imageDataUri != null)
3542
{
@@ -64,16 +71,16 @@ Max allowed files:<br />
6471

6572
try
6673
{
67-
foreach (var file in e.Files)
74+
foreach (var file in e.GetMultipleFiles(maxAllowedFiles))
6875
{
6976
StateHasChanged();
7077

71-
using var reader = new StreamReader(file.OpenReadStream());
78+
using var reader = new StreamReader(file.OpenReadStream(maxFileSize));
7279

7380
loadedFiles.Add(file, await reader.ReadToEndAsync());
7481
}
7582
}
76-
catch (InvalidOperationException ex)
83+
catch (Exception ex)
7784
{
7885
exceptionMessage = ex.Message;
7986
}
@@ -83,19 +90,13 @@ Max allowed files:<br />
8390

8491
async Task LoadImage(InputFileChangeEventArgs e)
8592
{
86-
var file = e.Files.SingleOrDefault();
93+
var format = "image/jpeg";
94+
var imageFile = await e.File.RequestImageFileAsync(format, 640, 480);
8795

88-
if (file != null)
89-
{
90-
var format = "image/jpeg";
91-
var imageFile = await file.RequestImageFileAsync(format, 640, 480);
92-
93-
using var fileStream = imageFile.OpenReadStream();
94-
using var memoryStream = new MemoryStream();
95-
await fileStream.CopyToAsync(memoryStream);
96+
using var fileStream = imageFile.OpenReadStream(maxFileSize);
97+
using var memoryStream = new MemoryStream();
98+
await fileStream.CopyToAsync(memoryStream);
9699

97-
imageDataUri = $"data:{format};base64,{Convert.ToBase64String(memoryStream.ToArray())}";
98-
StateHasChanged();
99-
}
100+
imageDataUri = $"data:{format};base64,{Convert.ToBase64String(memoryStream.ToArray())}";
100101
}
101102
}

0 commit comments

Comments
 (0)