Giter Site home page Giter Site logo

wiremock4microservices-net's Introduction

WireMock4Microservices.Net

WireMock4Microservices.Net is a .Net library built on top of WireMock.Net. It enhances support for mocked requests by injecting one or more services and extends WireMock's features using the original Callback Response.

โ“ Why 4Microservices?

It's common behavior for microservices to share contracts, SDKs, and messages among themselves. While WireMock supports webhooks, it cannot send or consume messages from queues or topics. By implementing the IWiremockEndpoint (see the "Using" section), it becomes possible to inject any service and create the expected behavior for simple or more complex scenarios.

๐Ÿ“„ Using

To begin creating mocked endpoints, implement the IWiremockEndpoint or inherit from WiremockEndpoint to start creating mocked endpoints as follows:

class MyFakeEndpoint : IWiremockEndpoint
{
    public IRequestMatcher RequestMatcher { get; }
    public IWebhook[] Webhooks { get; }

    public Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        throw new NotImplementedException();
    }
}

RequestMatcher Property

Start by defining the request matching criteria within the RequestMatcher property. For more details, refer to the WireMock.Net documentation.

Webhooks Property

The Webhooks property is optional and allows you to configure webhooks. Further information can be found in the WireMock.Net documentation.

CallbackHandler Method

The CallbackHandler method is responsible for generating the response based on the RequestMessage. You can use the original IRequestMessage and utilize services to create more complex scenarios. Here are examples of different response types:

Single text

public class GetHealth : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/health");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.OK)
                .BuildWithText("Healthy")
        );
    }
}

Json String

public class PostOrdersCheckoutWillReturnAccepted : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/orders/checkout");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.Accepted)
                .BuildWithStringAsJson("""
                                       {
                                         "orderId: "xpto",
                                         "status": "Processing"
                                       }
                                       """));
    }
}

Json Data

public class PostOrdersCheckoutWillReturnAccepted : WiremockEndpoint
{
    public override IRequestMatcher RequestMatcher
        => Request.Create().UsingGet().WithPath("/orders/checkout");

    public override Task<ResponseMessage> CallbackHandler(IRequestMessage requestMessage)
    {
        return Task.FromResult(
            ResponseMessageBuilder
                .Create()
                .WithStatusCode(HttpStatusCode.Accepted)
                .BuildWithDataAsJson(new() { orderId = "xpto", status = "Processing" }));
    }
}

Using with ASP.NET Web

To use WireMock4Microservices.Net with ASP.NET Web, you need to perform two steps in your Program or Startup file:

var builder = WebApplication.CreateBuilder(args);

IConfiguration configuration = builder.Configuration;

var services = builder.Services;

services.AddWiremockEndpoints(configuration, Assembly.GetExecutingAssembly());

In the AddWiremockEndpoints method, the Assembly parameter specifies where your implementations of IWiremockEndpoint are located. Through reflection, all implementations will be created using the WireMockServer singleton instance. The IConfiguration is used to get the WireMockServerSettings, see the WireMock.Net documentation for more information.

Finally, use the IApplicationBuilder to call the following method:

app.UseWiremockEndpoints();

This setup enables WireMock4Microservices.Net within your ASP.NET Web application.

wiremock4microservices-net's People

Contributors

pedroccrl avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.