HttpParrot 1.0.0

dotnet add package HttpParrot --version 1.0.0
NuGet\Install-Package HttpParrot -Version 1.0.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="HttpParrot" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpParrot --version 1.0.0
#r "nuget: HttpParrot, 1.0.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 HttpParrot as a Cake Addin
#addin nuget:?package=HttpParrot&version=1.0.0

// Install HttpParrot as a Cake Tool
#tool nuget:?package=HttpParrot&version=1.0.0

HTTP Parrot ("Record and Replay utilities")

Contains tools for recording HTTP responses when using HttpClient that can later be used to "replay" the communication in tests, instead of having to create complicated mocks for the external dependencies. That is, let the recorded HTTP responses be your mock data, that can also be changed if needed.

Included components

RecordAndReplayEnabledMessageHandler

A System.Net.Http.DelegatingHandler implementation that can record HTTP-responses and replay them at a later time. Example test setup for ASP.NET Core application using a IHttpClientFactory "typed client":

services.AddHttpClient<ISomeApiClient, SomeApiClient>()
    .AddHttpMessageHandler(serviceProvider =>
    {
        // The below path to the cache directory is normally what's needed to put the cache in the project folder and not in the build output.
        return new RecordAndReplayEnabledMessageHandler(new RecordAndRelayOptions
        {
            Mode = RecordAndReplayMode.RecordAndReplay,
            RelativeCacheDirectoryPath = @"..\..\..\RecordReplayCache",
            IdentityProvider = identityProvider // Custom implementation of IRecordAndReplayIdentityProvider, if needed
        });
    });

If you want to add record & replay handlers too all clients generated from the IHttpClientFactory you can instead use RecordAndReplayEnabledMessageHandlerExtensions.AddRecordAndReplayEnabledMessageHandlerToDefaultHttpClientFactory as follows:

// Adds HttpParrot "record and replay" handler to all HttpClient instances generated from the default http client factory
services.AddRecordAndReplayEnabledMessageHandlerToDefaultHttpClientFactory(new RecordAndRelayOptions
{
    Mode = RecordAndReplayMode.RecordAndReplay,
    RelativeCacheDirectoryPath = @"..\..\..\RecordReplayCache"
});

The behavior of the RecordAndReplayEnabledMessageHandler is controlled by the RecordAndReplayMode enum.

public enum RecordAndReplayMode
{
    /// <summary>
    /// Do not record or replay. Always do the actual call.
    /// </summary>
    Passthrough,

    /// <summary>
    /// Only replay, never do actual call even if no recorded response exists.
    /// </summary>
    ReplayOnly,

    /// <summary>
    /// Always do the actual call and record the response, overwriting any matching recorded response.
    /// </summary>
    RecordOnly,

    /// <summary>
    /// Replay if matching data exists, otherwise do actual call and record the response.
    /// </summary>
    RecordAndReplay
}

The request body, query parameters and user identity can also be used to identify a specific request by setting the corresponding properties in the RecordAndRelayOptions like so:

new RecordAndReplayEnabledMessageHandler(new RecordAndRelayOptions
    {
        ...
        IncludeQueryParametersWhenMatchingResponse = true,
        IncludeBodyWhenMatchingResponse = true
        IdentityProvider = new CustomIdentityProvider() // Implementation of IRecordAndReplayIdentityProvider
    });

The identity provider is only needed when the user identity is relevant for the calls, and not already part of the request body or query parameters.

Note that sometimes requests might contain a unique correlation id or similar (in the body, not as a header). In these cases the requests will not be determined identical, so steps has to be taken in the test setup to pin the correlation id to a specific value.

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. 
.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.0.0 591 11/14/2022