Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
Force redirect www (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminsampica authored Aug 20, 2022
1 parent ac9e1b4 commit 00f7b3f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Context;
using Serilog.Events;
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Reflection;
using System.Security.Claims;

Expand Down Expand Up @@ -136,13 +138,14 @@
});
}

app.UseHttpsRedirection();
app.UseRewriter(new RewriteOptions()
.AddRedirectToWww()
.AddRedirectToHttps((int)HttpStatusCode.TemporaryRedirect));

app.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();
app.UseCors();

app.UseIdentityServer();
app.UseAuthentication();
Expand Down
38 changes: 38 additions & 0 deletions src/Tests/RedirectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.AspNetCore.Mvc.Testing;

namespace DynamoLeagueBlazor.Tests;

public class RedirectTests
{
[Fact]
public async Task GivenAnyRequest_WhenIsHttp_ThenIsRedirectedToHttps()
{
var webApplicationFactory = new WebApplicationFactory<Program>();

var client = webApplicationFactory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false,
BaseAddress = new Uri("http://www.test.com")
});

var response = await client.GetAsync(string.Empty);

response.Headers.Location!.Scheme.Should().Be("https");
}

[Fact]
public async Task GivenAnyRequest_WhenIsNotWww_ThenIsRedirectedToWww()
{
var webApplicationFactory = new WebApplicationFactory<Program>();

var client = webApplicationFactory.CreateClient(new WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false,
BaseAddress = new Uri("https://test.com")
});

var response = await client.GetAsync(string.Empty);

response.Headers.Location!.AbsoluteUri.Should().Contain("www");
}
}

0 comments on commit 00f7b3f

Please sign in to comment.