DalSoft.RestClient.Testing
4.4.1
dotnet add package DalSoft.RestClient.Testing --version 4.4.1
NuGet\Install-Package DalSoft.RestClient.Testing -Version 4.4.1
<PackageReference Include="DalSoft.RestClient.Testing" Version="4.4.1" />
paket add DalSoft.RestClient.Testing --version 4.4.1
#r "nuget: DalSoft.RestClient.Testing, 4.4.1"
// Install DalSoft.RestClient.Testing as a Cake Addin #addin nuget:?package=DalSoft.RestClient.Testing&version=4.4.1 // Install DalSoft.RestClient.Testing as a Cake Tool #tool nuget:?package=DalSoft.RestClient.Testing&version=4.4.1
DalSoft C# RestClient Testing
- Extends DalSoft RestClient to Make testing Rest API's or anything HTTP trivial.
- Use DalSoft RestClient with ASP.NET Core In-Memory Test Server for integration tests.
Getting Started
Install via .NET CLI
> dotnet add package DalSoft.RestClient.Testing
Install via NuGet
PM> Install-Package DalSoft.RestClient.Testing
Using the Verify extension method to fluently test anything HTTP.
Just pass an type and expression returning a boolean to Verify.
[Fact]
public async Task GetUser_ProvidingAValidUserId_ReturnsExpectedResponse()
{
var client = new RestClient("https://jsonplaceholder.typicode.com/");
await client.Resource("users/1").Get()
.Verify<HttpResponseMessage>(response => response.IsSuccessStatusCode) // Verify using HttpResponseMessage
.Verify<string>(s => s.Contains("Leanne Graham")) // Verify string response body
.Verify<User>(user => user.Username == "Bret") // Verify by casting to your model
.Verify(o => o.username == "Bret") // Verify dynamically
.Verify(o => o.HttpResponseMessage.IsSuccessStatusCode); // Verify dynamically
}
If you change the test to fail for example change "Leanne Graham" to "XXX Leanne Graham" the test will fail.
A Test will fail on the first Verify failure, and won't carry on Verifying inline with how your would expect Assert to work.
When Verify fails it throws a meaningful exception which can be read in the test output, for example in the test above:
s => s.Contains("XXX Leanne Graham") was not verified
Integration Testing using ASP.NET Core In-Memory Test Server
If you haven't used ASP.NET Core's In-Memory Test Server for integration testing, it's worth heading over to ASP.NET Core Testing documentation.
DalSoft RestClient Testing extends both TestServer and WebApplicationFactory, you just call the CreateRestClient
extension method of instead of CreateClient
Using TestServer:
public class TestServerTests
{
[Fact]
public async Task TestServer_VerifyingResponseUsingCreateRestClient_ShouldVerifyResponseAsExpected()
{
var builder = new WebHostBuilder()
.UseStartup<Startup>(); // just an example for real use a Fixture
var testServer = new TestServer(builder);
var client = testServer.CreateRestClient();
await client.Resource("examples/createclient")
.Get()
.Verify<HttpResponseMessage>(response => response.IsSuccessStatusCode)
.Verify<List<Repository>>(r => r.FirstOrDefault().name != null);
}
}
Using WebApplicationFactory:
public class WebApplicationFactoryTests : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;
public WebApplicationFactoryTests(WebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task TestServer_VerifyingResponseUsingCreateRestClient_ShouldVerifyResponseAsExpected()
{
var client = _factory.WithWebHostBuilder(builder =>
{
builder.ConfigureServices(services =>
{
services.AddSingleton<IRestClientFactory>(provider => new MockRestClientFactory()); // Return Mock Response
});
}).CreateRestClient(new Config());
var result = await client
.Resource("examples/createclient")
.Get()
.Verify<HttpResponseMessage>(response => response.IsSuccessStatusCode)
.Verify<List<Repository>>(repositories => repositories.FirstOrDefault().name == "Hello World"); // Test Mock was used
}
}
public class MockRestClientFactory : IRestClientFactory
{
public RestClient CreateClient()
{
return new RestClient
(
"http://NotUsedAsMockResponseReturned",
new Config()
.UseUnitTestHandler(request => new HttpResponseMessage()
{
Content = new StringContent("[{ \"name\": \"Hello World\" }]")
})
);
}
public RestClient CreateClient(string name)
{
return CreateClient();
}
}
Supported Platforms
RestClient targets .NET Standard 2.0 therefore supports Windows, Linux, Mac and Xamarin (iOS, Android and UWP).
TestServer and WebApplicationFactory works with .NET Core 3.1 - .NET 6.0
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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. |
-
net5.0
- DalSoft.RestClient (>= 4.4.1)
- Microsoft.AspNetCore.Mvc.Testing (>= 3.1.21)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
* Upgraded DalSoft.RestClient version.
* Updated to target net5.0
* Fixed bug where RestClient handlers where not being passed to the Test Server Rest Client