Skip to content

Usage Examples (v2)

Matthew Herman edited this page May 18, 2021 · 5 revisions

The following examples are for version 2.X.X of the package. If you are still using a previous version, please see Usage Examples (v1).

Creating Imposters

This example shows how to create an HTTP imposter on port 8080. Currently only HTTP and TCP imposters are supported.

var client = new MountebankClient();
var imposter = client.CreateHttpImposter(8080);
client.Submit();

Adding Stubs to Imposters

This example shows how to add a stub to our imposter that will return a 400 status code.

imposter.AddStub().ReturnsStatus(HttpStatusCode.BadRequest);

A response object can also be returned from the imposter. The ReturnsJson and ReturnsXml methods will serialize the object into their respective formats and automatically set the Content-Type response header.

var obj = new TestObject { Name = "Ten", Value = 10 };
imposter.AddStub().ReturnsJson(HttpStatusCode.OK, obj);

If you would like to return plain text or a body of something other than XML or JSON, use the ReturnsBody method.

If setting up a more complicated response, such as one including headers, the Returns method can be called with a created response object.

var responseFields = new HttpResponseFields
{
    StatusCode = HttpStatusCode.BadRequest,
    Headers = new Dictionary<string, string> {{ "Location", "http://localhost:4545/customers/123" }}
}

var response = new IsResponse<HttpResponseFields>(responseFields);

imposter.AddStub().Returns(response);

Predicates can be specified on a stub as well. The following will return a response object if a request is made to the "/test" path.

imposter.AddStub().ReturnsJson(HttpStatusCode.OK, obj).OnPathEquals("/test");

A specific HTTP method can also be specified on the predicate.

imposter.AddStub().ReturnsStatus(HttpStatusCode.MethodNotSupported).OnPathAndMethodEqual("/test", Method.Post);

Similar to responses, a more complicated predicate can be added using the On method.

var complexPredicateFields = new HttpPredicateFields
{
    Method = Method.Post,
    Path = "/test",
    QueryParameters = new Dictionary<string, string> { { "first", "1" }, { "second", "2" } },
    Headers = new Dictionary<string, string> { { "Accept", "text/plain" } }
};

var complexPredicate = new EqualsPredicate<HttpPredicateFields>(complexPredicateFields);

imposter.AddStub().On(complexPredicate).ReturnsStatus(HttpStatusCode.BadRequest);

Currently, the above method of predicate creation is required to be used for any supported predicate other than "equals".

An alternate predicate constructor exists that will let you specify whether matches should be case sensitive (false by default), define a regex expression to remove prior to matching, or an xpath selector to that can be used to select values from an XML document.

Any number of stubs can be added to an imposter. If there are multiple, the stub with the first set of predicates to match the request will be used by mountebank.

Multiple responses/predicates can also be added to a single stub. If a stub has multiple responses, they will be returned in order. In the following example, the first request will get an 200 status code, but the next will receive a 404 status code.

imposter.AddStub().ReturnsStatus(HttpStatus.OK).ReturnsStatus(HttpStatus.NotFound);

When a stub has multiple predicates, they will be treated as an AND condition. For example:

imposter.AddStub().ReturnsStatus(HttpStatus.OK).OnPathAndMethodEqual("/test", Method.Get);

is equivalent to:

var firstPredicate = new EqualsPredicate<HttpPredicateFields>(new HttpPredicateFields { Path = "/test" });
var secondPredicate = new EqualsPredicate<HttpPredicateFields>(new HttpPredicateFields { Method = Method.Get});
imposter.AddStub().ReturnsStatus(HttpStatus.OK).On(firstPredicate).On(secondPredicate);

For more information on responses and predicates, see the mountebank documentation at http://www.mbtest.org.

Deleting Imposters

All existing imposters can be removed by calling the DeleteAllImposters methods on the client.

client.DeleteAllImposters();

To remove a single imposter, use the DeleteImposter method.

const int port = 8080;
client.DeleteImposter(port);

More Examples

The DocumentationTests.cs file in the MbDotNet.Acceptance.Tests project includes code samples for many of the examples shown in the mountebank documentation.

Clone this wiki locally