Fresp 1.0.0
Version 1.0.0 is legacy. Please, update to 2.0.0 or greater
See the version list below for details.
dotnet add package Fresp --version 1.0.0
NuGet\Install-Package Fresp -Version 1.0.0
<PackageReference Include="Fresp" Version="1.0.0" />
<PackageVersion Include="Fresp" Version="1.0.0" />
<PackageReference Include="Fresp" />
paket add Fresp --version 1.0.0
#r "nuget: Fresp, 1.0.0"
#:package Fresp@1.0.0
#addin nuget:?package=Fresp&version=1.0.0
#tool nuget:?package=Fresp&version=1.0.0
Fresp
Fresp (shorthand for fake response) is a .NET package that provides a way to mock API responses through your HttpClient during application execution. It allows you to configure both synchronous and asynchronous fake responses based on the incoming HttpRequestMessage.
Problem
In many development or UAT environments, external APIs may be unreliable, slow, or even unavailable. This can cause significant delays and issues when trying to test and develop features that depend on these APIs. For example, if an external API is down, it can block the entire development process, making it difficult to proceed with testing and development.
To address this issue, the team needs a way to bypass the call to the external API and provide a fake response instead. This allows the development and testing to continue smoothly without being dependent on the availability or reliability of the external API.
The Fresp package helps to solve this problem by allowing developers to configure fake responses for their HttpClient requests, ensuring that development and testing can proceed without interruption.
Fresp is not intended for unit testing; it is recommended for use in UAT, QA, and development environments during execution.
By default, Fresp is disabled in the production environment, so the chance of getting a fake response in production is zero! Unless your ASPNETCORE_ENVIRONMENT variable is wrong set in production server!
Installation
To install Fresp, use one of the following methods:
NuGet Package Manager Console
Install-Package Fresp
.NET CLI
dotnet add package Fresp
Usage
Adding Fake Response to your HttpClient
To make Fresp mock and return fake responses from your HttpClient, use the AddFakeResponseHandler extension method:
services.AddHttpClient("MyClient")
.AddFakeResponseHandler(options =>
{
options.Enabled = true; // Toggle fake responses for this client. It is recommended to use this in conjunction with configuration settings from appsettings.json.
});
Configuring Fake Responses
Use the method AddFakeResponse for synchronous request calls or AddFakeResponseAsync for asynchronous request calls:
- Synchronous:
services.AddHttpClient("MyClient")
.AddFakeResponseHandler(options =>
{
options.Enabled = true;
options.AddFakeResponse(request =>
{
if (request.RequestUri?.AbsolutePath == "/endpoint")
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Sync fake response")
};
}
return null;
});
});
- Asynchronous:
services.AddHttpClient("MyClient")
.AddFakeResponseHandler(options =>
{
options.Enabled = true;
options.AddFakeResponseAsync(async request =>
{
var body = await request.Content.ReadAsStringAsync();
if (body.Contains("something"))
{
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent("Async fake response")
};
}
return await Task.FromResult<HttpResponseMessage?>(null);
});
});
If the request predicate is matched, the following configured response will be returned. It's simple and lightweight!
License
This project is licensed under the MIT License. See the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.1)
- Microsoft.Extensions.Http (>= 6.0.1)
-
net7.0
- Microsoft.Extensions.Hosting.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Http (>= 7.0.0)
-
net8.0
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.1)
- Microsoft.Extensions.Http (>= 8.0.1)
-
net9.0
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Http (>= 9.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.