Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #371 from grumpydev/AsRedirectPathExpansion
Browse files Browse the repository at this point in the history
Tweaked AsRedirect to expand the base path with ~
  • Loading branch information
grumpydev committed Oct 26, 2011
2 parents 941165d + 1b4d25e commit e2c0381
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Nancy.Tests/Nancy.Tests.csproj
Expand Up @@ -112,6 +112,7 @@
<Compile Include="Unit\Conventions\DefaultStaticContentsConventionsFixture.cs" />
<Compile Include="Unit\DefaultResponseFormatterFactoryFixture.cs" />
<Compile Include="Unit\ErrorHandling\DefaultErrorHandlerFixture.cs" />
<Compile Include="Unit\FormatterExtensionsFixture.cs" />
<Compile Include="Unit\MimeTypesFixture.cs" />
<Compile Include="Unit\NamedPipelineBaseFixture.cs" />
<Compile Include="Unit\ModuleNameFixture.cs" />
Expand Down
51 changes: 51 additions & 0 deletions src/Nancy.Tests/Unit/FormatterExtensionsFixture.cs
@@ -0,0 +1,51 @@
namespace Nancy.Tests.Unit
{
using FakeItEasy;
using Xunit;

public class FormatterExtensionsFixture
{
private readonly IResponseFormatter formatter;
private readonly NancyContext context;

public FormatterExtensionsFixture()
{
this.context = new NancyContext();
this.formatter = new DefaultResponseFormatter(A.Fake<IRootPathProvider>(), context);
}

[Fact]
public void Should_expand_base_path_for_redirect_if_tilde_present()
{
this.context.Request = new Request(
"GET",
new Url
{
BasePath = "/basePath",
Path = "Path",
Scheme = "http",
});

var result = this.formatter.AsRedirect("~/test");

result.Headers["Location"].ShouldEqual("/basePath/test");
}

[Fact]
public void Should_leave_path_untouched_for_redirect_if_no_tilde()
{
this.context.Request = new Request(
"GET",
new Url
{
BasePath = "/basePath",
Path = "Path",
Scheme = "http",
});

var result = this.formatter.AsRedirect("/test");

result.Headers["Location"].ShouldEqual("/test");
}
}
}
5 changes: 3 additions & 2 deletions src/Nancy/FormatterExtensions.cs
@@ -1,6 +1,7 @@
namespace Nancy
{
using System;
using Extensions;
using Nancy.Responses;
using System.IO;

Expand Down Expand Up @@ -36,9 +37,9 @@ public static Response AsJson<TModel>(this IResponseFormatter formatter, TModel
return new JsonResponse<TModel>(model);
}

public static Response AsRedirect(this IResponseFormatter response, string location)
public static Response AsRedirect(this IResponseFormatter formatter, string location)
{
return new RedirectResponse(location);
return new RedirectResponse(formatter.Context.ToFullPath(location));
}

public static Response AsXml<TModel>(this IResponseFormatter formatter, TModel model)
Expand Down

0 comments on commit e2c0381

Please sign in to comment.