Skip to content

Commit

Permalink
Merge remote branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jan 7, 2012
2 parents 8a5aeab + a4cb559 commit c193642
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 31 deletions.
31 changes: 29 additions & 2 deletions src/ServiceStack.Common/Utils/PathUtils.cs
Expand Up @@ -13,20 +13,47 @@ public static string MapAbsolutePath(string relativePath, string appendPartialPa
var assemblyDirectoryPath = Path.GetDirectoryName(new Uri(typeof(PathUtils).Assembly.EscapedCodeBase).LocalPath);

// Escape the assembly bin directory to the hostname directory
var hostDirectoryPath = assemblyDirectoryPath + appendPartialPathModifier;
var hostDirectoryPath = appendPartialPathModifier != null
? assemblyDirectoryPath + appendPartialPathModifier
: assemblyDirectoryPath;

return Path.GetFullPath(relativePath.Replace("~", hostDirectoryPath));
}

return relativePath;
}

public static string MapAbsolutePath(this string relativePath)
/// <summary>
/// Maps the path of a file in the context of a VS project
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is two directories above the /bin/ directory,
/// eg. in a unit test scenario the assembly would be in /bin/Debug/.</remarks>
public static string MapProjectPath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..{0}..", Path.DirectorySeparatorChar));
return mapPath;
}

/// <summary>
/// Maps the path of a file in a self-hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is copied to /bin/ folder with the assemblies</remarks>
public static string MapAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, null);
return mapPath;
}

/// <summary>
/// Maps the path of a file in an Asp.Net hosted scenario
/// </summary>
/// <param name="relativePath">the relative path</param>
/// <returns>the absolute path</returns>
/// <remarks>Assumes static content is in the parent folder of the /bin/ directory</remarks>
public static string MapHostAbsolutePath(this string relativePath)
{
var mapPath = MapAbsolutePath(relativePath, string.Format("{0}..", Path.DirectorySeparatorChar));
Expand Down
Expand Up @@ -60,7 +60,7 @@ public static IEnumerable UserAuthRepositorys
sqliteRepo.Clear();
yield return new TestCaseData(sqliteRepo);

var dbFilePath = "~/App_Data/auth.sqlite".MapAbsolutePath();
var dbFilePath = "~/App_Data/auth.sqlite".MapProjectPath();
if (File.Exists(dbFilePath)) File.Delete(dbFilePath);
var sqliteDbFactory = new OrmLiteConnectionFactory(dbFilePath);
var sqliteDbRepo = new OrmLiteAuthRepository(sqliteDbFactory);
Expand Down
Expand Up @@ -51,7 +51,7 @@ public void SetUp()
userAuthRepositorys.Add(sqliteInMemoryRepo);

var sqliteDbFactory = new OrmLiteConnectionFactory(
"~/App_Data/auth.sqlite".MapAbsolutePath());
"~/App_Data/auth.sqlite".MapProjectPath());
var sqliteDbRepo = new OrmLiteAuthRepository(sqliteDbFactory);
sqliteDbRepo.CreateMissingTables();
userAuthRepositorys.Add(sqliteDbRepo);
Expand Down
Expand Up @@ -45,7 +45,7 @@ public void TestFixtureSetUp()
//staticTemplatePath = "~/AppData/Template/default.shtml".MapAbsolutePath();
//staticTemplateContent = File.ReadAllText(staticTemplatePath);

dynamicPagePath = "~/Views/Template/DynamicTpl.md".MapAbsolutePath();
dynamicPagePath = "~/Views/Template/DynamicTpl.md".MapProjectPath();
dynamicPageContent = File.ReadAllText(dynamicPagePath);

//dynamicListPagePath = "~/Views/Template/DynamicListTpl.md".MapAbsolutePath();
Expand All @@ -61,7 +61,7 @@ public void OnBeforeEachTest()
[Test]
public void Can_load_all_markdown_files()
{
markdownFormat.RegisterMarkdownPages("~/".MapAbsolutePath());
markdownFormat.RegisterMarkdownPages("~/".MapProjectPath());

Assert.That(markdownFormat.ViewPages.Count, Is.EqualTo(viewPageNames.Length));
Assert.That(markdownFormat.ViewSharedPages.Count, Is.EqualTo(sharedViewPageNames.Length));
Expand All @@ -78,8 +78,8 @@ public void Can_load_all_markdown_files()
[Test]
public void Can_Render_StaticPage()
{
markdownFormat.RegisterMarkdownPages("~/".MapAbsolutePath());
var html = markdownFormat.RenderStaticPageHtml("~/AppData/NoTemplate/Static".MapAbsolutePath());
markdownFormat.RegisterMarkdownPages("~/".MapProjectPath());
var html = markdownFormat.RenderStaticPageHtml("~/AppData/NoTemplate/Static".MapProjectPath());

Assert.That(html, Is.Not.Null);
Assert.That(html, Is.StringStarting("<h1>Static Markdown template</h1>"));
Expand All @@ -88,8 +88,8 @@ public void Can_Render_StaticPage()
[Test]
public void Can_Render_StaticPage_WithTemplate()
{
markdownFormat.RegisterMarkdownPages("~/".MapAbsolutePath());
var html = markdownFormat.RenderStaticPageHtml("~/AppData/Template/StaticTpl".MapAbsolutePath());
markdownFormat.RegisterMarkdownPages("~/".MapProjectPath());
var html = markdownFormat.RenderStaticPageHtml("~/AppData/Template/StaticTpl".MapProjectPath());

Console.WriteLine(html);

Expand All @@ -101,7 +101,7 @@ public void Can_Render_StaticPage_WithTemplate()
public void Can_Render_DynamicPage()
{
var person = new Person { FirstName = "Demis", LastName = "Bellot" };
markdownFormat.RegisterMarkdownPages("~/".MapAbsolutePath());
markdownFormat.RegisterMarkdownPages("~/".MapProjectPath());

var html = markdownFormat.RenderDynamicPageHtml("Dynamic", person);

Expand Down
2 changes: 1 addition & 1 deletion tests/ServiceStack.ServiceHost.Tests/Formats/MockClass.cs
Expand Up @@ -27,7 +27,7 @@ public ServiceStack.Markdown.MvcHtmlString EvalExpr_0()
//[Test]
public void Compare_access()
{
var filePath = "~/AppData/TestsResults/Customer.htm".MapAbsolutePath();
var filePath = "~/AppData/TestsResults/Customer.htm".MapProjectPath();
const int Times = 10000;

var start = DateTime.Now;
Expand Down
6 changes: 3 additions & 3 deletions tests/ServiceStack.ServiceHost.Tests/Formats/TemplateTests.cs
Expand Up @@ -27,13 +27,13 @@ public class TemplateTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
staticTemplatePath = "~/Views/Template/default.shtml".MapAbsolutePath();
staticTemplatePath = "~/Views/Template/default.shtml".MapProjectPath();
staticTemplateContent = File.ReadAllText(staticTemplatePath);

dynamicPagePath = "~/Views/Template/DynamicTpl.md".MapAbsolutePath();
dynamicPagePath = "~/Views/Template/DynamicTpl.md".MapProjectPath();
dynamicPageContent = File.ReadAllText(dynamicPagePath);

dynamicListPagePath = "~/Views/Template/DynamicListTpl.md".MapAbsolutePath();
dynamicListPagePath = "~/Views/Template/DynamicListTpl.md".MapProjectPath();
dynamicListPageContent = File.ReadAllText(dynamicListPagePath);
}

Expand Down
Expand Up @@ -17,7 +17,7 @@ public class TextBlockTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
dynamicListPagePath = "~/Views/Template/DynamicListTpl.md".MapAbsolutePath();
dynamicListPagePath = "~/Views/Template/DynamicListTpl.md".MapProjectPath();
dynamicListPageContent = File.ReadAllText(dynamicListPagePath);
}

Expand Down
Expand Up @@ -73,7 +73,7 @@ public class UseCaseTests : MarkdownTestBase
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
var jsonPages = File.ReadAllText("~/AppData/Pages.json".MapAbsolutePath());
var jsonPages = File.ReadAllText("~/AppData/Pages.json".MapProjectPath());

Pages = JsonSerializer.DeserializeFromString<List<Page>>(jsonPages);

Expand Down
8 changes: 4 additions & 4 deletions tests/ServiceStack.ServiceHost.Tests/Formats/ViewTests.cs
Expand Up @@ -25,7 +25,7 @@ public class ViewTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
var json = "~/AppData/ALFKI.json".MapAbsolutePath().ReadAllText();
var json = "~/AppData/ALFKI.json".MapProjectPath().ReadAllText();
response = JsonSerializer.DeserializeFromString<CustomerDetailsResponse>(json);
}

Expand All @@ -42,7 +42,7 @@ public class AppHost : IAppHost
public AppHost()
{
this.Config = new EndpointHostConfig {
MarkdownSearchPath = "~".MapAbsolutePath(),
MarkdownSearchPath = "~".MapProjectPath(),
MarkdownReplaceTokens = new Dictionary<string, string>(),
IgnoreFormatsInMetadata = new HashSet<string>(),
};
Expand Down Expand Up @@ -235,14 +235,14 @@ public void Does_process_Markdown_pages()
var markdownHandler = new MarkdownHandler {
MarkdownFormat = markdownFormat,
PathInfo = "/AppData/NoTemplate/Static.md",
FilePath = "~/AppData/NoTemplate/Static.md".MapAbsolutePath(),
FilePath = "~/AppData/NoTemplate/Static.md".MapProjectPath(),
};
var httpReq = new MockHttpRequest { QueryString = new NameValueCollection() };
var httpRes = new MockHttpResponse();
markdownHandler.ProcessRequest(httpReq, httpRes, "Static");

var expectedHtml = markdownFormat.Transform(
File.ReadAllText("~/AppData/NoTemplate/Static.md".MapAbsolutePath()));
File.ReadAllText("~/AppData/NoTemplate/Static.md".MapProjectPath()));

httpRes.Close();
Assert.That(httpRes.Contents, Is.EqualTo(expectedHtml));
Expand Down
Expand Up @@ -121,13 +121,13 @@ public class RazorTemplateTests
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
staticTemplatePath = "~/Views/Template/_Layout.cshtml".MapAbsolutePath();
staticTemplatePath = "~/Views/Template/_Layout.cshtml".MapProjectPath();
staticTemplateContent = File.ReadAllText(staticTemplatePath);

dynamicPagePath = "~/Views/Template/DynamicTpl.cshtml".MapAbsolutePath();
dynamicPagePath = "~/Views/Template/DynamicTpl.cshtml".MapProjectPath();
dynamicPageContent = File.ReadAllText(dynamicPagePath);

dynamicListPagePath = "~/Views/Template/DynamicListTpl.cshtml".MapAbsolutePath();
dynamicListPagePath = "~/Views/Template/DynamicListTpl.cshtml".MapProjectPath();
dynamicListPageContent = File.ReadAllText(dynamicListPagePath);

templateArgs = person;
Expand Down
8 changes: 4 additions & 4 deletions tests/ServiceStack.WebHost.Endpoints.Tests/FileUploadTests.cs
Expand Up @@ -93,7 +93,7 @@ public void AssertResponse(HttpWebResponse response, string contentType)
[Test]
public void Can_POST_upload_file()
{
var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapAbsolutePath());
var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath());

var webRequest = (HttpWebRequest)WebRequest.Create(ListeningOn + "/fileuploads");
webRequest.Accept = ContentType.Json;
Expand All @@ -114,7 +114,7 @@ public void Can_POST_upload_file_using_ServiceClient()
{
IServiceClient client = new JsonServiceClient(ListeningOn);

var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapAbsolutePath());
var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath());


var response = client.PostFile<FileUploadResponse>(
Expand All @@ -133,7 +133,7 @@ public void Can_handle_error_on_POST_upload_file_using_ServiceClient()
{
IServiceClient client = new JsonServiceClient(ListeningOn);

var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapAbsolutePath());
var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath());

try
{
Expand All @@ -155,7 +155,7 @@ public void Can_handle_error_on_POST_upload_file_using_ServiceClient()
[Test]
public void Can_GET_upload_file()
{
var uploadedFile = new FileInfo("~/TestExistingDir/upload.html".MapAbsolutePath());
var uploadedFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath());
var webRequest = (HttpWebRequest)WebRequest.Create(ListeningOn + "/fileuploads/TestExistingDir/upload.html");
var expectedContents = new StreamReader(uploadedFile.OpenRead()).ReadToEnd();

Expand Down
Expand Up @@ -230,8 +230,12 @@
<Link>sqlite3.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="webpage.forbidden" />
<Content Include="webpage.html" />
<Content Include="webpage.forbidden">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="webpage.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestExistingDir\default.html" />
<Content Include="TestExistingDir\upload.html" />
</ItemGroup>
Expand Down
Expand Up @@ -46,7 +46,7 @@ public override object OnGet(FileUpload request)
if (request.RelativePath.IsNullOrEmpty())
throw new ArgumentNullException("RelativePath");

var filePath = ("~/" + request.RelativePath).MapAbsolutePath();
var filePath = ("~/" + request.RelativePath).MapProjectPath();
if (!File.Exists(filePath))
throw new FileNotFoundException(request.RelativePath);

Expand Down

0 comments on commit c193642

Please sign in to comment.