Switchknife.Testing.Http 0.1.0

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

Switchknife.Testing.Http

A stub HttpMessageHandler for tests of code that takes an HttpClient. It answers each request from a function and records it, so no test reaches the network. No dependency beyond the BCL and none on a test framework: it works the same under xUnit 2, xUnit v3 or anything else.

var stub = new StubHttpHandler((request, body) => request.RequestUri!.AbsolutePath switch
{
    "/repos/o/r/pulls" => StubHttpHandler.Json("""{"number":7}""", HttpStatusCode.Created),
    _ => new HttpResponseMessage(HttpStatusCode.NotFound),
});
var client = new PullRequests(stub.Client("https://api.example.test/"));

await client.OpenAsync("o", "r", "Title");

var (method, uri, body, headers) = Assert.Single(stub.Requests);
Assert.Equal(HttpMethod.Post, method);
Assert.Equal("/repos/o/r/pulls", uri.PathAndQuery);
Assert.Equal("Bearer the-token", headers["Authorization"]);
  • new StubHttpHandler((request, body) => response): the body is read once, before the function runs, and passed to it as a string (empty when the request has none). The function may throw to simulate a dropped connection.
  • new StubHttpHandler(async (request, body, ct) => response): for responses that wait, such as a hanging upstream that only the caller's timeout or cancellation ends.
  • StubHttpHandler.Sequence(reply1, reply2, ...): the n-th request gets the n-th reply; when they run out, the last one keeps answering (a poller that sees the same state again). Assert on Requests.Count to catch extra calls.
  • Requests: every request in arrival order as RecordedRequest(Method, Uri, Body, Headers), copied when it arrived, so it is still readable after the caller disposed the request and its content. Headers holds request and content headers, case-insensitive, repeated values joined with ", ". Recording is thread-safe.
  • StubHttpHandler.Json(json, status = 200): a response with application/json; charset=utf-8 content.
  • Client(baseAddress?): an HttpClient over the stub; disposing the client leaves the stub and its records alone.

Limits:

  • Body is the content decoded as text (its charset, else UTF-8). Reading it buffers the content, so a test of a binary upload can still read request.Content as bytes inside the function.
  • No routing table: route inside the function with a switch on method and path, as above.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • 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
0.1.0 0 10/14/2026