Dubstep.TestUtilities.TestServer 0.0.4

dotnet add package Dubstep.TestUtilities.TestServer --version 0.0.4
NuGet\Install-Package Dubstep.TestUtilities.TestServer -Version 0.0.4
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="Dubstep.TestUtilities.TestServer" Version="0.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dubstep.TestUtilities.TestServer --version 0.0.4
#r "nuget: Dubstep.TestUtilities.TestServer, 0.0.4"
#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 Dubstep.TestUtilities.TestServer as a Cake Addin
#addin nuget:?package=Dubstep.TestUtilities.TestServer&version=0.0.4

// Install Dubstep.TestUtilities.TestServer as a Cake Tool
#tool nuget:?package=Dubstep.TestUtilities.TestServer&version=0.0.4

TestServer

TestServer is designed for unit test. It generates an HttpClient instance that you can inject to your service.

Get Started

  1. Install the package by nuget
    dotnet add package Dubstep.TestUtilities.TestServer
    
  2. Create a TestServer instance by new TestServer()
  3. Configure RuleSet of the TestServer instance
  4. Generate a HttpClient by CreateClient()

Example

// The default vaule when not match found is a HttpNotFound response
// It can be changed by SetDefaultAction
server.CurrentRuleSet
    .SetDefaultAction(async (response) =>
    {
        response.StatusCode = 500;
        await response.WriteAsync("Simon says server error");
    });

// Return OK response for every request
server.CurrentRuleSet
    .AddRule()
    .SetOkResponse("ok");

// Return Bad Request for every request
server.CurrentRuleSet
    .AddRule()
    .SetBadRequest();

// Return OK response for every HTTP GET request
server.CurrentRuleSet
    .AddRule()
    .WhenGet()
    .SetOkResponse("ok");

// Return OK response for every HTTP Get request that matches an url patter
var urlPattern = "\\?id=1";
server.CurrentRuleSet
    .AddRule()
    .WhenUrlMatch(urlPattern)
    .SetOkResponse("ok")

// Return OK response for every HTTP request that has expected header
server.CurrentRuleSet
    .AddRule()
    .WhenHeaderMatch("User-Agent", expectedUserAgent)
    .SetOkResponse("ok");

// You can chain your configuration, if the request matches mutiple rules, the first match will be picked
var firstPattern = "\\?id=1";
var secondPattern = "\\?id=*";

server.CurrentRuleSet
    .AddRule()
    .WhenUrlMatch(firstPattern)
    .SetOkResponse("ok-1")
    .AddRule()
    .WhenUrlMatch(secondPattern)
    .SetOkResponse("ok-2");

// Expire the rule after a certain count of match, once the rule has expired, it will never match a request
server.CurrentRuleSet
    .AddRule()
    .SetMaxMatchCount(1)
    .SetOkResponse(okResponse);

// Return Ok response on the first request, and Ok response with a different content on the second request
server.CurrentRuleSet
    .AddRule()
    .SetMaxMatchCount(1)
    .SetOkResponse("ok-1");
    .AddRule()
    .SetOkResponse("ok-2");

// Generate a HttpClient instance
var httpClient = server.CreateClient();

// Apply a HttpMessageHandler to the client
var httpClient = server.CreateClient(new AddAuthorizationHandler());

Check the unit test cases for more examples

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 netcoreapp3.1 is compatible. 
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
0.0.4 467 1/23/2022
0.0.3 426 1/23/2022
0.0.2 408 1/23/2022