Skip to content
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

Missing(Porting) SaveAS API to HttpPostedFile #459

Merged
merged 2 commits into from
Mar 11, 2024

Conversation

birojnayak
Copy link
Contributor

@birojnayak birojnayak commented Dec 15, 2023

This will bring the SaveAS API to HttpPostedFile, which will be used in FileUpload control CoreWebForms/CoreWebForms#29

Fixes #451

[Fact]
public void SaveAs()
{
var file = new Mock<IFormFile>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you follow the arrange/act/assert pattern as above? Can you add a test for something rooted? maybe to the temp directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@twsouthwick does this look ok ? (Dividing into 2 separate test cases)

    [Fact]
    public void SaveAsForInvalidRootPath()
    {
        // Arrange
        var file = new Mock<IFormFile>();
        var expectedStream = new Mock<Stream>();
        file.Setup(f => f.OpenReadStream()).Returns(expectedStream.Object);
        var posted = new HttpPostedFile(file.Object);

        //Act and Assert
        Assert.Throws<HttpException>(() => posted.SaveAs("InvalidPath"));
    }

    [Fact]
    public void SaveAsWithValidRootPath()
    {
        // Arrange
        var file = new Mock<IFormFile>();
        var expectedStream = new Mock<Stream>();
        file.Setup(f => f.OpenReadStream()).Returns(expectedStream.Object);
        string validTempPath = string.Format(CultureInfo.InvariantCulture, @"{0}{1}.txt", Path.GetTempPath(), Guid.NewGuid());
        var posted = new HttpPostedFile(file.Object);

        //Act
        posted.SaveAs(validTempPath);

        //Assert
        Assert.True(File.Exists(validTempPath), "Temp file should be created");

        //Cleanup
        File.Delete(validTempPath);
    }


using (var fileStream = new FileStream(filename, FileMode.Create))
{
InputStream.Seek(0, SeekOrigin.Begin);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary


public void SaveAs(string filename)
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@twsouthwick
Copy link
Member

btw putting "Fixes ..." at the bottom automatically links to the issue and will close it upon completion (as well as kicks off some automation we have)

@birojnayak
Copy link
Contributor Author

btw putting "Fixes ..." at the bottom automatically links to the issue and will close it upon completion (as well as kicks off some automation we have)

ok.. I have been tagging in the description , can stick to what suggested here as long as it triggers the automation.

@@ -163,7 +163,7 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b
return Server;
}

return null;
return Context.RequestServices?.GetService(service);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there's a weird merge thing going on

@twsouthwick
Copy link
Member

@birojnayak there appears to be a weird merge thing going on. Can you clean that up? The #457 change is showing up here when it shouldn't be

@AvremelM
Copy link
Contributor

@birojnayak Is there a chance this PR could also add SaveAs() to HttpPostedFileBase and HttpPostedFileWrapper as well?

@birojnayak
Copy link
Contributor Author

@twsouthwick feel free to merge it.. added to HttpPostedFileBase and HttpPostedFileWrapper

@twsouthwick twsouthwick added this to the 1.4 milestone Feb 28, 2024
@twsouthwick twsouthwick merged commit 4e5cd12 into dotnet:main Mar 11, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpPostedFile missing SaveAS method ?
3 participants