RedirectToHttps uses status code parameter

This commit is contained in:
Mikael Mengistu 2016-11-07 14:16:53 -08:00 коммит произвёл GitHub
Родитель 5d6fcb07da
Коммит 40da77eb17
2 изменённых файлов: 47 добавлений и 5 удалений

Просмотреть файл

@ -4,6 +4,7 @@
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Rewrite.Logging;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Rewrite.Internal
{
@ -29,9 +30,10 @@ namespace Microsoft.AspNetCore.Rewrite.Internal
}
var req = context.HttpContext.Request;
var newUrl = new StringBuilder().Append("https://").Append(host).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
context.HttpContext.Response.Redirect(newUrl.ToString());
var response = context.HttpContext.Response;
response.StatusCode = StatusCode;
response.Headers[HeaderNames.Location] = newUrl.ToString();
context.Result = RuleResult.EndResponse;
context.Logger?.RedirectedToHttps();
}

Просмотреть файл

@ -51,10 +51,14 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules
Assert.Equal("http://example.com/foo", response.Headers.Location.OriginalString);
}
[Fact]
public async Task CheckRedirectToHttps()
[Theory]
[InlineData(StatusCodes.Status301MovedPermanently)]
[InlineData(StatusCodes.Status302Found)]
[InlineData(StatusCodes.Status307TemporaryRedirect)]
[InlineData(StatusCodes.Status308PermanentRedirect)]
public async Task CheckRedirectToHttps(int statusCode)
{
var options = new RewriteOptions().AddRedirectToHttps(statusCode: StatusCodes.Status301MovedPermanently);
var options = new RewriteOptions().AddRedirectToHttps(statusCode: statusCode);
var builder = new WebHostBuilder()
.Configure(app =>
{
@ -65,8 +69,44 @@ namespace Microsoft.AspNetCore.Rewrite.Tests.CodeRules
var response = await server.CreateClient().GetAsync(new Uri("http://example.com"));
Assert.Equal("https://example.com/", response.Headers.Location.OriginalString);
Assert.Equal(statusCode, (int)response.StatusCode);
}
[Fact]
public async Task CheckPermanentRedirectToHttps()
{
var options = new RewriteOptions().AddRedirectToHttpsPermanent();
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseRewriter(options);
});
var server = new TestServer(builder);
var response = await server.CreateClient().GetAsync(new Uri("http://example.com"));
Assert.Equal("https://example.com/", response.Headers.Location.OriginalString);
Assert.Equal(StatusCodes.Status301MovedPermanently, (int)response.StatusCode);
}
[Theory]
[InlineData(25, "https://example.com:25/")]
[InlineData(-25, "https://example.com/")]
public async Task CheckRedirectToHttpsWithSslPort(int sslPort,string expected)
{
var options = new RewriteOptions().AddRedirectToHttps(statusCode: StatusCodes.Status301MovedPermanently, sslPort:sslPort);
var builder = new WebHostBuilder()
.Configure(app =>
{
app.UseRewriter(options);
});
var server = new TestServer(builder);
var response = await server.CreateClient().GetAsync(new Uri("http://example.com"));
Assert.Equal(expected, response.Headers.Location.OriginalString);
Assert.Equal(StatusCodes.Status301MovedPermanently, (int)response.StatusCode);
}
[Fact]
public async Task CheckIfEmptyStringRedirectCorrectly()