Skip to content

Commit 11c3ee1

Browse files
author
Biroj Nayak
committed
Porting SaveAS API to HttpPostedFile
1 parent 73d61ec commit 11c3ee1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters/HttpPostedFile.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public sealed class HttpPostedFile
2323

2424
public void SaveAs(string filename)
2525
{
26-
2726
if (!Path.IsPathRooted(filename))
2827
{
2928
throw new HttpException(string.Format(CultureInfo.InvariantCulture,
@@ -32,7 +31,6 @@ public void SaveAs(string filename)
3231

3332
using (var fileStream = new FileStream(filename, FileMode.Create))
3433
{
35-
InputStream.Seek(0, SeekOrigin.Begin);
3634
InputStream.CopyTo(fileStream);
3735
}
3836
}

test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpPostedFileTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
5+
using System.Globalization;
46
using System.IO;
57
using System.Web;
68
using AutoFixture;
@@ -102,13 +104,35 @@ public void InputStream()
102104
}
103105

104106
[Fact]
105-
public void SaveAs()
107+
public void SaveAsForInvalidRootPath()
106108
{
109+
// Arrange
107110
var file = new Mock<IFormFile>();
108111
var expectedStream = new Mock<Stream>();
109112
file.Setup(f => f.OpenReadStream()).Returns(expectedStream.Object);
110-
111113
var posted = new HttpPostedFile(file.Object);
114+
115+
//Act and Assert
112116
Assert.Throws<HttpException>(() => posted.SaveAs("InvalidPath"));
113117
}
118+
119+
[Fact]
120+
public void SaveAsWithValidRootPath()
121+
{
122+
// Arrange
123+
var file = new Mock<IFormFile>();
124+
var expectedStream = new Mock<Stream>();
125+
file.Setup(f => f.OpenReadStream()).Returns(expectedStream.Object);
126+
string validTempPath = string.Format(CultureInfo.InvariantCulture, @"{0}{1}.txt", Path.GetTempPath(), Guid.NewGuid());
127+
var posted = new HttpPostedFile(file.Object);
128+
129+
//Act
130+
posted.SaveAs(validTempPath);
131+
132+
//Assert
133+
Assert.True(File.Exists(validTempPath), "Temp file should be created");
134+
135+
//Cleanup
136+
File.Delete(validTempPath);
137+
}
114138
}

0 commit comments

Comments
 (0)