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

Add HttpRequest.FilePath and HttpRequest.PathInfo #285

Merged
merged 6 commits into from
Feb 10, 2023
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
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET6_0_OR_GREATER
namespace Microsoft.AspNetCore.SystemWebAdapters;

internal interface IPathInfoFeature
{
string PathInfo { get; }

string FileInfo { get; }
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ internal HttpRequest() { }
public int ContentLength { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string ContentType { 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 System.Web.HttpCookieCollection Cookies { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string FilePath { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Web.HttpFileCollection Files { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Collections.Specialized.NameValueCollection Form { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Collections.Specialized.NameValueCollection Headers { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
Expand All @@ -173,6 +174,7 @@ internal HttpRequest() { }
public System.Security.Principal.IIdentity LogonUserIdentity { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Collections.Specialized.NameValueCollection Params { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string Path { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string PathInfo { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Collections.Specialized.NameValueCollection QueryString { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public string RawUrl { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Web.ReadEntityBodyMode ReadEntityBodyMode { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal HttpRequest(HttpRequestCore request)

public string? Path => _request.Path.Value;

public string? PathInfo => _request.HttpContext.Features.Get<IPathInfoFeature>()?.PathInfo ?? string.Empty;

public string? FilePath => _request.HttpContext.Features.Get<IPathInfoFeature>()?.FileInfo ?? Path;

public NameValueCollection Headers => _headers ??= _request.Headers.ToNameValueCollection();

public Uri Url => new(_request.GetEncodedUrl());
Expand Down Expand Up @@ -180,7 +184,7 @@ public bool IsLocal
}
}

public string AppRelativeCurrentExecutionFilePath => $"~{_request.Path.Value}";
public string AppRelativeCurrentExecutionFilePath => $"~{FilePath}";

public string ApplicationPath => _request.HttpContext.RequestServices.GetRequiredService<IHttpRuntime>().AppDomainAppVirtualPath;

Expand Down
104 changes: 102 additions & 2 deletions test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public HttpRequestTests()
public void Path()
{
// Arrange
var path = new PathString("/" + _fixture.Create<string>());
var path = new PathString(CreateRandomPath());
var coreRequest = new Mock<HttpRequestCore>();
coreRequest.Setup(c => c.Path).Returns(path);

Expand All @@ -58,6 +58,101 @@ public void Path()
Assert.Equal(path.Value, result);
}

[Fact]
public void FilePathNoFeature()
{
// Arrange
var context = new Mock<HttpContextCore>();
context.Setup(c => c.Features).Returns(new FeatureCollection());

var coreRequest = new Mock<HttpRequestCore>();
var path = new PathString(CreateRandomPath());
coreRequest.Setup(c => c.Path).Returns(path);
coreRequest.Setup(c => c.HttpContext).Returns(context.Object);

var request = new HttpRequest(coreRequest.Object);

// Act
var result = request.FilePath;

// Assert
Assert.Equal(path, result);
}

[Fact]
public void FilePathFeature()
{
// Arrange
var feature = new Mock<IPathInfoFeature>();
var path = CreateRandomPath();
feature.Setup(f => f.FileInfo).Returns(path);

var features = new FeatureCollection();
features.Set(feature.Object);

var context = new Mock<HttpContextCore>();
context.Setup(c => c.Features).Returns(features);

var coreRequest = new Mock<HttpRequestCore>();
coreRequest.Setup(c => c.Path).Returns(CreateRandomPath());
coreRequest.Setup(c => c.HttpContext).Returns(context.Object);

var request = new HttpRequest(coreRequest.Object);

// Act
var result = request.FilePath;

// Assert
Assert.Equal(path, result);
}

[Fact]
public void PathInfoNoFeature()
{
// Arrange
var context = new Mock<HttpContextCore>();
context.Setup(c => c.Features).Returns(new FeatureCollection());

var coreRequest = new Mock<HttpRequestCore>();
coreRequest.Setup(c => c.Path).Returns(CreateRandomPath());
coreRequest.Setup(c => c.HttpContext).Returns(context.Object);

var request = new HttpRequest(coreRequest.Object);

// Act
var result = request.PathInfo;

// Assert
Assert.Same(string.Empty, result);
}

[Fact]
public void PathInfoFeature()
{
// Arrange
var feature = new Mock<IPathInfoFeature>();
var path = CreateRandomPath();
feature.Setup(f => f.PathInfo).Returns(path);

var features = new FeatureCollection();
features.Set(feature.Object);

var context = new Mock<HttpContextCore>();
context.Setup(c => c.Features).Returns(features);

var coreRequest = new Mock<HttpRequestCore>();
coreRequest.Setup(c => c.Path).Returns(CreateRandomPath());
coreRequest.Setup(c => c.HttpContext).Returns(context.Object);

var request = new HttpRequest(coreRequest.Object);

// Act
var result = request.PathInfo;

// Assert
Assert.Equal(path, result);
}

[Fact]
public void Url()
{
Expand Down Expand Up @@ -395,17 +490,20 @@ public void IsLocalNullLocal(bool isLoopback)
public void AppRelativeCurrentExecutionFilePath()
{
// Arrange
var context = new Mock<HttpContextCore>();
context.Setup(c => c.Features).Returns(new FeatureCollection());

var coreRequest = new Mock<HttpRequestCore>();
coreRequest.Setup(c => c.Scheme).Returns("http");
coreRequest.Setup(c => c.Host).Returns(new HostString("www.A.com"));
coreRequest.Setup(c => c.Path).Returns("/B/ C");
coreRequest.Setup(c => c.QueryString).Returns(new QueryString("?D=E"));
coreRequest.Setup(c => c.PathBase).Returns("/F");
coreRequest.Setup(c => c.HttpContext).Returns(context.Object);

var request = new HttpRequest(coreRequest.Object);

// Act
var url = request.Url;
var result = request.AppRelativeCurrentExecutionFilePath;

// Assert
Expand Down Expand Up @@ -1134,5 +1232,7 @@ public void EntityReadMode(ReadEntityBodyMode mode)
// Assert
Assert.Equal(mode, result);
}

private string CreateRandomPath() => "/" + _fixture.Create<string>();
}
}