public HttpWwwRewriterTest()
        {
            var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();
            var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();

            _helper = new HttpWwwRewriter(loggerFactory.CreateLogger <HttpWwwRewriter>());
        }
        public Task Invoke(HttpContext context, IHttpWwwRewriter helper)
        {
            // has to locate service instead of inject in for appsettings update to be picked up in middleware automatically
            var settings = context.RequestServices.GetService <IOptionsSnapshot <AppSettings> >().Value;

            _logger.LogDebug("PreferredDomain {@PreferredDomain}", settings.PreferredDomain);
            _logger.LogDebug("UseHttps {@UseHttps}", settings.UseHttps);

            if (helper.ShouldRewrite(settings, context.Request.GetDisplayUrl(), out string url))
            {
                _logger.LogInformation("RewriteUrl: {@RewriteUrl}", url);

                context.Response.Headers[HeaderNames.Location] = url;
                context.Response.StatusCode = 301;
                context.Response.Redirect(url, permanent: true);
                return(Task.CompletedTask);
            }

            return(_next(context));
        }