From 5166ad4ab5bdd462fa1df583c21ef0d360804d4a Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 17 Aug 2022 07:59:41 +0200 Subject: [PATCH] Updated Stubbing (markdown) --- Stubbing.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Stubbing.md b/Stubbing.md index ba9d3ff..14f441c 100644 --- a/Stubbing.md +++ b/Stubbing.md @@ -12,6 +12,7 @@ To know on which port your server is listening, just use server property *Port*. ## Basic stubbing The following code will configure a response with a status of 200 to be returned when the relative URL exactly matches /some/thing (including query parameters). The body of the response will be “Hello world!” and a Content-Type header will be sent with a value of text-plain. +### C# example ```csharp var server = WireMockServer.Start(); @@ -28,7 +29,35 @@ server ``` HTTP methods currently supported are: GET, POST, PUT, DELETE, HEAD. You can specify ANY (`.UsingAny`) if you want the stub mapping to match on any request method. -A response body in binary format can be specified as a `byte[]` via an overloaded `WithBody()`: +### Json Mapping example +``` json +{ + "Request": { + "Path": { + "Matchers": [ + { + "Name": "WildcardMatcher", + "Pattern": "/some/thing" + } + ] + }, + "Methods": [ + "get" + ] + }, + "Response": { + "StatusCode": 200, + "Body": "Hello world!", + "Headers": { + "Content-Type": "text/plain" + } + } +} +``` + +### C# Example for a body with bytes + +A response body in binary format can also be specified as a `byte[]` via an overloaded `WithBody()`: ```csharp var server = WireMockServer.Start(); @@ -42,6 +71,8 @@ server ); ``` +--- + ## Stub priority It is sometimes the case that you’ll want to declare two or more stub mappings that "overlap", in that a given request would be a match for more than one of them. @@ -63,6 +94,8 @@ server .RespondWith(Responses.Create().WithStatusCode(200)); ``` +--- + ## Verify interactions The server keeps a log of the received requests. You can use this log to verify the interactions that have been done with the server during a test. To get all the request received by the server, you just need to read property *LogEntries*: @@ -76,6 +109,8 @@ var customerReadRequests = server.FindLogEntries( ); ``` +--- + ## Simulating delays A server can be configured with a global delay that will be applied to all requests. To do so you need to call method WireMockServer.AddRequestProcessingDelay() as below: ```csharp @@ -99,6 +134,8 @@ server ); ``` +--- + ## Reset The WireMock server can be reset at any time, removing all stub mappings and deleting the request log. If you’re using either of the UnitTest rules this will happen automatically at the start of every test case. However you can do it yourself via a call to `server.Reset()`.