Opinionated .NET source package for mocking HTTP client requests
dotnet add package Devlead.Testing.MockHttp
The Routes.json
file is used to configure the mock HTTP client responses. It defines the routes, HTTP methods, URIs, optional headers, and authentication requirements. Each route specifies the expected request and the corresponding response, including content type, status code, and any required authorization.
The Routes.json
file, along with other resources like Index.txt
and Secret.json
, are included as embedded resources in the project. This allows them to be accessed at runtime for simulating HTTP responses. These resources are configured in the .csproj
file under <EmbeddedResource>
tags.
[
{
"Request": {
"Methods": [
{
"Method": "GET"
}
],
"AbsoluteUri": "https://example.com/login/secret.json"
},
"Responses": [
{
"RequestHeaders": {},
"ContentResource": "Example.Login.Secret.json",
"ContentType": "application/json",
"ContentHeaders": {},
"StatusCode": 200
}
],
"Authorization": {
"Authorization": [
"Bearer AccessToken"
]
}
},
{
"Request": {
"Methods": [
{
"Method": "GET"
}
],
"AbsoluteUri": "https://example.com/index.txt"
},
"Responses": [
{
"RequestHeaders": {},
"ContentResource": "Example.Index.txt",
"ContentType": "text/plain",
"ContentHeaders": {},
"StatusCode": 200
}
]
}
]
- Index.txt: Contains plain text data used in responses.
Hello, World!
- Secret.json: Contains JSON data representing a user, used in responses.
{
"Login": "johdoe",
"FirstName": "John",
"LastName": "Doe"
}
In the .csproj
file, these resources are specified as embedded resources:
<ItemGroup>
<EmbeddedResource Include="Resources\Example\Index.txt" />
<EmbeddedResource Include="Resources\Routes.json" />
<EmbeddedResource Include="Resources\Example\Login\Secret.json" />
</ItemGroup>
These configurations enable the mock HTTP client to simulate real HTTP requests and responses, facilitating testing without actual network calls.
The mock HTTP client is registered using dependency injection in the ServiceProviderFixture.cs
file. This setup allows for simulating HTTP requests and responses in a controlled test environment.
Create a class called ServiceProviderFixture.cs
without name space and implement the partial method:
public static partial class ServiceProviderFixture
{
static partial void InitServiceProvider(IServiceCollection services)
{
services.AddSingleton<MyService>()
.AddMockHttpClient<Constants>();
}
}
The AddMockHttpClient<Constants>()
method configures the HTTP client for use in tests. Here, Constants
is a type that serves as a parent to the resources configuration, encapsulating settings and paths for the mock HTTP client to use during testing.
- HttpClientTests.cs: The mock HTTP client is used to perform HTTP GET requests, and the responses are verified.
public class HttpClientTests
{
[Test]
public async Task GetAsync()
{
var httpClient = ServiceProviderFixture.GetRequiredService<HttpClient>();
var response = await httpClient.GetAsync("https://example.com/index.txt");
await Verify(response);
}
}
- MyServiceTests.cs: The mock HTTP client is used within the
MyService
class to test various scenarios, including unauthorized and authorized access.
public class MyServiceTests
{
[Test]
public async Task GetSecret()
{
var myService = ServiceProviderFixture.GetRequiredService<MyService>(
configure => configure.ConfigureMockHttpClient<Constants>(
client => client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer",
"AccessToken"
)
)
);
var result = await myService.GetSecret();
await Verify(result);
}
}
This approach ensures that your service logic is tested in isolation, without making actual network requests, by simulating HTTP interactions using the mock HTTP client. The Constants
type helps manage the configuration of these interactions, providing a centralized way to define and access resource settings.
A real world example can be found in the Blobify project.