### Full Test Setup with Imposter and Verification Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md This example shows a complete test scenario: setting up an HTTP imposter with request recording enabled, executing code under test that interacts with the imposter, and then verifying the imposter's received requests. Use this for end-to-end testing of services that depend on external APIs. ```csharp var client = new MountebankClient(); var books = new [] { new Book { Id = 1, Name = "Great Expectations" }, new Book { Id = 2, Name = "A Christmas Carol" } }; await client.CreateHttpImposterAsync(4545, imposter => { imposter.RecordRequests = true; imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); }); var codeUnderTest = new CodeUnderTest(apiUrl: "http://localhost:4545"); var result = await codeUnderTest.GetBooks(); Assert.AreEqual(result, books); var booksImposter = await client.GetHttpImposterAsync(4545); Assert.AreEqual(1, booksImposter.NumberOfRequests); Assert.AreEqual("GET", booksImposter.Requests[0].Method); Assert.AreEqual("/books", booksImposter.Requests[0].Path); ``` -------------------------------- ### Behavior class instantiation (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of instantiating the Behavior class in v4. ```csharp var behavior = new Behavior { LatencyInMilliseconds = 1000 }; ``` -------------------------------- ### Add a Stub with Multiple Predicates (AND condition) Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub where multiple predicates must all be true for the stub to match. This example requires the path to be '/test' AND the method to be GET. ```csharp imposter.AddStub().ReturnsStatus(HttpStatus.OK).OnPathAndMethod("/test", Method.Get); ``` -------------------------------- ### Configure HTTP Imposter (v5) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Examples of configuring an HTTP imposter using properties or a configuration object in v5. ```csharp var imposter = new HttpImposter(4545, "MyImposter"); imposter.RecordRequests = true; imposter.DefaultResponse = response; imposter.AllowCORS = true; ``` ```csharp var imposter = new HttpImposter(4545, "MyImposter", new HttpImposterConfiguration { RecordRequests = true, DefaultResponse = response, AllowCORS = true }); ``` -------------------------------- ### HTTP Imposter Configuration Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Example of an HTTP imposter configuration with two stubs. The first stub matches GET requests to /books and returns a 200 status with a JSON body. The second stub acts as a fallback, returning a 404 status if the first stub is not matched. ```json { "port": 4545, "protocol": "http", "stubs": [ { predicates: [ { "matches": { "method": "GET", "path": "/books" } } ], responses: [ { "is": { "statusCode": 200, "body": [ { "id": 1, "name": "Great Expectations" }, { "id": 2, "name": "A Christmas Carol" } ] } } ] }, { "responses": [ { "is": { "statusCode": 404 } } ] } ] } ``` -------------------------------- ### Configure HTTP Imposter (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an HTTP imposter using client factory methods in v4. ```csharp var imposter = client.CreateHttpImposter(4545, "MyImposter", recordRequests: true, defaultResponse: response, allowCors: true); ``` -------------------------------- ### WaitBehavior class instantiation (v5) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of instantiating the renamed WaitBehavior class in v5, requiring latency. ```csharp var behavior = new WaitBehavior(1000); ``` -------------------------------- ### Full Test with Imposter Setup and Verification Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Demonstrates setting up an HTTP imposter with request recording enabled, exercising code under test, and verifying requests made to the imposter. Useful for end-to-end testing of API interactions. ```csharp var client = new MountebankClient(); var books = new [] { new Book { Id = 1, Name = "Great Expectations" }, new Book { Id = 2, Name = "A Christmas Carol" } }; var imposter = client.CreateHttpImposter(4545, recordRequests: true); imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); client.Submit(imposter); var codeUnderTest = new CodeUnderTest(apiUrl: "http://localhost:4545"); var result = await codeUnderTest.GetBooks(); Assert.AreEqual(result, books); var booksImposter = client.GetHttpImposter(4545); Assert.AreEqual(1, booksImposter.NumberOfRequests); Assert.AreEqual("GET", booksImposter.Requests[0].Method); Assert.AreEqual("/books", booksImposter.Requests[0].Path); ``` -------------------------------- ### Create and Submit Http Imposter (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an Http imposter and submitting it to Mountebank using v4.x syntax. ```csharp var imposter = client.CreateHttpImposter(4545, "MyImposter", true); imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); client.Submit(imposter); ``` -------------------------------- ### Response with behaviors array (v5) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an IsResponse with an array of behaviors in v5. ```csharp var response = new IsResponse(fields, new []{ new WaitBehavior(1000) }); ``` -------------------------------- ### Run Mountebank with Docker Compose Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Command to start Mountebank using Docker Compose from the root directory of the project. This is an alternative to running Mountebank directly. ```bash docker compose up ``` -------------------------------- ### Response with single behavior (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an IsResponse with a single WaitBehavior in v4. ```csharp var response = new IsResponse(fields, new WaitBehavior(1000)); ``` -------------------------------- ### Equivalent Stub with Explicit Predicates Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Demonstrates an alternative way to define the same stub logic as the previous example, by explicitly creating and chaining predicate objects. This is functionally identical to using OnPathAndMethod. ```csharp var firstPredicate = new EqualsPredicate("/test", null, null, null, null); var secondPredicate = new EqualsPredicate(null, Method.Get, null, null, null); imposter.AddStub().ReturnsStatus(HttpStatus.OK).On(firstPredicate).On(secondPredicate); ``` -------------------------------- ### Create HTTPS Imposter with valid PEM key/cert (v5) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an HTTPS imposter with valid PEM-formatted key and cert strings in v5. ```csharp var imposter = new HttpsImposter(4545, "MyImposter", new HttpsImposterConfiguration { Key = "-----BEGIN CERTIFICATE-----base64_encoded_data-----END CERTIFICATE-----", Cert = "-----BEGIN CERTIFICATE-----base64_encoded_data-----END CERTIFICATE-----" }); ``` -------------------------------- ### Create HTTPS Imposter with invalid key/cert (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of creating an HTTPS imposter with invalid key and cert strings in v4. ```csharp var imposter = client.CreateHttpsImposter(4545, "MyImposter", key: "invalid_key", cert: "invalid_cert"); ``` -------------------------------- ### Get Imposter Hypermedia Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves hypermedia links for the imposter configuration. This provides entry points to other configuration details. ```csharp GetEntryHypermediaAsync() ``` -------------------------------- ### Add Multiple Responses to a Single Stub Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Configures a single stub to return different responses sequentially for subsequent requests. The first request gets a 200 OK, and the second gets a 404 Not Found. ```csharp imposter.AddStub().ReturnsStatus(HttpStatus.OK).ReturnsStatus(HttpStatus.NotFound); ``` -------------------------------- ### HttpPredicateFields with string dictionary (v4) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of HttpPredicateFields using Dictionary for QueryParameters in v4. ```csharp var fields = new HttpPredicateFields { QueryParameters = new Dictionary { ["q"] = "search" } }; ``` -------------------------------- ### Get Mountebank Configuration Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves the current configuration of the Mountebank server. This includes details about imposters and their settings. ```csharp GetConfigAsync() ``` -------------------------------- ### Create HTTP Imposter with MbDotNet Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Example of creating an HTTP imposter and adding a stub with a specific path, method, and XML response. This is a basic usage scenario for setting up mock HTTP services. ```csharp await _client.CreateHttpImposter(4545, "My Imposter", imposter => { imposter.AddStub() .OnPathAndMethodEqual("/customers/123", Method.Get) .ReturnsXml(HttpStatusCode.OK, new Customer { Email = "customer@test.com" }); }); ``` -------------------------------- ### Create HTTP Imposter with Custom Predicate and Response Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md This snippet demonstrates how to create an HTTP imposter with a custom predicate (e.g., path starts with) and a more controlled response, including custom headers and body. Use this when fluent helpers do not cover specific matching or response requirements. ```csharp var adminPath = new StartsWithPredicate(new HttpPredicateFields { Path = "/admin" }); imposter.AddStub() .On(adminPath) .Returns( HttpStatusCode.Forbidden, new Dictionary { ["Date"] = DateTime.UtcNow }, "User does not have admin rights" ); ``` -------------------------------- ### Get Mountebank Logs Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves the logs generated by the Mountebank server. This is useful for debugging and monitoring. ```csharp GetLogsAsync() ``` -------------------------------- ### HttpPredicateFields with object dictionary (v5) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Example of HttpPredicateFields using Dictionary for QueryParameters in v5 to support array values. ```csharp var fields = new HttpPredicateFields { QueryParameters = new Dictionary { ["q"] = "search" } }; ``` -------------------------------- ### Create Imposter with Custom Predicate and Response Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Define a stub with a custom predicate (e.g., path starts with) and a more controlled response including custom headers and body. Use this when helper methods are insufficient. ```csharp var adminPath = new StartsWithPredicate(new HttpPredicateFields { Path = "/admin" }); imposter.AddStub() .On(adminPath) .Returns( HttpStatusCode.Forbidden, new Dictionary { ["Date"] = DateTime.UtcNow }, "User does not have admin rights" ); ``` -------------------------------- ### Create Http Imposter with Callback (v5 Recommended) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Recommended v5.x style for creating and configuring an Http imposter using a callback for configuration. This method handles submission to Mountebank. ```csharp # First style (recommended) var firstStyle = await client.CreateHttpImposter(4545, "MyImposter", imposter => { imposter.RecordRequests = true; imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); }); ``` -------------------------------- ### Build MbDotNet Project Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Command to build the MbDotNet project from the root directory. This is a standard .NET build command. ```bash dotnet build ``` -------------------------------- ### Create Http Imposter Manually (v5 Alternative) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Alternative v5.x style for creating an Http imposter instance manually and then submitting it to Mountebank. The recommended callback style is generally preferred to avoid accidental modifications after submission. ```csharp # Second style var secondStyle = new HttpImposter(4545, "MyImposter", new HttpImposterOptions { RecordRequests = true }); await client.CreateHttpImposter(secondStyle); ``` -------------------------------- ### Create HTTP Imposter with Stubs Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Set up an HTTP imposter with a stub that matches a specific path and method, returning JSON data. This is useful for mocking simple API endpoints. ```csharp var client = new MountebankClient(); var imposter = client.CreateHttpImposter(4545); var books = new [] { new Book { Id = 1, Name = "Great Expectations" }, new Book { Id = 2, Name = "A Christmas Carol" } }; imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); imposter.AddStub() .ReturnsStatus(HttpStatusCode.NotFound); client.Submit(imposter); ``` -------------------------------- ### Create Basic HTTP Imposter with Stubs Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Use this to create a basic HTTP imposter with predefined stubs for specific paths and methods, and a default not found response. It utilizes a fluent interface for natural stub definition. ```csharp var client = new MountebankClient(); await client.CreateHttpImposterAsync(4545, imposter => { var books = new [] { new Book { Id = 1, Name = "Great Expectations" }, new Book { Id = 2, Name = "A Christmas Carol" } }; imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsJson(HttpStatusCode.OK, books); imposter.AddStub() .ReturnsStatus(HttpStatusCode.NotFound); }); ``` -------------------------------- ### Create Telnet Imposter with Custom Implementation Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md This C# code demonstrates how to create and configure a Telnet imposter using the custom `CustomMountebankClient`. It adds two stubs: one for direct response and another for proxying. ```csharp var client = new CustomMountebankClient(); await client.CreateTelnetImposterAsync(23, "TelnetImposter", imposter => { imposter.AddStub() .OnCommandDeepEquals("show run\r\n") .ReturnsResponse("end\r\n\r\n#"); imposter.AddStub() .ReturnsProxy("telnet://example.org", ProxyMode.ProxyOnce, new [] { new MatchesPredicate(new TelnetBooleanPredicateFields { Command = true }) }); }) ``` -------------------------------- ### MountebankClient Constructors Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Instantiate the MountebankClient. The default constructor assumes Mountebank is running at http://localhost:2525. An alternative constructor allows specifying a custom URI. ```csharp MountebankClient() MountebankClient(Uri mountebankUri) ``` -------------------------------- ### Create Proxy Imposter with Query Parameter Predicates Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v4) Creates an HTTP imposter that proxies requests to a remote URL. Predicates are generated based on query parameters, and the proxy mode is set to 'ProxyOnce'. ```csharp var proxyImposter = client.CreateHttpImposter(5000); var predicateGenerators = new List> { new MatchesPredicate(new HttpBooleanPredicateFields { QueryParameters = true }) }; proxyImposter.AddStub().ReturnsProxy( new System.Uri($"http://sitetoproxyto.com"), ProxyMode.ProxyOnce, predicateGenerators); ``` -------------------------------- ### Combine Multiple Predicates with Equals Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Demonstrates how to combine multiple predicates using the EqualsPredicate for HTTP path and method matching. This is an alternative to using a single predicate with combined fields. ```csharp var combinedPredicateFields = new HttpPredicateFields { Path = "/books", Method = Method.Post }; imposter.AddStub() .On(new EqualsPredicate(combinedPredicateFields)) .ReturnsStatus(HttpStatusCode.Created); ``` ```csharp var pathPredicateFields = new HttpPredicateFields { Path = "/books" }; var methodPredicateFields = new HttpPredicateFields { Method = Method.Post }; imposter.AddStub() .On(new EqualsPredicate(pathPredicateFields)) .On(new EqualsPredicate(methodPredicateFields)) .ReturnsStatus(HttpStatusCode.Created); ``` -------------------------------- ### Create an HTTP Imposter Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Use this to create a new HTTP imposter on a specified port. Ensure Mountebank is running and accessible. ```csharp var client = new MountebankClient(); var imposter = client.CreateImposter(8080, Protocol.Http); client.Submit(); ``` -------------------------------- ### Run Unit Tests for MbDotNet Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Command to run only the unit tests for the MbDotNet project. This does not require Mountebank to be running. ```bash dotnet test --filter Category=Unit ``` -------------------------------- ### HTTP Stub: StartsWith Predicate and Custom Response Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add an HTTP stub using a StartsWith predicate for path matching and a custom response object. This is useful for matching requests based on partial path information. ```csharp var predicateFields = new HttpPredicateFields { Path = "/binaryData" }; var responseFields = new HttpResponseFields { StatusCode = HttpStatusCode.OK, ResponseObject = "data", Mode = "binary" }; imposter.AddStub() .On(new StartsWithPredicate(predicateFields)) .Returns(new IsResponse(responseFields)); ``` -------------------------------- ### Run Acceptance Tests for MbDotNet Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Command to run only the acceptance tests for the MbDotNet project. These tests require Mountebank to be running. ```bash dotnet test --filter Category=Acceptance ``` -------------------------------- ### Run All MbDotNet Tests Source: https://github.com/mattherman/mbdotnet/blob/master/README.md Command to run all tests for the MbDotNet project, including acceptance tests that require Mountebank. Ensure Mountebank is running with appropriate options. ```bash dotnet test ``` -------------------------------- ### Factory Methods for Creating Imposters Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Use these factory methods to create different types of imposters. Optional parameters allow for custom configuration like port, name, and request recording. ```csharp CreateHttpImposter(int? port = null, string name = null, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) CreateHttpsImposter(int? port = null, string name = null, string key = null, string cert = null, bool mutualAuthRequired = false, bool recordRequests = false, HttpResponseFields defaultResponse = null, bool allowCORS = false) CreateTcpImposter(int? port = null, string name = null, TcpMode mode = TcpMode.Text, bool recordRequests = false, TcpResponseFields defaultResponse = null) ``` -------------------------------- ### Methods to Create HTTP Imposters Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md These methods are used to create HTTP imposters. They allow specifying the port, an optional name, and a configuration action for the imposter. ```csharp CreateHttpImposterAsync(int? port, string name, Action imposterConfigurator); CreateHttpImposterAsync(int? port, Action imposterConfigurator) CreateHttpImposterAsync(HttpImposter imposter) ``` -------------------------------- ### HTTP Stub: Path and Method Equal and Returns XML Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add an HTTP stub that matches both a specific path and HTTP method, returning an XML response. This allows for more precise endpoint matching. ```csharp imposter.AddStub() .OnPathAndMethodEqual("/books", Method.Get) .ReturnsXml(HttpStatusCode.OK, books); ``` -------------------------------- ### Create HTTP Imposter Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v2) Creates an HTTP imposter on a specified port. Currently, only HTTP and TCP imposters are supported. ```csharp var client = new MountebankClient(); var imposter = client.CreateHttpImposter(8080); client.Submit(); ``` -------------------------------- ### Create HTTP Imposter Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v3) Creates an HTTP imposter on a specified port. All imposter types are supported except for SMTP. ```csharp var client = new MountebankClient(); var imposter = client.CreateHttpImposter(8080); client.Submit(imposter); ``` -------------------------------- ### Telnet Imposter Configuration (JSON) Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md This JSON configuration defines a Telnet imposter with two stubs. The first stub matches a specific command and returns a predefined response. The second stub proxies requests to a remote server based on a predicate. ```json { "port": 23, "protocol": "telnet", "stubs": [ { "predicates": [ { "deepEquals": { "command": "show run\r\n" } } ], "responses": [ { "is": { "response": "end\r\n\r\n#" } } ] }, { "responses": [ { "proxy": { "predicatesGenerators": [ { "matches": { "command": true } } ], "to": "telnet://example.org" } } ] } ] } ``` -------------------------------- ### Retrieve All Imposters Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves a list of all imposters currently configured in Mountebank. The returned objects are simplified representations. ```csharp GetImpostersAsync() ``` -------------------------------- ### Methods for Deleting Imposters and Requests Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Delete specific imposters by port, delete all imposters, or clear saved requests for a specific imposter. ```csharp DeleteImposterAsync(int port) DeleteAllImpostersAsync() DeleteSavedRequestsAsync(int port) ``` -------------------------------- ### Methods for Retrieving Imposters Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Retrieve imposter information by port. These methods return a simplified representation that includes requests and stub matches if debug mode is enabled. ```csharp GetHttpImposter(int port) GetHttpsImposter(int port) GetTcpImposter(int port) ``` -------------------------------- ### Add a Stub Matching Path and HTTP Method Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub that returns a response only if the request path is '/test' and the HTTP method is POST. ```csharp imposter.AddStub().ReturnsStatus(HttpStatusCode.MethodNotSupported).OnPathAndMethodEqual("/test", Method.Post); ``` -------------------------------- ### Multiple Responses in a Stub Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Illustrates how to define multiple responses for a single stub. Mountebank will cycle through these responses in a round-robin fashion each time the stub is matched, useful for simulating sequential states like a delete operation. ```csharp imposter.AddStub() .OnPathAndMethodEqual("/books/123", Method.Delete) .ReturnsStatus(HttpStatusCode.NoContent) .ReturnsStatus(HttpStatusCode.NotFound); ``` -------------------------------- ### Define Custom Telnet Imposter and Stub Classes Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md These C# classes extend MbDotNet's `Imposter` and `Stub` to support a custom Telnet protocol. They define specific fields for predicates and responses, enabling custom interactions. ```csharp public class TelnetImposter : Imposter { public TelnetImposter(int? port, string name, bool recordRequests) : base(port, "telnet", name, recordRequests) { } } public class TelnetStub : Stub { public TelnetStub OnCommandDeepEquals(string command) { var fields = new TelnetPredicateFields { Command = command }; Predicates.Add(new DeepEqualsPredicate(fields)); return this; } public TelnetStub ReturnsResponse(string response) { var fields = new TelnetResponseFields { Response = response }; Responses.Add(new IsResponse(fields)); return this; } public TelnetStub ReturnsProxy(Uri to, ProxyMode proxyMode, IEnumerable> predicateGenerators) { var fields = new ProxyResponseFields { To = to, Mode = proxyMode, PredicateGenerators = predicateGenerators.ToList() }; Responses.Add(new ProxyResponse>(fields)); return this; } } public class TelnetPredicateFields : PredicateFields { [JsonProperty("command", NullValueHandling = NullValueHandling.Ignore)] public string Command { get; set; } } public class TelnetBooleanPredicateFields : PredicateFields { [JsonProperty("command", NullValueHandling = NullValueHandling.Ignore)] public bool? Command { get; set; } } public class TelnetResponseFields : ResponseFields { [JsonProperty("response", NullValueHandling = NullValueHandling.Ignore)] public string Response { get; set; } } public class CustomMountebankClient : MountebankClient { public async Task CreateTelnetImposterAsync(int port, string name, bool recordRequests, CancellationToken cancellationToken = default) => await ConfigureAndCreateImposter(new TelnetImposter(port, name, recordRequests), _ => { }, cancellationToken); } ``` -------------------------------- ### Create Multiple Imposters Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v3) Submits multiple imposters simultaneously. Supports creating HTTP and TCP imposters. ```csharp var client = new MountebankClient(); var imposterList = new List(); imposterList.Add(client.CreateHttpImposter(8080)); imposterList.Add(client.CreateTcpImposter(443)); client.Submit(imposterList); ``` -------------------------------- ### Retrieve HTTP Imposter Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v3) Use this method to retrieve an existing HTTP imposter for inspection. An ImposterNotFoundException is thrown if no imposter exists on the specified port. An InvalidProtocolException is thrown if an imposter exists but does not match the expected protocol. ```csharp client.GetHttpImposter(port); ``` -------------------------------- ### Proxy Response with Predicate Generators Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Configures a proxy response that forwards requests to a specified origin. It uses predicate generators to define how Mountebank creates stubs based on the proxied responses, and ProxyMode.ProxyOnce ensures subsequent requests are handled by the imposter. ```csharp var predicateGenerators = new List> { new(new HttpBooleanPredicateFields { QueryParameters = true }) }; imposter.AddStub() .ReturnsProxy( new Uri("http://origin-server.com"), ProxyMode.ProxyOnce, predicateGenerators ); ``` -------------------------------- ### Update Async Client Method Calls Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Client methods are now async and include an 'Async' suffix. Update method names and use 'await'. Ensure calling methods are also async. ```csharp client.GetHttpImposter(4545); ``` ```csharp await client.GetHttpImposterAsync(4545); ``` -------------------------------- ### HTTP Stub: Method Equals and Returns Status Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add an HTTP stub that matches a specific HTTP method and returns a specified status code. Useful for defining responses for different HTTP verbs. ```csharp imposter.AddStub() .OnMethodEquals(Method.Post) .ReturnsStatus(HttpStatusCode.Created); ``` -------------------------------- ### Update Test Method Signatures to Async Source: https://github.com/mattherman/mbdotnet/blob/master/docs/v4-to-v5-migration.md Test methods that call async client methods must also be async. Change void signatures to async Task. ```csharp public void MyTest() { ... } ``` ```csharp public async Task MyTest { ... } ``` -------------------------------- ### Delete All Imposters Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Removes all imposters currently running on Mountebank. Use with caution as this is a global reset. ```csharp client.DeleteAllImposters(); ``` -------------------------------- ### Add a Stub with Complex Predicates Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub with a complex predicate that matches based on path, method, headers, and query parameters. This allows for highly specific request matching. ```csharp var headers = new Dictionary {{ "Accept", "text/xml" }}; var queryParameters = new Dictionary {{ "id", "10" }}; var predicate = new EqualsPredicate("/test", Method.Get, null, headers, queryParameters); imposter.AddStub().ReturnsStatus(HttpStatusCode.UnsupportedMediaType).On(predicate); ``` -------------------------------- ### Add a Stub Matching a Specific Path Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub that only returns a response if the request path exactly matches '/test'. ```csharp imposter.AddStub().ReturnsJson(HttpStatusCode.OK, obj).OnPathEquals("/test"); ``` -------------------------------- ### Replace HTTP Imposter Stubs Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Replaces all existing stubs on an HTTP imposter with a new list of stubs. The imposter is identified by its port number. ```csharp ReplaceHttpImposterStubsAsync(int port, IEnumerable replacementStubs) ``` -------------------------------- ### Add Multiple Predicates to a Single Stub (AND condition) Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v2) Adds multiple predicates to a single stub, which are treated as an AND condition. The stub will only match if all predicates are satisfied. ```csharp imposter.AddStub().ReturnsStatus(HttpStatus.OK).OnPathAndMethodEqual("/test", Method.Get); ``` ```csharp var firstPredicate = new EqualsPredicate(new HttpPredicateFields { Path = "/test" }); var secondPredicate = new EqualsPredicate(new HttpPredicateFields { Method = Method.Get}); imposter.AddStub().ReturnsStatus(HttpStatus.OK).On(firstPredicate).On(secondPredicate); ``` -------------------------------- ### Add a Stub with a Bad Request Response Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub to an existing imposter that will return an HTTP 400 Bad Request status code when matched. ```csharp imposter.AddStub().ReturnsStatus(HttpStatusCode.BadRequest); ``` -------------------------------- ### Add Stub with Path Predicate Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v3) Adds a stub to an imposter that responds only when a request is made to a specific path. The stub returns a JSON response. ```csharp var obj = new TestObject { Name = "Ten", Value = 10 }; imposter.AddStub().ReturnsJson(HttpStatusCode.OK, obj).OnPathEquals("/test"); ``` -------------------------------- ### HTTP Stub: Path Equals and Returns JSON Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add an HTTP stub that matches a specific path and returns a JSON response. This is useful for defining simple API endpoints. ```csharp imposter.AddStub() .OnPathEquals("/books") .ReturnsJson(HttpStatusCode.OK, books); ``` -------------------------------- ### Retrieve HTTP Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves an HTTP imposter by its port. This method returns a simplified representation without stubs but may include requests and stub matches if Mountebank is configured accordingly. ```csharp GetHttpImposterAsync(int port) ``` -------------------------------- ### Retrieve HTTPS Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves an HTTPS imposter by its port. The returned object is a simplified representation, potentially including recorded requests and stub matches based on Mountebank's configuration. ```csharp GetHttpsImposterAsync(int port) ``` -------------------------------- ### Add a Stub with a Custom Response Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub with a fully customized response, including headers, status code, and a response object. Use this for complex response scenarios. ```csharp var headers = new Dictionary {{ "Location", "http://localhost:4545/customers/123" }}; var response = new IsResponse(HttpStatusCode.OK, obj, headers); imposter.AddStub().Returns(response); ``` -------------------------------- ### Add HTTP Imposter Stub Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Adds a new stub to an HTTP imposter at an optional specified index. The imposter is identified by its port. ```csharp AddHttpImposterStubAsync(int port, HttpStub newStub, int? newStubIndex) ``` -------------------------------- ### Replace Single HTTP Imposter Stub Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Replaces a specific stub on an HTTP imposter at a given index with a new stub. The imposter is identified by its port. ```csharp ReplaceHttpImposterStubAsync(int port, HttpStub replacementStub, int stubIndex) ``` -------------------------------- ### HTTP Stub: Injected Function and Returns Body Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add an HTTP stub that uses an injected JavaScript function for predicate matching and returns a custom response body. This enables dynamic stub behavior. ```csharp imposter.AddStub() .OnInjectedFunction("function(config) { return true; }") .ReturnsBody(HttpStatusCode.OK, "a=1&b=2"); ``` -------------------------------- ### Retrieve TCP Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves a TCP imposter by its port. The result is a simplified view of the imposter, which may contain requests and stub matches if Mountebank is running with specific flags. ```csharp GetTcpImposterAsync(int port) ``` -------------------------------- ### Add a Stub Returning JSON Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Adds a stub that returns a JSON response with a 200 OK status code. The provided object is automatically serialized to JSON, and the Content-Type header is set. ```csharp var obj = new TestObject { Name = "Ten", Value = 10 }; imposter.AddStub().ReturnsJson(HttpStatusCode.OK, obj); ``` -------------------------------- ### Add Stub with Custom Response Fields Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v2) Adds a stub with a custom HTTP response, including headers and status code. This is useful for more complex response configurations. ```csharp var responseFields = new HttpResponseFields { StatusCode = HttpStatusCode.BadRequest, Headers = new Dictionary {{ "Location", "http://localhost:4545/customers/123" }} } var response = new IsResponse(responseFields); imposter.AddStub().Returns(response); ``` -------------------------------- ### TCP Stub: Data Equals and Returns Data Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add a TCP stub that matches exact data input and returns specific data. This is suitable for simple TCP protocols. ```csharp imposter.AddStub() .OnDataEquals("123456") .ReturnsData("abcdefg"); ``` -------------------------------- ### Add Stub with Custom Response Body Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v4) Adds a stub to return plain text or a custom body content. Use this when the response is not JSON or XML. ```csharp imposter.AddStub().ReturnsBody("This is a plain text response."); ``` -------------------------------- ### Add Stub with Complex Predicate Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v2) Adds a stub using a complex predicate object that specifies method, path, query parameters, and headers. ```csharp var complexPredicateFields = new HttpPredicateFields { Method = Method.Post, Path = "/test", QueryParameters = new Dictionary { { "first", "1" }, { "second", "2" } }, Headers = new Dictionary { { "Accept", "text/plain" } } }; var complexPredicate = new EqualsPredicate(complexPredicateFields); imposter.AddStub().On(complexPredicate).ReturnsStatus(HttpStatusCode.BadRequest); ``` -------------------------------- ### Delete HTTP Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Deletes an HTTP imposter associated with the specified port. This action removes the imposter and all its associated configurations. ```csharp DeleteImposterAsync(int port) ``` -------------------------------- ### Delete a Specific Imposter Source: https://github.com/mattherman/mbdotnet/wiki/Usage-Examples-(v1) Removes a single imposter running on a specific port. Provide the port number of the imposter to be deleted. ```csharp const int port = 8080; client.DeleteImposter(port); ``` -------------------------------- ### TCP Stub: Injected Function and Returns Data Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v4.md Add a TCP stub that uses an injected JavaScript function for predicate matching and returns specific data. This allows for more complex TCP stub logic. ```csharp imposter.AddStub() .OnInjectedFunction("function(config) { return true; }") .ReturnsData("abcdefg"); ``` -------------------------------- ### Remove HTTP Imposter Stub Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Removes a stub from an HTTP imposter at a specified index. The imposter is identified by its port. ```csharp RemoveStubAsync(int port, int stubIndex) ``` -------------------------------- ### Retrieve SMTP Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Retrieves an SMTP imposter by its port. This method returns a simplified imposter representation, potentially including requests and stub matches depending on Mountebank's operational mode. ```csharp GetSmtpImposterAsync(int port) ``` -------------------------------- ### Delete Saved Requests for Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Deletes all saved requests for a specific imposter identified by its port. This is useful for clearing historical request data. ```csharp DeleteSavedRequestsAsync(int port) ``` -------------------------------- ### Delete Saved Proxy Responses for Imposter Source: https://github.com/mattherman/mbdotnet/blob/master/docs/docs-v5.md Deletes all saved proxy responses for a specific imposter identified by its port. This clears any cached proxy responses. ```csharp DeleteSavedProxyResponsesAsync(int port) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.