DalSoft.RestClient.Testing 4.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DalSoft.RestClient.Testing --version 4.3.0
NuGet\Install-Package DalSoft.RestClient.Testing -Version 4.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DalSoft.RestClient.Testing" Version="4.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DalSoft.RestClient.Testing --version 4.3.0
#r "nuget: DalSoft.RestClient.Testing, 4.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. 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.3.0

// Install DalSoft.RestClient.Testing as a Cake Tool
#tool nuget:?package=DalSoft.RestClient.Testing&version=4.3.0

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 works with .NET Core 1.0 - 2.2 WebApplicationFactory works with .NET Core 2.1 - 2.2

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 3,430 12/14/2021
4.3.0 2,780 11/25/2020
1.0.1 495 5/28/2020
1.0.0 914 8/24/2019

* Upgraded DalSoft.RestClient version. Made Version numbers match