HttpFaker.MockHttpClient 1.3.4

dotnet add package HttpFaker.MockHttpClient --version 1.3.4
                    
NuGet\Install-Package HttpFaker.MockHttpClient -Version 1.3.4
                    
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="HttpFaker.MockHttpClient" Version="1.3.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HttpFaker.MockHttpClient" Version="1.3.4" />
                    
Directory.Packages.props
<PackageReference Include="HttpFaker.MockHttpClient" />
                    
Project file
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 HttpFaker.MockHttpClient --version 1.3.4
                    
#r "nuget: HttpFaker.MockHttpClient, 1.3.4"
                    
#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.
#addin nuget:?package=HttpFaker.MockHttpClient&version=1.3.4
                    
Install HttpFaker.MockHttpClient as a Cake Addin
#tool nuget:?package=HttpFaker.MockHttpClient&version=1.3.4
                    
Install HttpFaker.MockHttpClient as a Cake Tool

Fake Http Server

It is used to respond to HttpClient requests for unit testing.

NuGet Version NuGet Downloads GitHub Actions Workflow Status GitHub License

FakeHttpServer

Overview

  • Purpose: It simulates HTTP server responses for unit testing purposes, allowing developers to test their HTTP client code without needing a real server.
  • Language: The project is written in C#.

Features

  • Middleware Setup: You can set up middleware to handle requests and responses, including authorization checks.
  • Route Mapping: It supports various HTTP methods (GET, POST, PUT, OPTIONS, TRACE, HEAD, DELETE) and routes, allowing you to define how the server should respond to different endpoints.
  • Response Handling: You can customize responses, including status codes, headers, and content.

Example Usage

Here's a brief example of how you might use FakeHttpServer:

var fakeHttp = new FakeHttpClient();

var client = fakeHttp.Setup(
    map => map.UseMiddleware((req, res, next) => 
    {
        bool exists = req.Headers.TryGetValue(HeaderNames.Authorization, out string header)

        if(exits && header.Equals("Bearer true-jwt", StringComparison.OrdinalIgnoreCase))
        {
            next();
        }
        else
        {
            res.StatusCode((int)HttpStatusCode.Unauthorized);
        }
    }),

	map => map.MapGet(@"/api/book/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})/page/(\d+)", (req, res) =>
	{
		return res.Ok(new BookPage
		{
			BookId = Guid.Parse(req.Route[2]),
			PageNum = int.Parse(req.Route[4])
		});
	})

    map => map.MapGet("/api/books", (req, res) =>
    {
        return res.Ok(new []
        {
            new{ name = "Helter Skelter", author = "Vincent Bugliosi", year = 1974 },
            new{ name = "Justice", author = "Dominic Dunne", year = 2001 },
            new{ name = "Devil's Knot", author = "Mara Leveritt", year = 2002 },
        });
    }),

    map => map.MapGet("/api/users", (req, res) =>
    {
        res.AddHeader("Set-Cookie", "SessionId=123");

        return res.Json(new 
        { 
            name = "emre" 
        });
    }),

    map => map.MapPost("/api/users", (req, res) =>
    {
        var createResult = new { UserId = 1 };

        return res.StatusCode((int)HttpStatusCode.Created, createResult);
    }),

    map => map.MapOptions("/api/users"),

    map => map.MapGet("/api/downloadFile", (req, res) =>
    {
        var file = new byte[]{ 16, 24, 35 };

        return res.File(file);
    })
);

var result = await client.GetAsync("/api/books");

string content = await  result.Content.ReadAsStringAsync();
//Response Content:
[
    { "name": "Helter Skelter", "author": "Vincent Bugliosi", "year": 1974 },
    { "name": "Justice", "author": "Dominic Dunne", "year": 2001 },
    { "name": "Devil's Knot", "author": "Mara Leveritt", "year": 2002 }
]

Summary

<details open><summary>Summary</summary>

Title Value
Generated on: 12.04.2025 - 20:38:24
Coverage date: 12.04.2025 - 20:29:22
Parser: Cobertura
Assemblies: 2
Classes: 16
Files: 15
Line coverage: 73.1% (313 of 428)
Covered lines: 313
Uncovered lines: 115
Coverable lines: 428
Total lines: 805
Branch coverage: 71.7% (56 of 78)
Covered branches: 56
Total branches: 78
Method coverage: Feature is only available for sponsors

</details>

Coverage

<details><summary>HttpFaker.Abstaction - 100%</summary>

Name Line Branch
HttpFaker.Abstaction 100% ****
HttpFaker.Abstaction.FakeHttpClientOptions 100%

</details> <details><summary>HttpFaker.MockHttpClient - 72.9%</summary>

Name Line Branch
HttpFaker.MockHttpClient 72.9% 71.7%
HttpFaker.MockHttpClient.Binding.FormBinder 100% 100%
HttpFaker.MockHttpClient.Binding.MockHttpRequestBinderFactory 100% 83.3%
HttpFaker.MockHttpClient.Binding.MultipartFormDataBinder 96.2% 100%
HttpFaker.MockHttpClient.Binding.NullBinder 100%
HttpFaker.MockHttpClient.Binding.RawBinder 100% 100%
HttpFaker.MockHttpClient.Extensions.FakeHttpClientOptionsExtensions 0% 0%
HttpFaker.MockHttpClient.FakeHttpClient 89.4%
HttpFaker.MockHttpClient.Internal.PayloadInput 100%
HttpFaker.MockHttpClient.Internal.RegexRouteMatcher 100% 100%
HttpFaker.MockHttpClient.Internal.RequestPayload 88.8% 83.3%
HttpFaker.MockHttpClient.Internal.RouteCollection 75%
HttpFaker.MockHttpClient.MockHttpMessageHandler 100% 85.7%
HttpFaker.MockHttpClient.MockHttpRequest 100%
HttpFaker.MockHttpClient.MockHttpResponse 52.4% 61.5%
HttpFaker.MockHttpClient.RequestHandler 56.1% 0%

</details>

Product 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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

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.3.4 44 a day ago
1.3.3 110 11 days ago