Skip to content

Commit

Permalink
Add HTTP/3 test to verify platform support (dotnet#46523)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Feb 10, 2023
1 parent ce510f1 commit 65c2f0d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Servers/Kestrel/Transport.Quic/test/SkipOnMarinerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Testing;

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Tests;

[AttributeUsage(AttributeTargets.Method)]
public class SkipOnMarinerAttribute : Attribute, ITestCondition
{
public SkipOnMarinerAttribute(string issueUrl = "")
{
IssueUrl = issueUrl;
}

public string IssueUrl { get; }

public bool IsMet { get; } = !IsMariner;

public string SkipReason => "Test cannot run on Mariner Linux.";

// This logic is borrowed from https://github.com/dotnet/runtime/blob/6a5a78bec9a6e14b4aa52cd5ac558f6cf5c6a211/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs
private static bool IsMariner { get; } =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && File.Exists("/etc/os-release") &&
File.ReadAllLines("/etc/os-release").Any(line =>
line.StartsWith("ID=", StringComparison.Ordinal) && line.Substring(3).Trim('"', '\'') == "mariner");
}
67 changes: 67 additions & 0 deletions src/Servers/Kestrel/Transport.Quic/test/WebHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Net;
using System.Net.Http;
using System.Net.Quic;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -19,6 +20,72 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Tests;
[Collection(nameof(NoParallelCollection))]
public class WebHostTests : LoggedTest
{
// This test isn't conditional on QuicListener.IsSupported. Instead, it verifies that HTTP/3 runs on expected platforms:
// 1. Windows 11 or later.
// 2. Linux with libmsquic package installed.
[ConditionalFact]
[SkipOnAlpine("https://github.com/dotnet/aspnetcore/issues/46537")]
[SkipOnMariner("https://github.com/dotnet/aspnetcore/issues/46537")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "HTTP/3 isn't supported on MacOS.")]
[MinimumOSVersion(OperatingSystems.Windows, WindowsVersions.Win11_21H2)]
public async Task PlatformSmoketest_HelloWorld_ClientSuccess()
{
// First check that System.Net.Quic reports that's supported.
Assert.True(QuicListener.IsSupported, "QuicListener.IsSupported should be true.");

// Next, do a basic HTTP/3 end-to-end test that ensures Kestrel can server a HTTP/3 request from a client.
//
// Arrange
using var httpEventSource = new HttpEventSourceListener(LoggerFactory);

var builder = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel(o =>
{
o.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.Protocols = Core.HttpProtocols.Http3;
});
o.ConfigureHttpsDefaults(httpsOptions =>
{
httpsOptions.ServerCertificate = TestResources.GetTestCertificate();
});
})
.UseUrls("https://127.0.0.1:0")
.Configure(app =>
{
app.Run(async context =>
{
await context.Response.WriteAsync("hello, world");
});
});
})
.ConfigureServices(AddTestLogging);

using (var host = builder.Build())
using (var client = CreateClient())
{
await host.StartAsync();

var request = new HttpRequestMessage(HttpMethod.Get, $"https://127.0.0.1:{host.GetPort()}/");
request.Version = HttpVersion.Version30;
request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;

// Act
var response = await client.SendAsync(request);

// Assert
response.EnsureSuccessStatusCode();
Assert.Equal(HttpVersion.Version30, response.Version);
var responseText = await response.Content.ReadAsStringAsync();
Assert.Equal("hello, world", responseText);

await host.StopAsync();
}
}

[ConditionalFact]
[MsQuicSupported]
public async Task UseUrls_HelloWorld_ClientSuccess()
Expand Down

0 comments on commit 65c2f0d

Please sign in to comment.