rest-mock-core 0.8.0

dotnet add package rest-mock-core --version 0.8.0
                    
NuGet\Install-Package rest-mock-core -Version 0.8.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="rest-mock-core" Version="0.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="rest-mock-core" Version="0.8.0" />
                    
Directory.Packages.props
<PackageReference Include="rest-mock-core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add rest-mock-core --version 0.8.0
                    
#r "nuget: rest-mock-core, 0.8.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.
#:package rest-mock-core@0.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=rest-mock-core&version=0.8.0
                    
Install as a Cake Addin
#tool nuget:?package=rest-mock-core&version=0.8.0
                    
Install as a Cake Tool

NuGet Badge Build Status Coverage Status codecov

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:

using 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
using HttpServer mockServer = new HttpServer(5001);
var users = new[]
{
    new User { id = 1, username = "user1" },
    new User { id = 2, username = "user2" },
    new User { id = 3, username = "user3" },
};
var _usersJson = JsonSerializer.Serialize(_users);
mockServer.Config.Get("/api/users").Send(_usersJson).Verifiable();
mockServer.Config.Get("/api/users2").Send(_usersJson).Verifiable();
mockServer.Run();

var apiClient = new ApiClient("http://localhost:5001/api/users");
var apiClient2 = new ApiClient("http://localhost:5001/api/users2");

// Act
_ = await apiClient.GetUsernames();
_ = await apiClient2.GetUsernames();

// Assert
mockServer.Config.VerifyAll();

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

  • Simply verify:
//Arrange
...
var routeItem = mockServer.Config.Get("/api/users")
                                 .Send(_usersJson)
                                 .Verifiable();
mockServer.Run();

var apiClient = new ApiClient("http://localhost:5001/api/users");

// Act
_ = await apiClient.GetUsernames();

// Assert
routeItem.Verify();
  • Verify by an action:
//Arrange
...

// Act
_ = await apiClient.GetUsernames();
_ = await apiClient.GetUsernames();
_ = await apiClient.GetUsernames();

// Assert
routeItem.Verify(x => x == 3);
  • Verify using Moq Times:
//Arrange
...
// Act
_ = await apiClient.GetUsernames();
_ = await apiClient.GetUsernames();
_ = await apiClient.GetUsernames();

// Assert
routeItem.Verify(x => Times.AtLeast(2).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()
  • Don't forget to use using when instantiate HttpServer, otherwise server may not be disposed.

For more details please check Test project.

image

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.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.8.0 259 11/14/2025
0.7.12 31,157 4/6/2022
0.7.1 683 4/1/2022
0.6.0 1,688 2/28/2022
0.4.17 973 1/9/2022
0.4.12 3,745 12/30/2020
0.4.9 655 12/26/2020
0.4.5 709 12/24/2020
0.2.3 12,517 11/8/2017
0.2.2 1,637 12/29/2016
0.2.1 1,479 12/25/2016
0.1.8 1,514 12/23/2016

Add VerifyAll and Verify features