Skip to content

Commit f450bac

Browse files
committed
Fix handling of relative paths in OWIN. References #253.
1 parent fe79d81 commit f450bac

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/React.Owin/EntryAssemblyFileSystem.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,41 @@ namespace React.Owin
1717
/// </summary>
1818
internal class EntryAssemblyFileSystem : FileSystemBase
1919
{
20+
private readonly string _rootPath;
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="EntryAssemblyFileSystem"/> class, using the
24+
/// entry assembly's location as the root directory.
25+
/// </summary>
26+
public EntryAssemblyFileSystem()
27+
: this(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
28+
{
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="EntryAssemblyFileSystem"/> class, using the
33+
/// specified path as the root directory.
34+
/// </summary>
35+
/// <param name="rootPath">The root path.</param>
36+
public EntryAssemblyFileSystem(string rootPath)
37+
{
38+
_rootPath = rootPath;
39+
}
40+
41+
/// <summary>
42+
/// Converts a path from an application relative path (~/...) to a full filesystem path
43+
/// </summary>
44+
/// <param name="relativePath">App-relative path of the file</param>
45+
/// <returns>Full path of the file</returns>
2046
public override string MapPath(string relativePath)
2147
{
2248
if (relativePath.StartsWith("~"))
23-
return Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), relativePath.Replace("~", string.Empty));
49+
{
50+
relativePath = relativePath
51+
.Replace("~/", string.Empty)
52+
.Replace('/', '\\');
53+
return Path.Combine(_rootPath, relativePath);
54+
}
2455

2556
return relativePath;
2657
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2016-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using NUnit.Framework;
11+
using React.Owin;
12+
13+
namespace React.Tests.Owin
14+
{
15+
[TestFixture]
16+
public class EntryAssemblyFileSystemTests
17+
{
18+
[TestCase("C:\\", "~/", "C:\\")]
19+
[TestCase("C:\\", "~/foo/bar.js", "C:\\foo\\bar.js")]
20+
public void MapPath(string rootPath, string relativePath, string expected)
21+
{
22+
var fileSystem = new EntryAssemblyFileSystem(rootPath);
23+
var result = fileSystem.MapPath(relativePath);
24+
Assert.AreEqual(expected, result);
25+
}
26+
}
27+
}

src/React.Tests/React.Tests.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>React.Tests</RootNamespace>
1111
<AssemblyName>React.Tests</AssemblyName>
12-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
</PropertyGroup>
1516
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1617
<DebugSymbols>true</DebugSymbols>
@@ -22,6 +23,7 @@
2223
<WarningLevel>4</WarningLevel>
2324
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
2425
<NoWarn>1607</NoWarn>
26+
<Prefer32Bit>false</Prefer32Bit>
2527
</PropertyGroup>
2628
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2729
<DebugType>pdbonly</DebugType>
@@ -32,6 +34,7 @@
3234
<WarningLevel>4</WarningLevel>
3335
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3436
<NoWarn>1607</NoWarn>
37+
<Prefer32Bit>false</Prefer32Bit>
3538
</PropertyGroup>
3639
<PropertyGroup>
3740
<SignAssembly>true</SignAssembly>
@@ -105,13 +108,18 @@
105108
<Compile Include="Core\BabelTransformerTests.cs" />
106109
<Compile Include="Core\ReactComponentTest.cs" />
107110
<Compile Include="Mvc\HtmlHelperExtensionsTests.cs" />
111+
<Compile Include="Owin\EntryAssemblyFileSystemTests.cs" />
108112
<Compile Include="Properties\AssemblyInfo.cs" />
109113
<Compile Include="Core\ReactEnvironmentTest.cs" />
110114
</ItemGroup>
111115
<ItemGroup>
112116
<None Include="packages.config" />
113117
</ItemGroup>
114118
<ItemGroup>
119+
<ProjectReference Include="..\React.Owin\React.Owin.csproj">
120+
<Project>{c3bf8d49-b7cc-4d7f-b0f0-a94347c595ea}</Project>
121+
<Name>React.Owin</Name>
122+
</ProjectReference>
115123
<ProjectReference Include="..\React.Web.Mvc4\React.Web.Mvc4.csproj">
116124
<Project>{662d52ac-1ee9-4372-bd74-379f9ac56451}</Project>
117125
<Name>React.Web.Mvc4</Name>

0 commit comments

Comments
 (0)