5

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);
}

FYI: I am currently injecting the WebApplicationFactory<TEntryPoint> into my test class, which I use to create my HttpClient.

But now I am curious how to check if the https redirect is working. Any ideas how to accomplish that? Thanks in advance :)

1
  • 2
    You should disable auto redirect feature of the httpclient and then test for Location value in the response header Commented Sep 21, 2018 at 19:36

1 Answer 1

11

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.

The HTTPS port can be specified by any of the following setting:

  • HttpsRedirectionOptions.HttpsPort
  • The ASPNETCORE_HTTPS_PORT environment variable.
  • In development, an HTTPS url in launchsettings.json.
  • An HTTPS url configured directly on Kestrel or HttpSys.

Reference:UseHttpsRedirection

To test for UseHttpsRedirection, specify the https port. You could follow steps below:

  1. Configure WebApplicationFactory with 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")); 
    }
    
  2. 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);
    }
    
  3. 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);
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to ask a question about how to use HTTPS for the integration test in-memory server; your step 1 solved my problem. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.