rest-mock-core
0.7.1
See the version list below for details.
dotnet add package rest-mock-core --version 0.7.1
NuGet\Install-Package rest-mock-core -Version 0.7.1
<PackageReference Include="rest-mock-core" Version="0.7.1" />
paket add rest-mock-core --version 0.7.1
#r "nuget: rest-mock-core, 0.7.1"
// Install rest-mock-core as a Cake Addin #addin nuget:?package=rest-mock-core&version=0.7.1 // Install rest-mock-core as a Cake Tool #tool nuget:?package=rest-mock-core&version=0.7.1
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 type 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 | Versions 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. |
-
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.
Add VerifyAll and Verify features