HttpClient Interception Samples
December 3, 2025 ยท View on GitHub
Introduction
This folder contains a sample ASP.NET Core application that uses the JustEat.HttpClientInterception NuGet package for testing its dependencies on other APIs.
Application
The application (/samples/SampleApp) exposes a single HTTP resource at GET api/repos.
This resource consumes the GitHub v3 API and returns the names of the public repositories belonging to the configured user/organisation as a JSON array of strings. For example:
[
"JustBehave",
"JustSaying",
"JustEat.RecruitmentTest"
]
To enable use of JustEat.HttpClientInterception for testing, the application has a small number of minor changes:
- An extension method that registers
HttpClientfor use with Refit to call the GitHub API. - Dependencies for the GitHub API are registered in the
Startupclass. IGitHubis injected into the constructor ofReposController
Tests
The tests (/samples/SampleApp.Tests) self-host the application using Kestrel so that the application can be tested in a black-box manner by performing HTTP requests against the API's GET resource.
This is enabled by:
- An xunit collection fixture that self-hosts the server using
WebApplicationFactory<T>. - Creates a shared
HttpClientInterceptorOptionsinstance which is registered to provide aDelegatingHandlerimplementation for use with Dependency Injection via a custom implementation ofIHttpMessageHandlerBuilderFilter. - Using
JustEat.HttpClientInterceptionto intercept HTTP calls to the GitHub API to test the API resource.