Skip to content

Move HttpApplication middleware into UseSystemWebAdapters #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public static ISystemWebAdapterBuilder AddHttpApplication(this ISystemWebAdapter
return builder;
}

public static ISystemWebAdapterBuilder AddHttpApplication<TApp>(this ISystemWebAdapterBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);

builder.AddHttpApplication(options =>
{
options.ApplicationType = typeof(TApp);
});

return builder;
}

public static ISystemWebAdapterBuilder AddHttpApplication<TApp>(this ISystemWebAdapterBuilder builder, Action<HttpApplicationOptions> configure)
where TApp : HttpApplication
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ internal static void UseSystemWebAdapterFeatures(this IApplicationBuilder app)
return;
}

app.UseMiddleware<RegisterAdapterFeaturesMiddleware>();

if (app.AreHttpApplicationEventsRequired())
{
app.UseMiddleware<HttpApplicationMiddleware>();
}

app.UseMiddleware<PreBufferRequestStreamMiddleware>();
app.UseMiddleware<BufferResponseStreamMiddleware>();
app.UseMiddleware<SetDefaultResponseHeadersMiddleware>();
app.UseMiddleware<SingleThreadedRequestMiddleware>();
app.UseMiddleware<CurrentPrincipalMiddleware>();

if (app.AreHttpApplicationEventsRequired())
{
app.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}
}

public static void UseSystemWebAdapters(this IApplicationBuilder app)
Expand Down Expand Up @@ -132,13 +144,6 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.UseMiddleware<SetHttpContextTimestampMiddleware>();
builder.UseMiddleware<RegisterAdapterFeaturesMiddleware>();

if (builder.AreHttpApplicationEventsRequired())
{
builder.UseMiddleware<HttpApplicationMiddleware>();
builder.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}

next(builder);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extras.Moq" Version="6.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET7_0_OR_GREATER

using System;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests;

[Collection(nameof(SelfHostedTests))]
public class OutputCachingTests
{
[Fact]
public async Task OutputCachingWorksWithHttpApplication()
{
// Arrange
using var host = await new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer(options =>
{
options.AllowSynchronousIO = true;
})
.ConfigureServices(services =>
{
services.AddRouting();
services.AddSystemWebAdapters()
.AddHttpApplication<MyApp>();
services.AddOutputCache();
})
.Configure(app =>
{
app.UseRouting();
app.UseSystemWebAdapters();
app.UseOutputCache();
app.UseEndpoints(endpoints =>
{
var count = 0;
endpoints.Map("/", () => Results.Ok(count++))
.CacheOutput(builder =>
{
builder.AddHttpApplicationVaryByCustom(MyApp.VaryBy);
});
});
});
})
.StartAsync();

using var client = host.GetTestClient();

// Act
var result1 = await GetResponseAsync(client, "test");
var result2 = await GetResponseAsync(client, "other");
var result3 = await GetResponseAsync(client, "test");

// Assert
Assert.Equal("0", result1);
Assert.Equal("1", result2);
Assert.Equal("0", result3);

static async Task<string> GetResponseAsync(HttpClient client, string value)
{
using var message = new HttpRequestMessage(HttpMethod.Get, new Uri("/", UriKind.Relative))
{
Headers =
{
{ MyApp.Header, value }
}
};

using var response = await client.SendAsync(message);

return await response.Content.ReadAsStringAsync();
}
}

private sealed class MyApp : HttpApplication
{
public const string VaryBy = "myValue";
public const string Header = "X-TEST-HEADER";

public override string? GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == VaryBy)
{
return context.Request.Headers[Header];
}

return base.GetVaryByCustomString(context, custom);
}
}
}
#endif