rest-mock-core 0.7.12

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

// Install rest-mock-core as a Cake Tool
#tool nuget:?package=rest-mock-core&version=0.7.12                

NuGet Badge Build Status Coverage Status

rest-mock-core

A simple http server for using in test projects which test .net core based projects.

Problem

When I started to write some tests for a dotnet core app, I realized that many libraries do not work on that platform. One of my problems was to find an appropriate HTTP Server Mocking library. So, I created this project.

Install

dotnet add package rest-mock-core

Usage

You can create and run a mock server as below. Default url is http://localhost:5000 The port can be changed in the constructor:

HttpServer mockServer = new HttpServer(5001);
mockServer.Run();

Then you can use any http client sending request to it.

HttpClient httpClient = new HttpClient(5001);
var response = await httpClient.GetAsync("http://localhost:5001/");
  • If you call the root of server, it will return "It Works!" with a OK status code (200). Of course it can be overrided by adding a responose for the root url.

  • You can use server.Config to manage requests, then server will return configured responses to your requests :

mockServer.Config.Get("/api/product/").Send("It Really Works!");
  • If you call a address which is not configured, you will receive "Page not found!" with status code (404).

Assert

You can mark each RouteItem as Verifiable and on the assert step all can be verified at once:

//Arrange
_mockServer = new HttpServer(PORT);
_mockServer.Run();
_mockServer.Config.Get("/api/product/").Send("It Really Works!").Verifiable();
_mockServer.Config.Post("/api/login/").Send("Welcome.").Verifiable();

//Act
var getMessage = new HttpRequestMessage(HttpMethod.Get, $"{_address}/api/product/");
_ = await _httpClient.SendAsync(getMessage);
var postMessage = new HttpRequestMessage(HttpMethod.Post, $"{_address}/api/login/");
_ = await _httpClient.SendAsync(postMessage);

//Assert
_mockServer.Config.VerifyAll();

Also it is possible to verify each route item in different ways:

  • Simply verify:
//Arrange
var route = new RouteTableItem(GET, URL_WITH_QUERY, _headers);
route.IsVerifiable = true;
route.CallCounter = 1;

//Act & Assert
route.Verify();
  • Verify by an action:
//Arrange
var route = new RouteTableItem(GET, URL_WITH_QUERY, _headers);
route.IsVerifiable = true;
route.CallCounter = _fixture.Create<int>();

//Act & Assert
route.Verify(x => x > (route.CallCounter - 1));
  • Verify using Moq Times:
//Arrange
var route = new RouteTableItem(GET, URL_WITH_QUERY, _headers);
route.IsVerifiable = true;
route.CallCounter = _fixture.Create<int>();

//Act & Assert
route.Verify(x => Times.AtLeast(route.CallCounter - 1).Validate(x));

More

There are some options to manage the requests easier:

mockServer.Config.Get("/api/v1/product/123").Send("It Really Works!");
mockServer.Config.Post("/api/v2/store/a123b").Send("Failed", 503);
mockServer.Config.Delete("/contact/y7eEty9").Send("Done", HttpStatusCode.OK);
mockServer.Config.Put("/branche/northwest1254").Send("updated", 200);
mockServer.Config.Get("/messages/123").Send(context =>
            {
                context.Response.StatusCode = 200;
                string response = "<h1>Your new message<h1>";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(response);
                buffer = System.Text.Encoding.UTF8.GetBytes(response);
                context.Response.Body.WriteAsync(buffer, 0, buffer.Length);
            });

Also, it is possible to use other types of 'http method' by using 'Request':

mockServer.Config.Request("PATCH", "/api/v2.3/products/3234")
                 .Send("We don't use PATCH here.", HttpStatusCode.MethodNotAllowed);

Lastly, headers can be added to the request:

Dictionary<string, string> headers = new(){{"user-agent", "IE6"}};
mockServer.Config.Request("PATCH", "/api/v2.3/products/3234", headers)
                 .Send("C'mon man! Really IE6?", HttpStatusCode.MethodNotAllowed);

You can use server.Config either before or after server.Run()

For more details please check Test project.

Product 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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.7.12 19,867 4/6/2022
0.7.1 546 4/1/2022
0.6.0 1,410 2/28/2022
0.4.17 808 1/9/2022
0.4.12 3,524 12/30/2020
0.4.9 501 12/26/2020
0.4.5 552 12/24/2020
0.2.3 11,456 11/8/2017
0.2.2 1,275 12/29/2016
0.2.1 1,121 12/25/2016
0.1.8 1,175 12/23/2016

Add VerifyAll and Verify features