DalSoft.RestClient.Testing 4.4.1

Install-Package DalSoft.RestClient.Testing -Version 4.4.1
dotnet add package DalSoft.RestClient.Testing --version 4.4.1
<PackageReference Include="DalSoft.RestClient.Testing" Version="4.4.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DalSoft.RestClient.Testing --version 4.4.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DalSoft.RestClient.Testing, 4.4.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DalSoft C# RestClient Testing

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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.4.1 73 12/14/2021
4.3.0 1,369 11/25/2020
1.0.1 277 5/28/2020
1.0.0 690 8/24/2019

* Upgraded DalSoft.RestClient version.
     * Updated to target net5.0
     * Fixed bug where RestClient handlers where not being passed to the Test Server Rest Client