如何在ASP.NET Core中测试https重定向? [英] How do I test an https redirect in ASP.NET Core?
问题描述
最近,我想出了单元测试来检查我的ASP.NET Core 2.1应用程序的一些重定向规则:
Recently, I came up with unit tests for checking some redirect rules of my ASP.NET Core 2.1 application:
[Fact(DisplayName = "lowercase path")]
public async Task LowercaseRedirect()
{
var result = await this.Client.GetAsync("/BLOG/");
Assert.EndsWith("/blog/", result.RequestMessage.RequestUri.PathAndQuery, StringComparison.InvariantCulture);
}
[Fact(DisplayName = "add missing slash")]
public async Task SlashRedirect()
{
var result = await this.Client.GetAsync("/blog");
Assert.EndsWith("/blog/", result.RequestMessage.RequestUri.PathAndQuery, StringComparison.InvariantCulture);
}
仅供参考:我目前正在注入 HttpClient
.
FYI: I am currently injecting the WebApplicationFactory<TEntryPoint>
into my test class, which I use to create my HttpClient
.
但是现在我很好奇如何检查https重定向是否有效.任何想法如何做到这一点?在此先感谢:)
But now I am curious how to check if the https redirect is working. Any ideas how to accomplish that? Thanks in advance :)
推荐答案
对于UseHttpsRedirection
,中间件必须具有可用的端口才能重定向到HTTPS.如果没有可用的端口,则不会重定向到HTTPS.
For UseHttpsRedirection
, a port must be available for the middleware to redirect to HTTPS. If no port is available, redirection to HTTPS does not occur.
可以通过以下任意设置指定HTTPS端口:
The HTTPS port can be specified by any of the following setting:
- HttpsRedirectionOptions.HttpsPort
- ASPNETCORE_HTTPS_PORT环境变量.
- 开发中,launchsettings.json中有一个HTTPS网址.
- 直接在Kestrel或HttpSys上配置的HTTPS URL.
要测试UseHttpsRedirection
,请指定https端口.您可以按照以下步骤操作:
To test for UseHttpsRedirection
, specify the https port. You could follow steps below:
-
使用
https_port
public class UnitTest1 : IClassFixture<WebApplicationFactory<CoreHttps.Startup>>
{
private readonly WebApplicationFactory<CoreHttps.Startup> _factory;
public UnitTest1(WebApplicationFactory<CoreHttps.Startup> factory)
{
_factory = factory.WithWebHostBuilder(builder => builder
.UseStartup<Startup>()
.UseSetting("https_port", "8080"));
}
默认情况下,请求url为http://localhost/
,如果客户端未自动重定向,请检查请求url.
For default, the request url is http://localhost/
, check the request url if client did not auto redirect.
[Theory]
[InlineData("/Home")]
public async Task HttpsRedirectionWithoutAutoRedirect(string url)
{
// Arrange
var client = _factory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
// Act
var response = await client.GetAsync(url);
// Assert
Assert.Equal(HttpStatusCode.RedirectKeepVerb, response.StatusCode);
Assert.StartsWith("http://localhost/",
response.RequestMessage.RequestUri.AbsoluteUri);
Assert.StartsWith("https://localhost:8080/",
response.Headers.Location.OriginalString);
}
如果请求是自动重定向的,请检查请求url.
check the request url if the request is auto redirect.
[Theory]
[InlineData("/Home")]
public async Task HttpsRedirectionWithAutoRedirect(string url)
{
// Arrange
var client = _factory.CreateClient();
// Act
var response = await client.GetAsync(url);
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.StartsWith("https://localhost:8080/",
response.RequestMessage.RequestUri.AbsoluteUri);
}
这篇关于如何在ASP.NET Core中测试https重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!