-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redirect and RedirectPermanent to HttpResponseBase/HttpResponseWr…
- Loading branch information
Showing
5 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
118 changes: 115 additions & 3 deletions
118
test/Microsoft.AspNetCore.SystemWebAdapters.Tests/HttpResponseWrapperTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,127 @@ | ||
using System; | ||
using System.Web; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.Features; | ||
using Microsoft.AspNetCore.SystemWebAdapters.Features; | ||
using Microsoft.AspNetCore.SystemWebAdapters.Internal; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Net.Http.Headers; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.SystemWebAdapters.Tests | ||
{ | ||
public class HttpServerUtilityWrapperTests | ||
public class HttpResponseWrapperTests | ||
{ | ||
[Fact] | ||
public void Constructor() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => new HttpServerUtilityWrapper(null!)); | ||
} | ||
Assert.Throws<ArgumentNullException>(() => new HttpResponseWrapper(null!)); | ||
} | ||
|
||
[InlineData("/", "~", "/", true, null)] | ||
[InlineData("/", "~", "/", true, false)] | ||
[InlineData("/", "~", "/", true, true)] | ||
[InlineData("/", "~", "/", false, null)] | ||
[InlineData("/", "~", "/", false, false)] | ||
[InlineData("/", "~", "/", false, true)] | ||
|
||
[InlineData("/", "~/dir", "/dir", true, null)] | ||
[InlineData("/", "~/dir", "/dir", true, false)] | ||
[InlineData("/", "~/dir", "/dir", true, true)] | ||
[InlineData("/", "~/dir", "/dir", false, null)] | ||
[InlineData("/", "~/dir", "/dir", false, false)] | ||
[InlineData("/", "~/dir", "/dir", false, true)] | ||
|
||
[InlineData("/", "/dir", "/dir", true, null)] | ||
[InlineData("/", "/dir", "/dir", true, false)] | ||
[InlineData("/", "/dir", "/dir", true, true)] | ||
[InlineData("/", "/dir", "/dir", false, null)] | ||
[InlineData("/", "/dir", "/dir", false, false)] | ||
[InlineData("/", "/dir", "/dir", false, true)] | ||
|
||
[InlineData("/dir1/", "/dir2", "/dir2", true, null)] | ||
[InlineData("/dir1/", "/dir2", "/dir2", true, false)] | ||
[InlineData("/dir1/", "/dir2", "/dir2", true, true)] | ||
[InlineData("/dir1/", "/dir2", "/dir2", false, null)] | ||
[InlineData("/dir1/", "/dir2", "/dir2", false, false)] | ||
[InlineData("/dir1/", "/dir2", "/dir2", false, true)] | ||
|
||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", true, null)] | ||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", true, false)] | ||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", true, true)] | ||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", false, null)] | ||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", false, false)] | ||
[InlineData("/dir1/", "~/dir2", "/dir1/dir2", false, true)] | ||
|
||
[InlineData("/dir1/", "", "/", true, null)] | ||
[InlineData("/dir1/", "", "/", true, false)] | ||
[InlineData("/dir1/", "", "/", true, true)] | ||
[InlineData("/dir1/", "", "/", false, null)] | ||
[InlineData("/dir1/", "", "/", false, false)] | ||
[InlineData("/dir1/", "", "/", false, true)] | ||
|
||
[Theory] | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1054:URI-like parameters should not be strings", Justification = "Testing")] | ||
public void Redirect(string vdir, string url, string resolved, bool permanent, bool? endResponse) | ||
{ | ||
// Arrange | ||
var isEndCalled = endResponse ?? true; | ||
|
||
var options = new SystemWebAdaptersOptions | ||
{ | ||
AppDomainAppVirtualPath = vdir, | ||
}; | ||
|
||
var services = new Mock<IServiceProvider>(); | ||
services.Setup(s => s.GetService(typeof(IOptions<SystemWebAdaptersOptions>))).Returns(Options.Create(options)); | ||
|
||
var endFeature = new Mock<IHttpResponseEndFeature>(); | ||
endFeature.SetupAllProperties(); | ||
|
||
var context = new DefaultHttpContext(); | ||
context.Features.Set(endFeature.Object); | ||
context.Features.Set(new Mock<IHttpResponseContentFeature>().Object); | ||
context.RequestServices = services.Object; | ||
|
||
|
||
// Assemble: The HttpResponse and HttpResponseWrapper | ||
HttpResponse response = new HttpResponse(context.Response); | ||
HttpResponseBase responseBase = new HttpResponseWrapper(response); | ||
|
||
// Act: On the HttpResponseBase | ||
if (endResponse.HasValue) | ||
{ | ||
if (permanent) | ||
{ | ||
responseBase.RedirectPermanent(url, endResponse.Value); | ||
} | ||
else | ||
{ | ||
responseBase.Redirect(url, endResponse.Value); | ||
} | ||
} | ||
else | ||
{ | ||
if (permanent) | ||
{ | ||
responseBase.RedirectPermanent(url); | ||
} | ||
else | ||
{ | ||
responseBase.Redirect(url); | ||
} | ||
} | ||
|
||
// Assert: On the inner HttpResponse | ||
Assert.Equal(resolved, response.RedirectLocation); | ||
Assert.Null(context.Features.GetRequired<IHttpResponseFeature>().ReasonPhrase); | ||
Assert.Equal(2, context.Response.Headers.Count); | ||
Assert.Equal(resolved, context.Response.Headers.Location); | ||
Assert.Equal("text/html", context.Response.Headers.ContentType); | ||
Assert.Equal(permanent ? 301 : 302, context.Response.StatusCode); | ||
|
||
endFeature.Verify(b => b.EndAsync(), isEndCalled ? Times.Once : Times.Never); | ||
} | ||
} | ||
} |
This file contains 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