TeePee 2.0.1

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

// Install TeePee as a Cake Tool
#tool nuget:?package=TeePee&version=2.0.1                

<img src="https://raw.githubusercontent.com/oatsoda/TeePee/main/teepee-icon.png" alt="TeePee Logo" width="64" height="64" />

TeePee

NuGet Package

A fluent API to configure HttpClients for unit testing.

CI: Build Status
Release: Build Status

Documentation

Mocking

Everything in TeePee starts by creating a TeePeeBuilder.

var teePeeBuilder = new TeePeeBuilder();

Matching requests

Add requests that you want to support by using the fluent API to specify as little or as much you want to match on:

teePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Post)
             .ThatHasBody(new { Value = 12 })
             .ThatContainsQueryParam("filter", "those")
             .ThatContainsHeader("ApiKey", "123abc-xyz987");             
Query strings

Query strings can either be included in the URL:

teePeeBuilder.ForRequest("https://some.api/path/resource?filter=those")

or by matching using the ContainsQueryParam

teePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Post)
             .ThatContainsQueryParam("filter", "those")

You cannot combine both though. Once you specify ContainingQueryParam then incoming requests at execution-time will have their query string removed when attempting to match a rule which is using ContainingQueryParam.

Returning responses

The response to a matching request is set using the Responds() fluent method:

teePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Post)
             .Responds()
             .WithStatus(HttpStatusCode.OK)
             .WithBody(new { Result = "Done" })
             .WithHeader("Set-Cookie", "Yum");
Defaults

If you don't specify a Status Code in the response, the default is 204 NoContent. (i.e. it matched, but you didn't tell it what status to return) If you don't call Responds() then the default response Status Code is 202 Accepted. (i.e. it matched, but you didn't tell it to respond)

Defaults for no matches & Strict Mode

If there is no match for a request, the default is to response with a Status Code is 404 NotFound. (This is configurable using the WithDefaultResponse on TeePeeBuilder)

Unit Testing

It's worth making sure you fully understand the various HttpClientFactory mechanisms for registering and resolving HttpClients before reading this.

Classicist/Detroit/Black Box vs Mockist/London/White Box

TeePee is focused on the Classicist/Detroit/Black Box approach to unit testing. It can be used for Mockist/London/White Box approaches, but be aware that due to the way HttpClientFactory is implemented, you may find there are limitations if you are planning to mock and inject your dependencies into your test subject manually.

Verifying

When Black Box unit testing, it's recommended to be as passive with mocked dependencies as possible. This means, where possible, not asserting specific details about calls to the HttpClient but instead mocking the requests and responses and instead asserting the outcomes of the Subject Under Test.

This isn't always possible - for example in a Fire and Forget situation where the behaviour is that the Subject Under Test is required to call an external HTTP service, but the SUT itself doesn't indicate this was done correctly.

In this case, you can set up a tracker using TrackRequest() to make simple verification of the requests that you set up.

var requestTracker = teePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Post)
                                  .TrackRequets();
                                  
// Execute SUT

reququestTracker.WasCalled(1);                                  

Injection during Unit Tests

As stated above, TeePee is more focused on the Classicist/Detroit/Black Box of testing approach and this allows unit test coverage of DI registrations for HttpClientFactory. You can of course still manually inject should you wish to.

Manual Injection

Once you have finished setting up one or more requests in you TeePeeBuilder then depending on your HttpClientFactory approach, you can create the relevant objects to inject:

Basic HttpClient

Basic HttpClient usage is very limited and is only really meant for intermediate refactoring stages. You probably won't want to use this in your production code.

var teePee = await teePeeBuilder.Build();
var httpClientFactory = teePee.Manual().CreateHttpClientFactory();
var subjectUnderTest = new UserController(httpClientFactory);
Named HttpClient

For Named HttpClient instances, you need to specify the expected Name of the instance when creating the TeePeeBuilder:

var teePeeBuilder = new TeePeeBuilder("GitHub");

// Setup request matching...

var teePee = await teePeeBuilder.Build();
var httpClientFactory = teePee.Manual().CreateHttpClientFactory();
var subjectUnderTest = new UserController(httpClientFactory);
Typed HttpClient

For Typed HttpClient instances, you need to create the HttpClient instead of the HttpClientFactory:

var teePeeBuilder = new TeePeeBuilder();

// Setup request matching...

var teePee = await teePeeBuilder.Build();
var typedHttpClient = new MyTypedHttpClient(teePee.Manual().CreateClient());
var subjectUnderTest = new UserController(typedHttpClient);
HttpClient BaseAddress

If you are wanting to specify the BaseAddress in your HttpClient and use Relative URLs in your Subject Under Test when calling the HttpClient, you can set TeePee up to ALSO configure this in your tests. (Note, this obviously means you are not covering this in your tests, it is just so that the HttpClient accepts Relative URLs.

To do this, pass a dummy Base Address into the Manual() call.

var teePeeBuilder = new TeePeeBuilder("GitHub");
teePeeBuilder.ForRequest("https://some.api/path/resource", HttpMethod.Get)
             .Responds()
             .WithStatusCode(HttpStatusCode.OK);

var teePee = await teePeeBuilder.Build();
var typedHttpClient = new MyTypedHttpClient(teePee.Manual("https://some.api").CreateClient());         
var subjectUnderTest = new UserController(typedHttpClient);

Auto Injection

Injecting automatically allows you to cover the startup DI registrations as part of your unit tests. This is mostly done using the Resolve static class.

Basic HttpClient

Basic HttpClient usage is very limited and is only really meant for intermediate refactoring stages. You probably won't want to use this in your production code.

var subjectUnderTest = await Resolve.WithDefaultClient<UserController>(teePeeBuilder);
Named HttpClient

For Named HttpClient instances, you need to specify the expected Name of the instance when creating the TeePeeBuilder:

var teePeeBuilder = new TeePeeBuilder("GitHub");

// Setup request matching...

var subjectUnderTest = await Resolve.WithNamedClients<UserController>(
                                services => 
                                {
                                   // Call your production code/extension methods here - but for this example we're inlining it - see examples for further details
                                   // Expect any intermediate dependencies to also be registered
                                   services.AddHttpClient("GitHub, c => c.BaseAddress = "https://external.api");                             
                                },
                                teePeeBuilder);
Typed HttpClient

For Typed HttpClients, your unit tests unfortunately will need to know which Type is the HttpClient (therefore exposing a bit of internal implementation detail into your tests):

var teePeeBuilder = new TeePeeBuilder();

// Setup request matching...

var subjectUnderTest = await Resolve.WithTypedClient<UserController, MyTypedHttpClient>(
                                services => 
                                {
                                   // Call your production code/extension methods here - but for this example we're inlining it - see examples for further details
                                   // Expect any intermediate dependencies to also be registered
                                   services.AddHttpClient<MyTypedHttpClient>(c => c.BaseAddress = "https://external.api");                             
                                },
                                teePeeBuilder);

Multiple HttpClient dependencies

See Examples for demonstrations of how to apply the above Manual or Auto injection when you have multiple HttpClient dependencies.

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 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.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TeePee:

Package Downloads
TeePee.Refit

Refit extensions for TeePee.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 291 8/15/2023
1.2.1 246 8/14/2023
1.1.1 284 8/9/2023
1.0.40 333 7/27/2023
1.0.39 176 7/27/2023
1.0.38 1,038 8/3/2022
1.0.37 974 8/2/2022
1.0.36 761 7/19/2022
1.0.34 779 6/15/2022
1.0.33 763 6/15/2022
1.0.32 767 6/13/2022
1.0.31 764 6/7/2022
1.0.30 1,053 5/31/2022
1.0.29 751 5/30/2022
1.0.28 1,057 5/9/2022
1.0.27 765 5/6/2022
1.0.26 767 5/6/2022
1.0.25 740 5/6/2022
1.0.24 564 8/25/2021
1.0.23 488 8/25/2021
1.0.22 755 6/28/2021
1.0.21 543 6/10/2021
1.0.18 483 10/19/2020
1.0.17 647 10/19/2020
1.0.15-alpha 360 10/9/2020