diff --git a/examples/WireMock.Net.Client/Program.cs b/examples/WireMock.Net.Client/Program.cs index 1331fc4bc..eb0a9a4d6 100644 --- a/examples/WireMock.Net.Client/Program.cs +++ b/examples/WireMock.Net.Client/Program.cs @@ -14,10 +14,13 @@ class Program { static async Task Main(string[] args) { + // Start WireMock.Net tool with Admin interface + // dotnet-wiremock --StartAdminInterface + // Create an implementation of the IWireMockAdminApi and pass in the base URL for the API. var api = RestClient.For("http://localhost:9091"); - // await api.ResetMappingsAsync().ConfigureAwait(false); + await api.ResetMappingsAsync().ConfigureAwait(false); var mappingBuilder = api.GetMappingBuilder(); mappingBuilder.Given(m => m @@ -51,13 +54,32 @@ static async Task Main(string[] args) .WithPath("/bla3") ) .WithResponse(rsp => rsp - .WithBodyAsJson(new + .WithBodyAsJson(new { x = "test" }, true) ) ); + mappingBuilder.Given(m => m + .WithRequest(req => req + .WithPath("/test1") + .UsingPost() + .WithBody(b => b + .WithJmesPathMatcher("things.name == 'RequiredThing'") + ) + ) + .WithResponse(rsp => rsp + .WithHeaders(h => h.Add("Content-Type", "application/json")) + .WithDelay(TimeSpan.FromMilliseconds(50)) + .WithStatusCode(200) + .WithBodyAsJson(new + { + status = "ok" + }, true) + ) + ); + var result = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false); Console.WriteLine($"result = {JsonConvert.SerializeObject(result)}"); @@ -112,6 +134,9 @@ static async Task Main(string[] args) var getFileResult = await api.GetFileAsync("1.cs"); Console.WriteLine($"getFileResult = {getFileResult}"); + Console.WriteLine("Press any key to reset mappings"); + Console.ReadKey(); + var resetMappingsAsync = await api.ResetMappingsAsync(); Console.WriteLine($"resetMappingsAsync = {resetMappingsAsync.Status}"); diff --git a/examples/WireMock.Net.Client/Properties/launchSettings.json b/examples/WireMock.Net.Client/Properties/launchSettings.json deleted file mode 100644 index 33504c948..000000000 --- a/examples/WireMock.Net.Client/Properties/launchSettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "profiles": { - "WSL": { - "commandName": "WSL2", - "distributionName": "" - } - } -} \ No newline at end of file diff --git a/examples/WireMock.Net.Client/WireMock.Net.Client.csproj b/examples/WireMock.Net.Client/WireMock.Net.Client.csproj index 5515959f0..241aaec50 100644 --- a/examples/WireMock.Net.Client/WireMock.Net.Client.csproj +++ b/examples/WireMock.Net.Client/WireMock.Net.Client.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 ../../resources/WireMock.Net-Logo.ico diff --git a/src/WireMock.Net.Abstractions/BuilderExtensions/BodyModelBuilder.cs b/src/WireMock.Net.Abstractions/BuilderExtensions/BodyModelBuilder.cs new file mode 100644 index 000000000..a4ee1e801 --- /dev/null +++ b/src/WireMock.Net.Abstractions/BuilderExtensions/BodyModelBuilder.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; + +// ReSharper disable once CheckNamespace +namespace WireMock.Admin.Mappings; + +/// +/// BodyModelBuilder +/// +public partial class BodyModelBuilder +{ + public BodyModelBuilder WithNotNullOrEmptyMatcher(bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("NotNullOrEmptyMatcher") + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithCSharpCodeMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher("CSharpCodeMatcher", pattern, rejectOnMatch); + } + + public BodyModelBuilder WithLinqMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher("LinqMatcher", pattern, rejectOnMatch); + } + + public BodyModelBuilder WithExactMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher("ExactMatcher", pattern, rejectOnMatch); + } + + public BodyModelBuilder WithExactObjectMatcher(object value, bool rejectOnMatch = false) + { + return WithMatcher("ExactObjectMatcher", value, rejectOnMatch); + } + + public BodyModelBuilder WithGraphQLMatcher(string pattern, IDictionary? customScalars = null, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("GraphQLMatcher") + .WithCustomScalars(customScalars) + .WithPattern(pattern) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithProtoBufMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("ProtoBufMatcher") + .WithPattern(pattern) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithRegexMatcher(string pattern, bool ignoreCase = false, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("RegexMatcher") + .WithPattern(pattern) + .WithIgnoreCase(ignoreCase) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithJsonMatcher(string pattern, bool ignoreCase = false, bool useRegex = false, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("JsonMatcher") + .WithPattern(pattern) + .WithIgnoreCase(ignoreCase) + .WithRegex(useRegex) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithJsonPartialMatcher(string pattern, bool ignoreCase = false, bool useRegex = false, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("JsonPartialMatcher") + .WithPattern(pattern) + .WithIgnoreCase(ignoreCase) + .WithRegex(useRegex) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithJsonPathMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher("JsonPathMatcher", pattern, rejectOnMatch); + } + + public BodyModelBuilder WithJmesPathMatcher(string pattern, bool rejectOnMatch = false) + { + return WithMatcher("JmesPathMatcher", pattern, rejectOnMatch); + } + + public BodyModelBuilder WithXPathMatcher(string pattern, XmlNamespace[]? xmlNamespaceMap = null, bool rejectOnMatch = false) + { + return WithMatcher(mb => mb + .WithName("PathMatcher") + .WithPattern(pattern) + .WithXmlNamespaceMap(xmlNamespaceMap) + .WithRejectOnMatch(rejectOnMatch) + ); + } + + public BodyModelBuilder WithWildcardMatcher(string pattern, bool ignoreCase = false, bool rejectOnMatch = false) + { + return WithMatcher("WildcardMatcher", pattern, rejectOnMatch, ignoreCase); + } + + public BodyModelBuilder WithSimMetricsMatcher(string pattern, bool ignoreCase = false, bool rejectOnMatch = false) + { + return WithMatcher("SimMetricsMatcher", pattern, rejectOnMatch, ignoreCase); + } + + private BodyModelBuilder WithMatcher(string name, object pattern, bool rejectOnMatch, bool ignoreCase = false) + { + return WithMatcher(mb => mb + .WithName(name) + .WithPattern(pattern) + .WithRejectOnMatch(rejectOnMatch) + .WithIgnoreCase(ignoreCase) + ); + } +} \ No newline at end of file