ExtendBoomerang - garfieldmoore/Boomerang GitHub Wiki

Boomerang was designed with extension in mind. There are a number of ways you can extend Boomerang.

Extending the interface

You've already seen an example of extending the interface in our Trace request example;

Implementing a new Http verb

Imagine we use the Request interface method to configure a Trace request as below.

      host.Request("api/menu", "TRACE").Returns("", 200);

If we did this often we would have a lot of code duplication. So we might consider extending the Boomerang interface by creating our own extension method.

    public static IRequestHandler Trace(this IBoomerang host, string address)
    {
        return host.Request(address, "TRACE");
    }

Now we can use this anywhere in our project to configure a trace request.

    host.Trace("api/menu").Returns("", 200);

Adding Repeat

In the same way we can add a method to configure multiple Http Get requests.

  public static IBoomerang Repeat(this IBoomerang host, 
     Action<IBoomerang>   configuration, int n)
  {
      for (int i = 0; i < n; i++)
      {
          configuration(host);
      }

      return host;
  }

Now we can configure as many requests as we like.

    host.Repeat(x => x.Get("api/menu").Returns("Mocha", 200),10);

Changing behaviour

So far we've looked at ways to extend the framework that added simple if convenient syntactic sugar.

However, we can also change Boomerang's behaviour in more interesting ways.

Changing the web server

Under construction

Extending Request handling

Boomerang's default behaviour for handling requests can be replaced by implementing the IResponseRepository interface and configuring the framework to use it;

 var proxy = Boomerang.Create(x =>
        {
            x.AtAddress("http://localhost:5300");
            x.UseRequestHandlerFactory(() => new ConsoleRequestHandler());
        }
        );
        proxy.Start();

The UseRequestHandlerFactory takes a method that creates the concrete implementation of IResponseRepository .

The IResponseRepository interface is used to store and retrieve the desired requests and responses. It defines the following interface;

    void AddAddress(Request request);
    void AddResponse(string body, int statusCode);
    void AddResponse(string body, int statusCode, IDictionary<string, string> headers);
    Response GetNextResponseFor(string method, string addressTarget);

The AddAddress and AddResponse methods are used to store the required responses when configuring the API.

The GetNextResponseFor method is used by the framework to ask for the response to a request. The implementation must decide what response is required and what to respond with if one is not configured.

⚠️ **GitHub.com Fallback** ⚠️