mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-21 17:10:26 +01:00
Updated Stubbing (markdown)
17
Stubbing.md
17
Stubbing.md
@@ -1,5 +1,4 @@
|
||||
# Stubbing
|
||||
|
||||
The core feature of WireMock is the ability to return predefined HTTP responses for requests matching criteria.
|
||||
|
||||
## Start a server
|
||||
@@ -17,7 +16,7 @@ The following code will configure a response with a status of 200 to be returned
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithUrl("/some/thing").UsingGet()
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
@@ -34,7 +33,7 @@ A response body in binary format can be specified as a `byte[]` via an overloade
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithUrl("/some/thing").UsingGet()
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
@@ -61,7 +60,7 @@ A JSON body will be considered to match a path expression if the expression retu
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithUrl("/some/thing").UsingGet()
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||
)
|
||||
.RespondWith(Response.Create().WithBody("Hello"));
|
||||
@@ -85,7 +84,7 @@ WireMock delegates to [XPath2.Net](https://github.com/StefH/XPath2.Net), therefo
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithUrl("/some/thing").UsingGet()
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
.WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
|
||||
)
|
||||
.RespondWith(Response.WithBody("Hello"));
|
||||
@@ -109,7 +108,7 @@ Example:
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(
|
||||
Request.Create().WithUrl("/some/thing").UsingGet()
|
||||
Request.Create().WithPath("/some/thing").UsingGet()
|
||||
)
|
||||
.RespondWith(
|
||||
Response.Create()
|
||||
@@ -150,7 +149,7 @@ var allRequests = server.RequestLogs;
|
||||
If you need to be more specific on the requests that have been send to the server, you can use the very same fluent API that allows to define routes:
|
||||
```csharp
|
||||
var customerReadRequests = server.SearchLogsFor(
|
||||
Request.Create().WithUrl("/api/customer*").UsingGet()
|
||||
Request.Create().WithPath("/api/customer*").UsingGet()
|
||||
);
|
||||
```
|
||||
|
||||
@@ -167,7 +166,7 @@ Delays can also be configured at route level:
|
||||
```csharp
|
||||
var server = FluentMockServer.Start();
|
||||
server
|
||||
.Given(Request.Create().WithUrl("/slow"))
|
||||
.Given(Request.Create().WithPath("/slow"))
|
||||
.RespondWith(
|
||||
Responses.Create()
|
||||
.WithStatusCode(200)
|
||||
@@ -181,4 +180,4 @@ server
|
||||
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()`.
|
||||
|
||||
## Getting all currently registered stub mappings
|
||||
All stub mappings can be fetched in C# by calling `server.ListAllStubMappings()`.
|
||||
All stub mappings can be fetched in C# by calling `server.ListAllStubMappings()`.
|
||||
Reference in New Issue
Block a user