mockserver.client
1.0.0
dotnet add package mockserver.client --version 1.0.0
NuGet\Install-Package mockserver.client -Version 1.0.0
<PackageReference Include="mockserver.client" Version="1.0.0" />
paket add mockserver.client --version 1.0.0
#r "nuget: mockserver.client, 1.0.0"
// Install mockserver.client as a Cake Addin #addin nuget:?package=mockserver.client&version=1.0.0 // Install mockserver.client as a Cake Tool #tool nuget:?package=mockserver.client&version=1.0.0
MockServerClient
Dot net wrapper for mock server.
Example usage
Given mock server is running on port 1080, e.g.
docker run -p 1080:1080 -d --platform linux/amd64 mockserver/mockserver:latest
Setup a mock using the library in c# code
using (var httpClient = new HttpClient{ BaseAddress = new Uri("http://localhost:1080/")})
{
var mockServerClient = new MockServerClient(httpClient);
var expectedRequest = new RequestBuilder().WithPath("/helloworld").Create()
await mockServerClient.SetExpectations(
new Expectation
{
HttpRequest = expectedRequest,
HttpResponse = new ResponseBuilder().WithStatusCode(200).Create()
});
}
Test the mock
curl -I localhost:1080/helloworld
Verify the expected request was made
await mockServerClient.Verify(expectedRequest.Create(), VerificationTimes.Once)
Configuring Matches
Header Matches
The expectation builders allow you to setup matching in a few different ways. See the examples below and the MockServer documentation for the kinds of syntax that are supported.
Example: requiring a named header with a specific value.
new MockServerClient(httpClient)
.When(RequestBuilder.Build()
.WithPath("/mypath")
.WithMethod(HttpMethod.Post)
.WithHeaders(new Dictionary<string, IEnumerable<string>>{{"HeaderOne", new[]{"ValueOne"}}}))
.Respond(responseBuilder => responseBuilder.WithStatusCode(200));
MockServer supports a schema value syntax enabling you to match header values by type. This is enabled via the SchemaValue
class.
Example: Requiring a header with the name HeaderOne
which has an integer
value.
new MockServerClient(httpClient)
.When(RequestBuilder.Build()
.WithPath("/mypath")
.WithMethod(HttpMethod.Post)
.WithHeaders(headersResponseBuilder => headersResponseBuilder.WithHeader(
headerResponseBuilder => headerResponseBuilder
.WithName("HeaderOne")
.WithValues(
headerValueResponseBuilder => headerValueResponseBuilder.WithValue(SchemaValue.Integer())))))
.Respond(responseBuilder => responseBuilder.WithStatusCode(200));
Multiple expected header values can be configured by chaining calls to WithValue
on the headerValueResponseBuilder
and multiple headers can be configured by chaining calls to WithHeader
on the headersResponseBuilder
.
Optional Headers
Optionality can be configured in mockserver using the ?
operator. For example
.WithHeaders(headersResponseBuilder => headersResponseBuilder.WithHeader(
headerResponseBuilder => headerResponseBuilder
.WithName("?HeaderTwo")
.WithValues(
headerValueResponseBuilder => headerValueResponseBuilder.WithValue("?TestValue"))))
Exclusive Headers
Exclusive headers/values can be configured using the !
operator. i.e. when a header/value must not be included.
.WithHeader(
headerResponseBuilder => headerResponseBuilder
.WithName("!HeaderThree"))
Regex Matching
Both header name and values can be matched by regex strings, e.g.
.WithHeader(
headerResponseBuilder => headerResponseBuilder
.WithName("Header.*")
.WithValues(
headerValueResponseBuilder => headerValueResponseBuilder.WithValue(@"[A-Z0-9\\-]+")))
Similarly, regex can be used using the schema value syntax, e.g.
.WithHeader(
headerResponseBuilder => headerResponseBuilder
.WithName("Header.*")
.WithValues(
headerValueResponseBuilder => headerValueResponseBuilder.WithValue(SchemaValue.StringWithPattern("[A-Z0-9\\-]+"))))
Configuring Query String Matches
The expectation builders allow you to setup matching in a few different ways. See the examples below and the MockServer documentation for the kinds of syntax that are supported.
Example: Requiring a query string with the name qs
with a specific value of valueone
:
new MockServerClient(httpClient)
.When(RequestBuilder().Build()
.WithMethod(HttpMethod.Get)
.WithPath("/mypath")
.WithQueryStringParameters(queryStringExpectationBuilder => queryStringExpectationBuilder
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("qs")
.WithValue("valueone"))
)
)
.Respond(new ResponseBuilder().WithStatusCode(200));
MockServer supports a schema value syntax enabling you to match query string parameters by type. This is enabled via the SchemaValue
class.
Example: Requiring a query string with the name qs
which contains a guid/uuid value:
new MockServerClient(httpClient)
.When(new RequestBuilder()
.WithMethod(HttpMethod.Get)
.WithPath("/mypath")
.WithQueryStringParameters(queryStringExpectationBuilder => queryStringExpectationBuilder
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("qs")
.WithValue(SchemaValue.Uuid()))
)
)
.Respond(new ResponseBuilder().WithStatusCode(200));
Multiple query string match configurations can be specified by chaining WithParameter
calls.
Multiple query string value match configurations can be specified by chaining WithValue
calls.
Optional Query Strings
MockServer supports specifying optional query strings. These can be setup by specifying a parameter name with a leading ?
, e.g.
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("?qs")
.WithValue(SchemaValue.Uuid())
Exclusive Query Strings
Query strings which must not appear are supported by specifying a parameter with a leading !
, e.g.
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("!qs")
.WithValue(SchemaValue.Integer())
Regex Matching
Both querystring name and values can be matched by regex strings, e.g.
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("[A-z]{0,10}")
.WithValue("[A-Z0-9\\-]+")
The same can be achieved using the schema value syntax, e.g.
.WithParameter(queryStringParameterExpectationBuilder => queryStringParameterExpectationBuilder
.WithName("[A-z]{0,10}")
.WithValue(SchemaValue.StringWithPattern("[A-Z0-9\\-]+"))
This would produce a call to the MockServer REST API as follows:
"queryStringParameters": {
"[A-z]{0,10}": [{
"schema": {
"type": "string",
"pattern": "[A-Z0-9\\-]+"
}
}]
}
Configuring Response Headers
Response headers can be configured as part of setting up a mock. Multiple headers and multiple values per header can be added.
For example, a simple static header collection can be added as follows
new MockServerClient(httpClient)
.When(RequestBuilder.Build()
.WithMethod(HttpMethod.Get)
.WithPath("http://mockserver/test"))
.Respond(
responseBuilder => responseBuilder
.WithStatusCode(200)
.WithHeaders(new Dictionary<string, IEnumerable<string>>
{ { "HeaderA", new[] { "HeaderValueA", "HeaderValueB" } } }));
Headers can also be added via the builder - For example the code below sets up a header named HeaderA
with values ValueA
and ValueB
. It also adds a header named HeaderB
with a value derived from the request.
new MockServerClient(httpClient)
.When(RequestBuilder.Build()
.WithMethod(HttpMethod.Get)
.WithPath("http://mypath"))
.Respond(
responseBuilder => responseBuilder
.WithStatusCode(200)
.WithHeaders(headersResponseBuilder => headersResponseBuilder
.WithHeader(headerResponseBuilder => headerResponseBuilder
.WithName("HeaderA")
.WithValues(headerValueResponseBuilder => headerValueResponseBuilder
.WithValue("ValueA")
.WithValue("ValueB")))
.WithHeader(headerResponseBuilder => headerResponseBuilder
.WithName("HeaderB")
.WithValues(headerValueResponseBuilder => headerValueResponseBuilder
.WithValue(request => request.Path)))
));
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.ComponentModel.Annotations (>= 5.0.0)
- System.Text.Json (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 163 | 3/9/2024 |