A small library to mock HTTP services. Its primary goal is to simplify writing integration tests for RESTful microservices.
First, create a new RestMockBuilder
object:
var mock = RestMockBuilder.New();
Then, configure all HTTP endpoints you need:
mock.Verb("GET").Url("/items/{id}").Returns(context =>
{
var id = context.GetRouteValue("id");
var json = new { id = id, content = "This is a fake object" };
context.Header("X-Server", "RestMock");
context.WriteJson(200, json);
});
// You may also use helpful extension methods:
mock.Post("/items").Returns(200);
mock.Put("/items/{id}").ReturnsJson(new { msg = "It's a fake object" });
You may also import a swagger JSON file:
var swagger = @"
/* Put swagger content here */
";
mock.ImportSwaggerJson(swagger);
This code will take all pathes and operations and define corresponding mocks. Mocks will respond with JSONs generated from swagger schemes.
Optionally, you may attach some custom middlewares, e.g. error handler:
mock.UseMiddleware(new MyHttpMiddleware());
mock.UseErrorHandler((method, url, exception) =>
{
Console.WriteLine($"RestMock failed to handle {method} {url}!");
Console.WriteLine(exception);
});
Finally, you may create a instance of HTTP server with configured mocks:
using(var server = mock.Create())
{
var url = server.ListenUrl;
// TODO Make some HTTP requests here
}