Meziantou.Framework.HttpClientMock
1.3.0
Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Meziantou.Framework.HttpClientMock --version 1.3.0
NuGet\Install-Package Meziantou.Framework.HttpClientMock -Version 1.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Meziantou.Framework.HttpClientMock" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Meziantou.Framework.HttpClientMock --version 1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Meziantou.Framework.HttpClientMock, 1.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Meziantou.Framework.HttpClientMock as a Cake Addin #addin nuget:?package=Meziantou.Framework.HttpClientMock&version=1.3.0 // Install Meziantou.Framework.HttpClientMock as a Cake Tool #tool nuget:?package=Meziantou.Framework.HttpClientMock&version=1.3.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Meziantou.Framework.HttpClientMock
Define the http mock
await using var mock = new HttpClientMock();
mock.MapGet("/", () => Results.Ok("test"));
mock.MapGet("/todos/{id}", (int id) => Results.Ok("test"));
mock.MapGet("/todos/?filter=abc", () => Results.Ok("test"));
mock.MapGet("https://example.com/path?search=text", () => Results.Ok("test"));
mock.MapGet("/json", () => Results.Extensions.RawJson("""{"id": 1}"""));
mock.MapGet("/xml", () => Results.Extensions.RawXml("""<root></root>"""));
// Forward the request to the upstream server
mock.MapGet("/upstream", () => Results.Extensions.ForwardToUpstream());
mock.ForwardUnknownRequestsToUpstream();
Count number of requests per endpoint
mock.MapGet("/counter", (RequestCounter counter) => counter.Get());
Register the mock in the service collection (Dependency Injection)
// Register the mock in the service collection
var services = new ServiceCollection();
services.AddHttpClient();
services.AddHttpClient<SampleClient>();
// Mock both HttpClient
services.AddHttpClientMock(builder => builder
.AddHttpClientMock(mock)
.AddHttpClientMock<SampleClient>(mock));
// Alternative way to register the mock
services.AddHttpClient<SampleClient>()
.ConfigurePrimaryHttpMessageHandler(() => mock.CreateHttpMessageHandler());
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.AspNetCore.TestHost (>= 6.0.35)
-
net8.0
- Microsoft.AspNetCore.TestHost (>= 8.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.