Permalink
Browse files

Merge pull request #591 from thecodejunkie/staticcontentinroot-587

Static content in root
  • Loading branch information...
grumpydev committed Apr 26, 2012
2 parents 87dcda5 + 12cbd9f commit 23d00cf1b11af2b03cb9109c35ff9fa04e509b1d
@@ -196,8 +196,8 @@
<Compile Include="Unit\Sessions\DefaultSessionObjectFormatterFixture.cs" />
<Compile Include="Unit\Sessions\NullSessionProviderFixture.cs" />
<Compile Include="Unit\Sessions\SessionFixture.cs" />
- <Compile Include="Unit\StaticConventBuilderFixture.cs" />
<Compile Include="Unit\TextFormatterFixture.cs" />
+ <Compile Include="Unit\StaticContentConventionBuilderFixture.cs" />
<Compile Include="Unit\UrlFixture.cs" />
<Compile Include="Unit\Validation\CompositeValidatorFixture.cs" />
<Compile Include="Unit\Validation\ModuleExtensionsFixture.cs" />
@@ -234,6 +234,9 @@
<EmbeddedResource Include="Resources\Views\staticviewresource.html" />
</ItemGroup>
<ItemGroup>
+ <Content Include="Resources\Assets\Styles\dotted.filename.css">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
<Content Include="Resources\Assets\Styles\css\styles.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@@ -243,6 +246,9 @@
<Content Include="Resources\Assets\Styles\styles.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
+ <Content Include="Resources\Assets\Styles\Sub.folder\styles.css">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </Content>
<Content Include="Resources\Assets\Styles\Sub\styles.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
@@ -262,4 +268,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
@@ -0,0 +1,3 @@
+body {
+ background-color: white;
+}
@@ -0,0 +1,3 @@
+body {
+ background-color: white;
+}
@@ -0,0 +1,137 @@
+namespace Nancy.Tests.Unit
+{
+ using System;
+ using System.IO;
+ using System.Security;
+ using System.Text;
+ using Nancy.Conventions;
+ using Nancy.Responses;
+ using Xunit;
+ using Xunit.Extensions;
+
+ public class StaticContentConventionBuilderFixture
+ {
+ private const string StylesheetContents = @"body {
+ background-color: white;
+}";
+
+ [Fact]
+ public void Should_retrieve_static_content_when_path_has_same_name_as_extension()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css", "styles.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_retrieve_static_content_when_virtual_directory_name_exists_in_static_route()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css", "strange-css-filename.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_retrieve_static_content_when_path_is_nested()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css/sub", "styles.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_retrieve_static_content_when_path_contains_nested_folders_with_duplicate_name()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css/css", "styles.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_retrieve_static_content_when_filename_contains_dot()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css", "dotted.filename.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_retrieve_static_content_when_path_contains_dot()
+ {
+ // Given
+ // When
+ var result = GetStaticContent("css/Sub.folder", "styles.css");
+
+ // Then
+ result.ShouldEqual(StylesheetContents);
+ }
+
+ [Fact]
+ public void Should_throw_security_exception_when_content_path_points_to_root()
+ {
+ // Given
+ var convention = StaticContentConventionBuilder.AddDirectory("/", "/");
+ var request = new Request("GET", "/face.png", "http");
+ var context = new NancyContext { Request = request };
+
+ // When
+ var exception = Record.Exception(() => convention.Invoke(context, Environment.CurrentDirectory));
+
+ // Then
+ exception.ShouldBeOfType<SecurityException>();
+ }
+
+ [Fact]
+ public void Should_throw_security_exception_when_content_path_is_null_and_requested_path_points_to_root()
+ {
+ // Given
+ var convention = StaticContentConventionBuilder.AddDirectory("/");
+ var request = new Request("GET", "/face.png", "http");
+ var context = new NancyContext { Request = request };
+
+ // When
+ var exception = Record.Exception(() => convention.Invoke(context, Environment.CurrentDirectory));
+
+ // Then
+ exception.ShouldBeOfType<SecurityException>();
+ }
+
+ private static string GetStaticContent(string virtualDirectory, string requestedFilename)
+ {
+ var resource =
+ string.Format("/{0}/{1}", virtualDirectory, requestedFilename);
+
+ var context =
+ new NancyContext { Request = new Request("GET", resource, "http") };
+
+ var resolver =
+ StaticContentConventionBuilder.AddDirectory(virtualDirectory, "Resources/Assets/Styles");
+
+ GenericFileResponse.SafePaths.Add(Environment.CurrentDirectory);
+
+ var response =
+ resolver.Invoke(context, Environment.CurrentDirectory) as GenericFileResponse;
+
+ using (var stream = new MemoryStream())
+ {
+ response.Contents(stream);
+ return Encoding.UTF8.GetString(stream.GetBuffer(), 0, (int)stream.Length);
+ }
+ }
+ }
+}
@@ -1,62 +0,0 @@
-using System;
-using System.IO;
-using System.Text;
-using FakeItEasy;
-using Nancy.Conventions;
-using Nancy.Responses;
-using Xunit;
-
-namespace Nancy.Tests.Unit
-{
- public class StaticConventBuilderFixture
- {
- private const string StylesheetContents = @"body {
- background-color: white;
-}";
-
- [Fact]
- public void Static_routes_can_have_same_name_as_extension()
- {
- getStaticContent("css", "styles.css");
- }
-
- [Fact]
- public void Virtual_directory_name_can_exist_in_static_route()
- {
- getStaticContent("css", "strange-css-filename.css");
- }
-
- [Fact]
- public void Static_content_can_be_nested()
- {
- getStaticContent("css/sub", "styles.css");
- }
-
- [Fact]
- public void Static_content_can_be_nested_with_duplicate_name()
- {
- getStaticContent("css/css", "styles.css");
- }
-
- private void getStaticContent(string virtualDirectory, string requestedFilename)
- {
- var resource = string.Format("{0}/{1}", virtualDirectory, requestedFilename);
- var nancyCtx = new NancyContext() { Request = new Request("GET", resource, "http") };
-
- var resolver = StaticContentConventionBuilder.AddDirectory("css", @"Resources\Assets\Styles");
-
- GenericFileResponse.SafePaths.Add(Environment.CurrentDirectory);
- var response = resolver.Invoke(nancyCtx, Environment.CurrentDirectory) as GenericFileResponse;
-
- Assert.NotNull(response);
- Assert.True(requestedFilename.Equals(response.Filename, StringComparison.CurrentCultureIgnoreCase));
-
- using (var ms = new MemoryStream())
- {
- response.Contents(ms);
- var css = Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
- Assert.Equal(StylesheetContents, css);
- }
- }
- }
-}
Oops, something went wrong.

0 comments on commit 23d00cf

Please sign in to comment.