-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Fix IBrowserFile Name property to return FileName instead of form field name #63238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
184891a
Initial plan
Copilot 3a47882
Fix IBrowserFile LastModified header parsing issue and add tests
Copilot dc08c2e
Fix IBrowserFile Name property to use FileName instead of Name and ad…
Copilot d2497e3
Add E2E test for IBrowserFile.Name property mapping to verify file na…
Copilot 3ab7921
Remove IBrowserFileNameReturnsFileNameNotFormFieldName E2E test
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2024,7 +2024,7 @@ public void CanDeserialize_ComplexType_CanSerializerBrowserFile() | |
|
||
// Assert | ||
var browserFile = Assert.IsAssignableFrom<IBrowserFile>(result); | ||
Assert.Equal("file", browserFile.Name); | ||
Assert.Equal("file.txt", browserFile.Name); | ||
Assert.Equal(expectedString.Length, browserFile.Size); | ||
var buffer = new byte[browserFile.Size]; | ||
browserFile.OpenReadStream().Read(buffer); | ||
|
@@ -2059,18 +2059,18 @@ public void CanDeserialize_ComplexType_CanSerializerIReadOnlyListBrowserFile() | |
var browserFiles = Assert.IsAssignableFrom<IReadOnlyList<IBrowserFile>>(result); | ||
// First file | ||
var browserFile1 = browserFiles[0]; | ||
Assert.Equal("file", browserFile1.Name); | ||
Assert.Equal("file1.txt", browserFile1.Name); | ||
Assert.Equal(expectedString1.Length, browserFile1.Size); | ||
var buffer1 = new byte[browserFile1.Size]; | ||
browserFile1.OpenReadStream().Read(buffer1); | ||
Assert.Equal(expectedString1, Encoding.UTF8.GetString(buffer1, 0, buffer1.Length)); | ||
// Second files | ||
var browserFile2 = browserFiles[0]; | ||
Assert.Equal("file", browserFile2.Name); | ||
Assert.Equal(expectedString1.Length, browserFile2.Size); | ||
var browserFile2 = browserFiles[1]; | ||
Assert.Equal("file2.txt", browserFile2.Name); | ||
Assert.Equal(expectedString2.Length, browserFile2.Size); | ||
var buffer2 = new byte[browserFile2.Size]; | ||
browserFile1.OpenReadStream().Read(buffer2); | ||
Assert.Equal(expectedString1, Encoding.UTF8.GetString(buffer2, 0, buffer2.Length)); | ||
browserFile2.OpenReadStream().Read(buffer2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line correctly fixes the bug where the test was incorrectly reading from browserFile1's stream instead of browserFile2's stream when testing the second file. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
Assert.Equal(expectedString2, Encoding.UTF8.GetString(buffer2, 0, buffer2.Length)); | ||
} | ||
|
||
[Fact] | ||
|
105 changes: 105 additions & 0 deletions
105
src/Components/Endpoints/test/FormMapping/BrowserFileFromFormFileTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using Microsoft.AspNetCore.Components.Endpoints.FormMapping; | ||
using Microsoft.AspNetCore.Components.Forms; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Primitives; | ||
using Moq; | ||
|
||
namespace Microsoft.AspNetCore.Components.Endpoints.Tests.FormMapping; | ||
|
||
public class BrowserFileFromFormFileTests | ||
{ | ||
[Fact] | ||
public void Name_ReturnsFormFileFileName_NotFormFileName() | ||
{ | ||
// Arrange | ||
const string expectedFileName = "document.pdf"; | ||
const string formFieldName = "fileInput"; | ||
|
||
var mockFormFile = new Mock<IFormFile>(); | ||
mockFormFile.Setup(x => x.FileName).Returns(expectedFileName); | ||
mockFormFile.Setup(x => x.Name).Returns(formFieldName); | ||
|
||
var browserFile = new BrowserFileFromFormFile(mockFormFile.Object); | ||
|
||
// Act | ||
var actualName = browserFile.Name; | ||
|
||
// Assert | ||
Assert.Equal(expectedFileName, actualName); | ||
Assert.NotEqual(formFieldName, actualName); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_IBrowserFile_ReturnsCorrectFileName() | ||
{ | ||
// Arrange | ||
const string prefixName = "fileInput"; | ||
const string expectedFileName = "upload.txt"; | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var mockFormFile = new Mock<IFormFile>(); | ||
mockFormFile.Setup(x => x.FileName).Returns(expectedFileName); | ||
mockFormFile.Setup(x => x.Name).Returns(prefixName); | ||
|
||
var formFileCollection = new Mock<IFormFileCollection>(); | ||
formFileCollection.Setup(x => x.GetFile(prefixName)).Returns(mockFormFile.Object); | ||
|
||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(new Dictionary<FormKey, StringValues>(), culture, buffer, formFileCollection.Object); | ||
reader.PushPrefix(prefixName); | ||
|
||
var converter = new FileConverter<IBrowserFile>(); | ||
|
||
// Act | ||
var result = converter.TryRead(ref reader, typeof(IBrowserFile), default!, out var browserFile, out var found); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(found); | ||
Assert.NotNull(browserFile); | ||
Assert.Equal(expectedFileName, browserFile.Name); | ||
} | ||
|
||
[Fact] | ||
public void TryRead_IBrowserFileList_ReturnsCorrectFileNames() | ||
{ | ||
// Arrange | ||
const string prefixName = "fileInputs"; | ||
const string expectedFileName1 = "file1.txt"; | ||
const string expectedFileName2 = "file2.jpg"; | ||
var culture = CultureInfo.GetCultureInfo("en-US"); | ||
|
||
var mockFormFile1 = new Mock<IFormFile>(); | ||
mockFormFile1.Setup(x => x.FileName).Returns(expectedFileName1); | ||
mockFormFile1.Setup(x => x.Name).Returns(prefixName); | ||
|
||
var mockFormFile2 = new Mock<IFormFile>(); | ||
mockFormFile2.Setup(x => x.FileName).Returns(expectedFileName2); | ||
mockFormFile2.Setup(x => x.Name).Returns(prefixName); | ||
|
||
var formFiles = new List<IFormFile> { mockFormFile1.Object, mockFormFile2.Object }; | ||
var formFileCollection = new Mock<IFormFileCollection>(); | ||
formFileCollection.Setup(x => x.GetFiles(prefixName)).Returns(formFiles); | ||
|
||
var buffer = prefixName.ToCharArray().AsMemory(); | ||
var reader = new FormDataReader(new Dictionary<FormKey, StringValues>(), culture, buffer, formFileCollection.Object); | ||
reader.PushPrefix(prefixName); | ||
|
||
var converter = new FileConverter<IReadOnlyList<IBrowserFile>>(); | ||
|
||
// Act | ||
var result = converter.TryRead(ref reader, typeof(IReadOnlyList<IBrowserFile>), default!, out var browserFiles, out var found); | ||
|
||
// Assert | ||
Assert.True(result); | ||
Assert.True(found); | ||
Assert.NotNull(browserFiles); | ||
Assert.Equal(2, browserFiles.Count); | ||
Assert.Equal(expectedFileName1, browserFiles[0].Name); | ||
Assert.Equal(expectedFileName2, browserFiles[1].Name); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line correctly fixes the bug where browserFile2 was incorrectly assigned to browserFiles[0] instead of browserFiles[1], but the change appears to be fixing a pre-existing test bug rather than being directly related to the Name property fix.
Copilot uses AI. Check for mistakes.