From 0dbbd2f655b7b7490a799881bd02b77629e70c9e Mon Sep 17 00:00:00 2001 From: Praburaj Date: Mon, 23 Mar 2015 17:19:27 -0700 Subject: [PATCH] Adding some tests for the MapPath extension method And looks like I pushed the MapPath extension change too soon. I had to fix an issue in the path for forward and backward slash. For example if an application tries to do MapPath("/resource") then this code does not work cross plat unless the directory separator characters are fixed appropriately. Fixed it and also added some test coverage. --- .../HostingEnvironmentExtensions.cs | 2 ++ .../HostingEngineTests.cs | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs b/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs index 0af82fee14..8de0d9f97a 100644 --- a/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs +++ b/src/Microsoft.AspNet.Hosting.Interfaces/HostingEnvironmentExtensions.cs @@ -40,6 +40,8 @@ namespace Microsoft.AspNet.Hosting return hostingEnvironment.WebRootPath; } + // On windows replace / with \. + virtualPath = virtualPath.Replace('/', Path.DirectorySeparatorChar); return Path.Combine(hostingEnvironment.WebRootPath, virtualPath); } } diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs index 078bc3681e..7bebf1c0e7 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs @@ -131,6 +131,32 @@ namespace Microsoft.AspNet.Hosting } } + [Theory] + [InlineData(null, "")] + [InlineData("", "")] + [InlineData("/", "/")] + [InlineData(@"\", @"\")] + [InlineData("sub", "sub")] + [InlineData("sub/sub2/sub3", @"sub/sub2/sub3")] + [InlineData(@"sub/sub2\sub3\", @"sub/sub2/sub3/")] + public void MapPath_Facts(string virtualPath, string expectedSuffix) + { + var context = new HostingContext + { + ServerFactory = this + }; + + var engine = new HostingEngine(); + + using (engine.Start(context)) + { + var env = context.ApplicationServices.GetRequiredService(); + var mappedPath = env.MapPath(virtualPath); + expectedSuffix = expectedSuffix.Replace('/', Path.DirectorySeparatorChar); + Assert.Equal(Path.Combine(env.WebRootPath, expectedSuffix), mappedPath); + } + } + public void Initialize(IApplicationBuilder builder) {