Skip to content

Commit

Permalink
Porting SaveAS API to HttpPostedFile
Browse files Browse the repository at this point in the history
  • Loading branch information
birojnayak committed Dec 15, 2023
1 parent 1a0c991 commit 67db52e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public partial class HttpContextWrapper : System.Web.HttpContextBase
public override System.Security.Principal.IPrincipal User { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} set { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public override void AddError(System.Exception ex) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void ClearError() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override object GetService(System.Type serviceType) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void RewritePath(string path) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void RewritePath(string path, bool rebaseClientPath) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public override void RewritePath(string filePath, string pathInfo, string queryString) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
Expand Down Expand Up @@ -313,6 +314,7 @@ internal HttpPostedFile() { }
public string ContentType { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string FileName { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.IO.Stream InputStream { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public void SaveAs(string filename) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public abstract partial class HttpPostedFileBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void RewritePath(string filePath, string pathInfo, string? queryString, b
return Server;
}

return null;
return Context.RequestServices?.GetService(service);
}

public ISubscriptionToken DisposeOnPipelineCompleted(IDisposable target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ public override IPrincipal User
public override void RewritePath(string filePath, string pathInfo, string? queryString, bool setClientFilePath) => _context.RewritePath(filePath, pathInfo, queryString, setClientFilePath);

public override void SetSessionStateBehavior(SessionStateBehavior sessionStateBehavior) => _context.SetSessionStateBehavior(sessionStateBehavior);

public override object? GetService(Type serviceType) => ((IServiceProvider)_context).GetService(serviceType);
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpPostedFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.IO;
using Microsoft.AspNetCore.Http;

Expand All @@ -19,4 +20,18 @@ public sealed class HttpPostedFile
public int ContentLength => (int)File.Length;

public Stream InputStream => File.OpenReadStream();

public void SaveAs(string filename)
{
if (!Path.IsPathRooted(filename))
{
throw new HttpException(string.Format(CultureInfo.InvariantCulture,
"The SaveAs method is configured to require a rooted path, and the path '{0}' is not rooted", filename));
}

using (var fileStream = new FileStream(filename, FileMode.Create))
{
InputStream.CopyTo(fileStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public void CacheFromHttpContext()

// Assert
Assert.Same(cache, result);

//Act via GetService
var cacheFromService = context.GetService<Cache>();
Assert.Same(cache, cacheFromService);
}

[Fact]
Expand All @@ -54,6 +58,10 @@ public void CacheFromHttpContextWrapper()

// Assert
Assert.Same(cache, result);

//Act via GetService
var cacheFromService = contextWrapper.GetService<Cache>();
Assert.Same(cache, cacheFromService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SystemWebAdapters.Features;
using Microsoft.AspNetCore.SystemWebAdapters.SessionState;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

Expand Down Expand Up @@ -152,8 +153,8 @@ public void GetServiceReturnsExpected()
Assert.Same(context.Response, provider.GetService(typeof(HttpResponse)));
Assert.Same(context.Server, provider.GetService(typeof(HttpServerUtility)));
Assert.Same(context.Session, provider.GetService(typeof(HttpSessionState)));

Assert.Null(provider.GetService(typeof(HttpContext)));
Assert.Null(provider.GetService<Cache>());
}

[Fact]
Expand Down Expand Up @@ -215,6 +216,10 @@ public void CacheFromServices()

// Assert
Assert.Same(cache, result);

var provider = (IServiceProvider)context;
Assert.NotNull(provider.GetService<Cache>());
Assert.Same(cache, provider.GetService<Cache>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Globalization;
using System.IO;
using System.Web;
using AutoFixture;
Expand Down Expand Up @@ -100,4 +102,37 @@ public void InputStream()
// Assert
Assert.Equal(expected.Object, stream);
}

[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);
}
}

0 comments on commit 67db52e

Please sign in to comment.