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:
varmock=RestMockBuilder.New();Then, configure all HTTP endpoints you need:
mock.Verb("GET").Url("/items/{id}").Returns(context =>{varid=context.GetRouteValue("id");varjson=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:
varswagger=@" /* 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(newMyHttpMiddleware());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(varserver=mock.Create()){varurl=server.ListenUrl;// TODO Make some HTTP requests here}