Skip to content

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 as a transient dependency using any registered DelegatingHandler implementations.
  2. HttpClient is registered in the StartupBase class.
  3. HttpClient 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 at http://localhost:5050.
  2. Creates a shared HttpClientInterceptorOptions instance which is registered to provide a DelegatingHandler implementation for use with Dependency Injection.
  3. Using JustEat.HttpClientInterception to intercept HTTP calls to the GitHub API to test the API resource.