-
Notifications
You must be signed in to change notification settings - Fork 844
AspNetCore.Testing readme, namespaces #4561
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
joperezr
merged 4 commits into
dotnet:release/8.0
from
Tratcher:tratcher/readme_asptesting
Oct 20, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,50 @@ | ||
| README | ||
| # Microsoft.AspNetCore.Testing | ||
|
|
||
| This package provides test fakes for integration testing of ASP.NET Core applications. | ||
|
|
||
| In particular: | ||
|
|
||
| - `IWebHostBuilder` extensions to setup the test web app. | ||
| - `IHost` extensions to access that test web app. | ||
|
|
||
| ## Install the package | ||
|
|
||
| From the command-line: | ||
|
|
||
| ```dotnetcli | ||
| dotnet add package Microsoft.AspNetCore.Testing | ||
| ``` | ||
|
|
||
| Or directly in the C# project file: | ||
|
|
||
| ```xml | ||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Testing" Version="[CURRENTVERSION]" /> | ||
| </ItemGroup> | ||
| ``` | ||
|
|
||
| ## Usage Example | ||
|
|
||
| ### Creating a Test Web App | ||
|
|
||
| The [`IWebHostBuilder`](https://learn.microsoft.com/dotnet/api/microsoft.aspnetcore.hosting.iwebhostbuilder) extensions can help set up a host for testing. | ||
|
|
||
| ```csharp | ||
| using var host = await FakeHost.CreateBuilder() | ||
| .ConfigureWebHost(webHost => webHost.UseFakeStartup().ListenHttpOnAnyPort()) | ||
| .StartAsync(); | ||
| ``` | ||
|
|
||
| ### Accessing the test Web App | ||
|
|
||
| The [`IHost`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.hosting.ihost) extensions can help access the test host that was created above. | ||
|
|
||
| ```csharp | ||
| using var client = host.CreateClient(); | ||
|
|
||
| var response = await client.GetAsync("/"); | ||
| ``` | ||
|
|
||
| ## Feedback & Contributing | ||
|
|
||
| We welcome feedback and contributions in [our GitHub repo](https://github.com/dotnet/extensions). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Libraries/Microsoft.AspNetCore.Testing/ServiceFakesWebHostExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Net; | ||
| using System.Security.Cryptography.X509Certificates; | ||
| using Microsoft.AspNetCore.Testing; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Shared.Diagnostics; | ||
|
|
||
| namespace Microsoft.AspNetCore.Hosting; | ||
|
|
||
| /// <summary> | ||
| /// Extension methods supporting Kestrel server unit testing scenarios. | ||
| /// </summary> | ||
| public static class ServiceFakesWebHostExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds an empty Startup class to satisfy ASP.NET check. | ||
| /// </summary> | ||
| /// <param name="builder">An <see cref="IWebHostBuilder"/> instance.</param> | ||
| /// <returns>The value of <paramref name="builder"/>.</returns> | ||
| public static IWebHostBuilder UseFakeStartup(this IWebHostBuilder builder) | ||
| { | ||
| return builder.UseStartup<FakeStartup>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds Kestrel server instance listening on the given HTTP port. | ||
| /// </summary> | ||
| /// <param name="builder">An <see cref="IWebHostBuilder"/> instance.</param> | ||
| /// <returns>The value of <paramref name="builder"/>.</returns> | ||
| /// <remarks>When a concrete port is set by caller, it's not further validated if the port is really free.</remarks> | ||
| public static IWebHostBuilder ListenHttpOnAnyPort(this IWebHostBuilder builder) | ||
| => Throw.IfNull(builder) | ||
| .UseKestrel(options => options.Listen(new IPEndPoint(IPAddress.Loopback, 0))); | ||
|
|
||
| /// <summary> | ||
| /// Adds Kestrel server instance listening on a random HTTPS port. | ||
| /// </summary> | ||
| /// <param name="builder">An <see cref="IWebHostBuilder"/> instance.</param> | ||
| /// <param name="sslCertificate">An SSL certificate for the port. If null, a self-signed certificate is created and used.</param> | ||
| /// <returns>The value of <paramref name="builder"/>.</returns> | ||
| /// <remarks>When a concrete port is set by caller, it's not further validated if the port is really free.</remarks> | ||
| [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Dispose objects before losing scope")] | ||
| public static IWebHostBuilder ListenHttpsOnAnyPort(this IWebHostBuilder builder, X509Certificate2? sslCertificate = null) | ||
| { | ||
| sslCertificate ??= FakeSslCertificateFactory.CreateSslCertificate(); | ||
|
|
||
| return builder | ||
| .UseKestrel(options => | ||
| { | ||
| options.Listen(new IPEndPoint(IPAddress.Loopback, 0), listenOptions => | ||
| { | ||
| _ = listenOptions.UseHttps(sslCertificate); | ||
| }); | ||
| }) | ||
| .ConfigureServices(services => | ||
| services.Configure<FakeCertificateOptions>(options => | ||
| options.Certificate = sslCertificate)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.