Created
November 26, 2012 02:48
-
-
Save mikeobrien/4146313 to your computer and use it in GitHub Desktop.
Fubu Trailing Slash Behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TrailingSlashRedirectBehavior : IActionBehavior | |
{ | |
private readonly IActionBehavior _innerBehavior; | |
private readonly IOutputWriter _outputWriter; | |
private readonly ICurrentHttpRequest _request; | |
public TrailingSlashRedirectBehavior(IActionBehavior innerBehavior, IOutputWriter outputWriter, ICurrentHttpRequest request) | |
{ | |
_innerBehavior = innerBehavior; | |
_outputWriter = outputWriter; | |
_request = request; | |
} | |
public void Invoke() | |
{ | |
var urlParts = _request.FullUrl().Split(new []{'?'}, 2); | |
if (urlParts[0].EndsWith("/")) _innerBehavior.Invoke(); | |
else _outputWriter.RedirectToUrl(urlParts.Length == 2 ? urlParts.Join("/?") : urlParts[0] + "/"); | |
} | |
public void InvokePartial() | |
{ | |
_innerBehavior.InvokePartial(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class TrailingSlashRedirectBehaviorTests | |
{ | |
private IActionBehavior _innerBehavior; | |
private IOutputWriter _output; | |
private ICurrentHttpRequest _request; | |
[SetUp] | |
public void Setup() | |
{ | |
_innerBehavior = Substitute.For<IActionBehavior>(); | |
_output = Substitute.For<IOutputWriter>(); | |
_request = Substitute.For<ICurrentHttpRequest>(); | |
} | |
[Test] | |
public void should_not_redirect_to_url_with_trailing_slash_if_it_has_one() | |
{ | |
_request.FullUrl().Returns("https://test.net/login/"); | |
var behavior = new TrailingSlashRedirectBehavior(_innerBehavior, _output, _request); | |
behavior.Invoke(); | |
_output.DidNotReceiveWithAnyArgs().RedirectToUrl(null); | |
_innerBehavior.Received().Invoke(); | |
} | |
[Test] | |
public void should_not_redirect_to_url_with_trailing_slash_if_it_has_one_before_the_querystring() | |
{ | |
_request.FullUrl().Returns("https://test.net/login/?yada=yada"); | |
var behavior = new TrailingSlashRedirectBehavior(_innerBehavior, _output, _request); | |
behavior.Invoke(); | |
_output.DidNotReceiveWithAnyArgs().RedirectToUrl(null); | |
_innerBehavior.Received().Invoke(); | |
} | |
[Test] | |
public void should_redirect_to_url_with_trailing_slash_if_it_does_not_have_one() | |
{ | |
_request.FullUrl().Returns("https://go.dev.reachmail.net/login"); | |
var behavior = new TrailingSlashRedirectBehavior(_innerBehavior, _output, _request); | |
behavior.Invoke(); | |
_output.Received().RedirectToUrl("https://test.net/login/"); | |
_innerBehavior.DidNotReceive().Invoke(); | |
} | |
[Test] | |
public void should_redirect_to_url_with_trailing_slash_if_it_does_not_have_one_before_the_querystring() | |
{ | |
_request.FullUrl().Returns("https://go.dev.reachmail.net/login?yada=yada"); | |
var behavior = new TrailingSlashRedirectBehavior(_innerBehavior, _output, _request); | |
behavior.Invoke(); | |
_output.Received().RedirectToUrl("https://test.net/login/?yada=yada"); | |
_innerBehavior.DidNotReceive().Invoke(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment