mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 07:24:34 +01:00
Updated Stubbing (markdown)
39
Stubbing.md
39
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()`.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user