Skip to content

Files

Latest commit

 

History

History

samples

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

HttpClient Interception Samples

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:

  1. An extension method that registers HttpClient for use with Refit to call the GitHub API.
  2. Dependencies for the GitHub API are registered in the Startup class.
  3. IGitHub is injected into the constructor of ReposController

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:

  1. An xunit collection fixture that self-hosts the server using WebApplicationFactory<T>.
  2. Creates a shared HttpClientInterceptorOptions instance which is registered to provide a DelegatingHandler implementation for use with Dependency Injection via a custom implementation of IHttpMessageHandlerBuilderFilter.
  3. Using JustEat.HttpClientInterception to intercept HTTP calls to the GitHub API to test the API resource.