HttpClientMoq 1.0.1
dotnet add package HttpClientMoq --version 1.0.1
NuGet\Install-Package HttpClientMoq -Version 1.0.1
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="HttpClientMoq" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpClientMoq" Version="1.0.1" />
<PackageReference Include="HttpClientMoq" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HttpClientMoq --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HttpClientMoq, 1.0.1"
#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.
#:package HttpClientMoq@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HttpClientMoq&version=1.0.1
#tool nuget:?package=HttpClientMoq&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HttpClientHandlerMoq
Package to mock HttpClient for .NET
Example of mocking http get request:
// Set test url
var url = "http://url/user/1";
// Create httpClientHandler for url with text response data.
var httpClientHandlerMocked = new HttpClientHandlerMoqBuilder(url, "response data").Build();
// Create http client by handler.
var httpClient = new HttpClient(httpClientHandlerMocked);
// Send get request.
var actualHttpResponseMessage = await httpClient.GetAsync(url);
// Read response content as string.
var actualContent = await actualHttpResponseMessage.Content.ReadAsStringAsync();
// Assertion using Shoudly:
// HttpClientHandlerStatistics contains http request and response statistics
// SentContentBody is collection of request bodies
// VisitedUrls is collection of visited urls
_ = httpClientHandlerMocked.HttpClientHandlerStatistics.SentContentBody.Should().BeEmpty();
_ = httpClientHandlerMocked.HttpClientHandlerStatistics.VisitedUrls.Should().HaveCount(1);
_ = httpClientHandlerMocked.HttpClientHandlerStatistics.VisitedUrls!.Single().Should().Be("http://url/user/1");
_ = actualHttpResponseMessage.StatusCode.Should().Be(HttpStatusCode.OK);
_ = actualContent.Should().Be("response data");
It's possible to create and send multiple requests. Example xUnit test of mocking get and post requests:
[Fact]
public async Task GetAsync_SourceDataContainsMultipleRequests_ShouldBeAsExpected()
{
// Arrange
var url1 = "http://url/user/1";
var jsonInputContent = @"
{
""Name"" : ""John"",
""Surname"": ""Doe""
}";
var url2 = "http://url/user/save";
var httpClientHandlerMoq = new HttpClientHandlerMoqBuilder(url1, "response data 1")
.SetupRequest(
httpMethod: HttpMethod.Post,
url: url2,
body: jsonInputContent,
httpStatusCode: HttpStatusCode.Created,
responseStringData: "{{ \"Id\" : 1 }}")
.Build();
var httpClient = new HttpClient(httpClientHandlerMoq);
var expectedVisitedUrls = new List<string>
{
url1,
url2,
};
var expectedBody = new List<string>()
{
jsonInputContent
};
var expectedHttpResponseMessageStatusCode1 = HttpStatusCode.OK;
var expectedContent1 = "response data 1";
var expectedHttpResponseMessageStatusCode2 = HttpStatusCode.Created;
var expectedContent2 = "{{ \"Id\" : 1 }}";
// Act
var actualHttpResponseMessage1 = await httpClient.GetAsync(url1);
var actualContent1 = await actualHttpResponseMessage1.Content.ReadAsStringAsync();
var actualHttpResponseMessage2 = await httpClient.PostAsync(url2, new StringContent(jsonInputContent));
var actualContent2 = await actualHttpResponseMessage2.Content.ReadAsStringAsync();
// Assert
_ = httpClientHandlerMoq.HttpClientHandlerStatistics.SentContentBody.Should().BeEquivalentTo(expectedBody);
_ = httpClientHandlerMoq.HttpClientHandlerStatistics.VisitedUrls.Should().BeEquivalentTo(expectedVisitedUrls);
_ = actualHttpResponseMessage1.StatusCode.Should().Be(expectedHttpResponseMessageStatusCode1);
_ = actualContent1.Should().Be(expectedContent1);
_ = actualHttpResponseMessage2.StatusCode.Should().Be(expectedHttpResponseMessageStatusCode2);
_ = actualContent2.Should().Be(expectedContent2);
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.1 | 405 | 6/26/2022 |