Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.AspNetCore.Components.Endpoints.FormMapping;

internal sealed class BrowserFileFromFormFile(IFormFile formFile) : IBrowserFile
{
public string Name => formFile.Name;
public string Name => formFile.FileName;

public DateTimeOffset LastModified => DateTimeOffset.Parse(formFile.Headers.LastModified.ToString(), CultureInfo.InvariantCulture);

Expand Down
14 changes: 7 additions & 7 deletions src/Components/Endpoints/test/Binding/FormDataMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Copy link
Preview

Copilot AI Aug 14, 2025

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.

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);
Copy link
Preview

Copilot AI Aug 14, 2025

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 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.

Assert.Equal(expectedString2, Encoding.UTF8.GetString(buffer2, 0, buffer2.Length));
}

[Fact]
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,7 @@ public void CanBindToFormWithFiles()
Assert.Equal($"Profile Picture: {profilePicture.Name}", Browser.Exists(By.Id("profile-picture")).Text);
Assert.Equal("Documents: 2", Browser.Exists(By.Id("documents")).Text);
Assert.Equal("Images: 3", Browser.Exists(By.Id("images")).Text);
Assert.Equal("Header Photo: Model.HeaderPhoto", Browser.Exists(By.Id("header-photo")).Text);
Assert.Equal($"Header Photo: {headerPhoto.Name}", Browser.Exists(By.Id("header-photo")).Text);
Assert.Equal("Total: 7", Browser.Exists(By.Id("form-collection")).Text);
}

Expand Down
Loading