Compare commits

..

15 Commits

Author SHA1 Message Date
Stef Heyenrath
a7d283e12b 1.4.29 (release notes) 2021-12-12 15:53:08 +01:00
Stef Heyenrath
5280970765 1.4.29 2021-12-12 15:51:56 +01:00
Stef Heyenrath
6943b90da6 RegexExtended in settings (#700)
* Add extra unittest for RegexExtended

* settings
2021-12-12 15:40:38 +01:00
Michael Brogdon
4a434b5dba GUID Pattern support in RegexMatcher (#699)
* Add the RegexGuid class

* Use of RegexGuid in the RegexMatcher

* Fix up the CodeFactor violations

* Rename RegexGuid --> RegexExtended
2021-12-11 10:57:15 +01:00
Stef Heyenrath
3dafd2e725 update WireMockServer_Should_delay_responses_for_a_given_route 2021-12-08 21:56:26 +01:00
Stef Heyenrath
c6e4608039 1.4.28 2021-12-01 18:51:39 +01:00
Stef Heyenrath
57f89a06e1 Update WireMockOpenApiParserSettings 2021-11-29 08:40:39 +01:00
Daniel L. Romero
4d80eb53fe Allow configure IgnoreCase in settings (#695)
* Add IgnoreCase = true in Request body, query parameters, headers, example value

* Ignorecase is configurable in settings!

* Remove unnecesary comments!
2021-11-29 08:25:15 +01:00
Daniel L. Romero
13c002fede Filter required properti in headers, query params, request body (#696) 2021-11-27 09:44:24 +01:00
mcheguini
9db6e800ad RamlToOpenAPI updated to 0.5.0 (#694)
Co-authored-by: Mazeyar <mcheguini@valley.com>
2021-11-26 20:53:16 +01:00
Stef Heyenrath
897ee9ffe3 Update the OpenApiPathsMapper to handle Value/Wildcard (#691) 2021-11-23 08:09:38 +01:00
Stef Heyenrath
8865543bf1 some code refactorings for WireMock.Net.OpenApiParser 2021-11-21 10:07:27 +01:00
Stef Heyenrath
48cfd2d20e Upgrade some NuGet packages (Codecov, coverlet and NFluent) (#689) 2021-11-19 11:42:48 +01:00
dependabot[bot]
71d2660aff Bump System.Text.Encodings.Web (#688)
Bumps [System.Text.Encodings.Web](https://github.com/dotnet/corefx) from 4.5.0 to 4.5.1.
- [Release notes](https://github.com/dotnet/corefx/releases)
- [Commits](https://github.com/dotnet/corefx/commits)

---
updated-dependencies:
- dependency-name: System.Text.Encodings.Web
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-11-19 10:49:24 +01:00
Snyk bot
7f2a42de96 fix: src/WireMock.Net/WireMock.Net.csproj to reduce vulnerabilities (#686)
The following vulnerabilities are fixed with an upgrade:
- https://snyk.io/vuln/SNYK-DOTNET-MICROSOFTOWIN-1019387
2021-11-19 10:37:09 +01:00
32 changed files with 406 additions and 119 deletions

View File

@@ -1,3 +1,18 @@
# 1.4.29 (12 December 2021)
- [#699](https://github.com/WireMock-Net/WireMock.Net/pull/699) - GUID Pattern support in RegexMatcher contributed by [brogdogg](https://github.com/brogdogg)
- [#700](https://github.com/WireMock-Net/WireMock.Net/pull/700) - RegexExtended in settings [feature] contributed by [StefH](https://github.com/StefH)
# 1.4.28 (01 December 2021)
- [#686](https://github.com/WireMock-Net/WireMock.Net/pull/686) - [Snyk] Security upgrade Microsoft.Owin from 4.0.0 to 4.1.1 [dependencies] contributed by [snyk-bot](https://github.com/snyk-bot)
- [#688](https://github.com/WireMock-Net/WireMock.Net/pull/688) - Bump System.Text.Encodings.Web from 4.5.0 to 4.5.1 in /examples/WireMock.Net.Console.Net472.Classic [dependencies] contributed by [dependabot[bot]](https://github.com/apps/dependabot)
- [#689](https://github.com/WireMock-Net/WireMock.Net/pull/689) - Upgrade some NuGet's (Codecov, coverlet, Moq and NFluent) [dependencies] contributed by [StefH](https://github.com/StefH)
- [#691](https://github.com/WireMock-Net/WireMock.Net/pull/691) - Update the OpenApiPathsMapper to handle Value/Wildcard [feature] contributed by [StefH](https://github.com/StefH)
- [#694](https://github.com/WireMock-Net/WireMock.Net/pull/694) - RamlToOpenAPI updated to 0.5.0 [feature] contributed by [mcheguini](https://github.com/mcheguini)
- [#695](https://github.com/WireMock-Net/WireMock.Net/pull/695) - Allow configure IgnoreCase in settings [feature] contributed by [leolplex](https://github.com/leolplex)
- [#696](https://github.com/WireMock-Net/WireMock.Net/pull/696) - Filter required property in headers, query params, request body [feature] contributed by [leolplex](https://github.com/leolplex)
- [#666](https://github.com/WireMock-Net/WireMock.Net/issues/666) - Example is not working as expected [bug]
- [#692](https://github.com/WireMock-Net/WireMock.Net/issues/692) - Case insensitive and ignoring optional path and header parameters in OpenApiPathsMapper [feature]
# 1.4.27 (17 November 2021)
- [#678](https://github.com/WireMock-Net/WireMock.Net/pull/678) - Support RequestBody [feature] contributed by [leolplex](https://github.com/leolplex)
- [#680](https://github.com/WireMock-Net/WireMock.Net/pull/680) - Support examples in properties [feature] contributed by [leolplex](https://github.com/leolplex)
@@ -136,6 +151,7 @@
- [#549](https://github.com/WireMock-Net/WireMock.Net/issues/549) - WithProxy(...) does not save the mappings to file [bug]
# 1.3.8 (03 December 2020)
- [#539](https://github.com/WireMock-Net/WireMock.Net/pull/539) - Support for partial JSON matching contributed by [gleb-osokin](https://github.com/gleb-osokin)
- [#542](https://github.com/WireMock-Net/WireMock.Net/pull/542) - Create dotnet-wiremock tool [feature] contributed by [StefH](https://github.com/StefH)
- [#543](https://github.com/WireMock-Net/WireMock.Net/pull/543) - Add support for .NET 5 [feature] contributed by [StefH](https://github.com/StefH)
- [#544](https://github.com/WireMock-Net/WireMock.Net/pull/544) - Use Java 11 in Azure Pipelines (needed for SonarCloud) [feature] contributed by [StefH](https://github.com/StefH)
@@ -143,9 +159,6 @@
- [#547](https://github.com/WireMock-Net/WireMock.Net/pull/547) - Fix Proxying with SSL and NetCoreApp3.1 [bug] contributed by [StefH](https://github.com/StefH)
- [#524](https://github.com/WireMock-Net/WireMock.Net/issues/524) - Proxying with SSL Not Working in .NET Core 3.1 [bug]
# 1.3.7 (17 November 2020)
- [#539](https://github.com/WireMock-Net/WireMock.Net/pull/539) - Support for partial JSON matching contributed by [gleb-osokin](https://github.com/gleb-osokin)
# 1.3.6 (10 November 2020)
- [#529](https://github.com/WireMock-Net/WireMock.Net/pull/529) - Add assertions for ClientIP, Url and ProxyUrl [feature] contributed by [akamud](https://github.com/akamud)
- [#535](https://github.com/WireMock-Net/WireMock.Net/pull/535) - WithCallback should use also use enum HttpStatusCode [bug] contributed by [StefH](https://github.com/StefH)

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.4.27</VersionPrefix>
<VersionPrefix>1.4.29</VersionPrefix>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>

View File

@@ -1,6 +1,6 @@
rem https://github.com/StefH/GitHubReleaseNotes
SET version=1.4.27
SET version=1.4.29
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate --version %version% --token %GH_TOKEN%

View File

@@ -1,6 +1,5 @@
# 1.4.27 (17 November 2021)
- #678 Support RequestBody [feature]
- #680 Support examples in properties [feature]
- #681 Support enums in properties [feature]
# 1.4.29 (12 December 2021)
- #699 GUID Pattern support in RegexMatcher
- #700 RegexExtended in settings [feature]
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -74,7 +74,7 @@
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net472" />
<package id="System.Security.Cryptography.Cng" version="4.5.0" targetFramework="net472" />
<package id="System.Security.Principal.Windows" version="4.5.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.5.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="4.5.1" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
<package id="WireMock.Net" version="1.4.2" targetFramework="net472" />

View File

@@ -8,11 +8,11 @@ namespace WireMock.Net.OpenApiParser.ConsoleApp
private const string Folder = "OpenApiFiles";
static void Main(string[] args)
{
var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "http://localhost:9091/");
var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "http://localhost:9092/");
var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "http://localhost:9093/");
var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "http://localhost:9094/");
var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "http://localhost:9095/");
var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "https://localhost:9091/");
var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "https://localhost:9092/");
var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "https://localhost:9093/");
var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "https://localhost:9094/");
var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "https://localhost:9095/");
Console.WriteLine("Press any key to stop the servers");
Console.ReadKey();

View File

@@ -13,7 +13,7 @@ namespace WireMock.Net.OpenApiParser.ConsoleApp
{
public static class Run
{
public static WireMockServer RunServer(string path, string url)
public static WireMockServer RunServer(string path, string url, bool dynamicExamples = true)
{
var server = WireMockServer.Start(new WireMockServerSettings
{
@@ -31,7 +31,9 @@ namespace WireMock.Net.OpenApiParser.ConsoleApp
var settings = new WireMockOpenApiParserSettings
{
PathPatternToUse = ExampleValueType.Wildcard
DynamicExamples = dynamicExamples,
PathPatternToUse = ExampleValueType.Wildcard,
HeaderPatternToUse = ExampleValueType.Wildcard
};
server.WithMappingFromOpenApiFile(path, settings, out var diag);

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>

View File

@@ -2,6 +2,5 @@
<configuration>
<packageSources>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="coverlet" value="https://f.feedz.io/marcorossignoli/coverletunofficial/nuget/index.json" />
</packageSources>
</configuration>

View File

@@ -33,7 +33,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<!-- See also https://mstack.nl/blog/20210801-source-generators -->

View File

@@ -29,7 +29,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

View File

@@ -31,7 +31,7 @@
<Target Name="CheckIfShouldKillVBCSCompiler" />
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />

View File

@@ -0,0 +1,21 @@
#if NET46 || NETSTANDARD2_0
using System.Collections.Generic;
namespace WireMock.Net.OpenApiParser.Extensions
{
internal static class DictionaryExtensions
{
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary is null || dictionary.ContainsKey(key))
{
return false;
}
dictionary[key] = value;
return true;
}
}
}
#endif

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -8,6 +8,7 @@ using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Writers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stef.Validation;
using WireMock.Admin.Mappings;
using WireMock.Net.OpenApiParser.Extensions;
using WireMock.Net.OpenApiParser.Settings;
@@ -18,12 +19,14 @@ namespace WireMock.Net.OpenApiParser.Mappers
{
internal class OpenApiPathsMapper
{
private const string HeaderContentType = "Content-Type";
private readonly WireMockOpenApiParserSettings _settings;
private readonly ExampleValueGenerator _exampleValueGenerator;
public OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
_settings = Guard.NotNull(settings, nameof(settings));
_exampleValueGenerator = new ExampleValueGenerator(settings);
}
@@ -60,7 +63,7 @@ namespace WireMock.Net.OpenApiParser.Mappers
MapSchemaToObject(responseSchema);
var requestBodyModel = new BodyModel();
if (operation.RequestBody != null && operation.RequestBody.Content != null)
if (operation.RequestBody != null && operation.RequestBody.Content != null && operation.RequestBody.Required)
{
var request = operation.RequestBody.Content;
TryGetContent(request, out OpenApiMediaType requestContent, out string requestContentType);
@@ -108,11 +111,15 @@ namespace WireMock.Net.OpenApiParser.Mappers
return null;
}
var requestBodyModel = new BodyModel();
requestBodyModel.Matcher = new MatcherModel();
requestBodyModel.Matcher.Name = "JsonMatcher";
requestBodyModel.Matcher.Pattern = JsonConvert.SerializeObject(requestBody, Formatting.Indented);
return requestBodyModel;
return new BodyModel
{
Matcher = new MatcherModel
{
Name = "JsonMatcher",
Pattern = JsonConvert.SerializeObject(requestBody, Formatting.Indented),
IgnoreCase = _settings.RequestBodyIgnoreCase
}
};
}
private bool TryGetContent(IDictionary<string, OpenApiMediaType> contents, out OpenApiMediaType openApiMediaType, out string contentType)
@@ -243,10 +250,11 @@ namespace WireMock.Net.OpenApiParser.Mappers
}
else
{
bool propertyIsNullable = openApiSchema.Nullable || (openApiSchema.TryGetXNullable(out bool x) && x);
// bool propertyIsNullable = openApiSchema.Nullable || (openApiSchema.TryGetXNullable(out bool x) && x);
return new JProperty(key, _exampleValueGenerator.GetExampleValue(openApiSchema));
}
}
private string MapPathWithParameters(string path, IEnumerable<OpenApiParameter> parameters)
{
if (parameters == null)
@@ -257,7 +265,8 @@ namespace WireMock.Net.OpenApiParser.Mappers
string newPath = path;
foreach (var parameter in parameters)
{
newPath = newPath.Replace($"{{{parameter.Name}}}", GetExampleValue(parameter.Schema, _settings.PathPatternToUse));
var exampleMatcherModel = GetExampleMatcherModel(parameter.Schema, _settings.PathPatternToUse);
newPath = newPath.Replace($"{{{parameter.Name}}}", exampleMatcherModel.Pattern as string);
}
return newPath;
@@ -271,11 +280,11 @@ namespace WireMock.Net.OpenApiParser.Mappers
}
OpenApiServer server = servers.First();
Uri uriResult;
if (Uri.TryCreate(server.Url, UriKind.RelativeOrAbsolute, out uriResult))
if (Uri.TryCreate(server.Url, UriKind.RelativeOrAbsolute, out Uri uriResult))
{
return uriResult.IsAbsoluteUri ? uriResult.AbsolutePath : uriResult.ToString();
}
return string.Empty;
}
@@ -304,19 +313,12 @@ namespace WireMock.Net.OpenApiParser.Mappers
{
var mappedHeaders = headers.ToDictionary(
item => item.Key,
item => GetExampleValue(null, _settings.HeaderPatternToUse) as object
item => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern
);
if (!string.IsNullOrEmpty(responseContentType))
{
if (!mappedHeaders.ContainsKey("Content-Type"))
{
mappedHeaders.Add("Content-Type", responseContentType);
}
else
{
mappedHeaders["Content-Type"] = responseContentType;
}
mappedHeaders.TryAdd(HeaderContentType, responseContentType);
}
return mappedHeaders.Keys.Any() ? mappedHeaders : null;
@@ -325,16 +327,14 @@ namespace WireMock.Net.OpenApiParser.Mappers
private IList<ParamModel> MapQueryParameters(IEnumerable<OpenApiParameter> queryParameters)
{
var list = queryParameters
.Where(req => req.Required)
.Select(qp => new ParamModel
{
Name = qp.Name,
IgnoreCase = _settings.QueryParameterPatternIgnoreCase,
Matchers = new[]
{
new MatcherModel
{
Name = "ExactMatcher",
Pattern = GetDefaultValueAsStringForSchemaType(qp.Schema)
}
GetExampleMatcherModel(qp.Schema, _settings.QueryParameterPatternToUse)
}
})
.ToList();
@@ -345,16 +345,14 @@ namespace WireMock.Net.OpenApiParser.Mappers
private IList<HeaderModel> MapRequestHeaders(IEnumerable<OpenApiParameter> headers)
{
var list = headers
.Where(req => req.Required)
.Select(qp => new HeaderModel
{
Name = qp.Name,
IgnoreCase = _settings.HeaderPatternIgnoreCase,
Matchers = new[]
{
new MatcherModel
{
Name = "ExactMatcher",
Pattern = GetDefaultValueAsStringForSchemaType(qp.Schema)
}
GetExampleMatcherModel(qp.Schema, _settings.HeaderPatternToUse)
}
})
.ToList();
@@ -362,30 +360,26 @@ namespace WireMock.Net.OpenApiParser.Mappers
return list.Any() ? list : null;
}
private string GetDefaultValueAsStringForSchemaType(OpenApiSchema schema)
private MatcherModel GetExampleMatcherModel(OpenApiSchema schema, ExampleValueType type)
{
return type switch
{
ExampleValueType.Value => new MatcherModel { Name = "ExactMatcher", Pattern = GetExampleValueAsStringForSchemaType(schema), IgnoreCase = _settings.IgnoreCaseExampleValues },
_ => new MatcherModel { Name = "WildcardMatcher", Pattern = "*" }
};
}
private string GetExampleValueAsStringForSchemaType(OpenApiSchema schema)
{
var value = _exampleValueGenerator.GetExampleValue(schema);
switch (value)
return value switch
{
case string valueAsString:
return valueAsString;
string valueAsString => valueAsString,
default:
return value.ToString();
}
}
private string GetExampleValue(OpenApiSchema schema, ExampleValueType type)
{
switch (type)
{
case ExampleValueType.Value:
return GetDefaultValueAsStringForSchemaType(schema);
default:
return "*";
}
_ => value.ToString(),
};
}
}
}

View File

@@ -1,4 +1,4 @@
using WireMock.Net.OpenApiParser.Types;
using WireMock.Net.OpenApiParser.Types;
namespace WireMock.Net.OpenApiParser.Settings
{
@@ -23,12 +23,41 @@ namespace WireMock.Net.OpenApiParser.Settings
public ExampleValueType HeaderPatternToUse { get; set; } = ExampleValueType.Value;
/// <summary>
/// The example values to use
/// The example value type to use when generating a Query Parameter
/// </summary>
public IWireMockOpenApiParserExampleValues ExampleValues { get; set; } = new WireMockOpenApiParserExampleValues();
public ExampleValueType QueryParameterPatternToUse { get; set; } = ExampleValueType.Value;
/// <summary>
/// Are examples generated dynamically?
/// The example values to use.
///
/// Default implementations are:
/// - <see cref="WireMockOpenApiParserExampleValues"/>
/// - <see cref="WireMockOpenApiParserDynamicExampleValues"/>
/// </summary>
public IWireMockOpenApiParserExampleValues ExampleValues { get; set; }
/// <summary>
/// Is a Header match case insensitive? (default is true).
/// </summary>
public bool HeaderPatternIgnoreCase { get; set; } = true;
/// <summary>
/// Is a Query Parameter match case insensitive? (default is true).
/// </summary>
public bool QueryParameterPatternIgnoreCase { get; set; } = true;
/// <summary>
/// Is a Request Body match case insensitive? (default is true).
/// </summary>
public bool RequestBodyIgnoreCase { get; set; } = true;
/// <summary>
/// Is a ExampleValue match case insensitive? (default is true).
/// </summary>
public bool IgnoreCaseExampleValues { get; set; } = true;
/// <summary>
/// Are examples generated dynamically? (default is false).
/// </summary>
public bool DynamicExamples { get; set; } = false;
}

View File

@@ -1,4 +1,4 @@
namespace WireMock.Net.OpenApiParser.Types
namespace WireMock.Net.OpenApiParser.Types
{
/// <summary>
/// The example value to use
@@ -6,7 +6,9 @@
public enum ExampleValueType
{
/// <summary>
/// Use a generated example value based on the SchemaType (default).
/// 1. Use a generated example value based on the SchemaType (default).
/// 2. If there is no example value defined in the schema,
/// then the <see cref="Settings.IWireMockOpenApiParserExampleValues"/> will be used (custom, fixed or dynamic).
/// </summary>
Value,

View File

@@ -17,13 +17,17 @@ namespace WireMock.Net.OpenApiParser.Utils
{
_settings = Guard.NotNull(settings, nameof(settings));
if (_settings.DynamicExamples)
// Check if user provided an own implementation
if (settings.ExampleValues is null)
{
_settings.ExampleValues = new WireMockOpenApiParserDynamicExampleValues();
}
else
{
_settings.ExampleValues = new WireMockOpenApiParserExampleValues();
if (_settings.DynamicExamples)
{
_settings.ExampleValues = new WireMockOpenApiParserDynamicExampleValues();
}
else
{
_settings.ExampleValues = new WireMockOpenApiParserExampleValues();
}
}
}
@@ -105,6 +109,7 @@ namespace WireMock.Net.OpenApiParser.Utils
}
}
}
private static IOpenApiAny GetRandomEnumValue(IList<IOpenApiAny> schemaEnum)
{
if (schemaEnum?.Count > 0)
@@ -113,7 +118,8 @@ namespace WireMock.Net.OpenApiParser.Utils
int randomEnum = new Random().Next(0, maxValue);
return schemaEnum[randomEnum];
}
return null;
}
}
}
}

View File

@@ -22,8 +22,8 @@
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.2.3" />
<PackageReference Include="RamlToOpenApiConverter" Version="0.4.3" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="RamlToOpenApiConverter" Version="0.6.0" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.13" />
<PackageReference Include="Stef.Validation" Version="0.0.4" />

View File

@@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="7.8.0.7320">
<PrivateAssets>all</PrivateAssets>

View File

@@ -5,6 +5,7 @@ using AnyOfTypes;
using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
using WireMock.RegularExpressions;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -30,7 +31,10 @@ namespace WireMock.Matchers
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher([NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="useRegexExtended">Use RegexExtended (default = true).</param>
public RegexMatcher([NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) :
this(MatchBehaviour.AcceptOnMatch, new[] { pattern }, ignoreCase, throwException, useRegexExtended)
{
}
@@ -40,16 +44,10 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher([NotNull, RegexPattern] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="useRegexExtended">Use RegexExtended (default = true).</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern> pattern, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true) :
this(matchBehaviour, new[] { pattern }, ignoreCase, throwException, useRegexExtended)
{
}
@@ -60,7 +58,8 @@ namespace WireMock.Matchers
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false, bool throwException = false)
/// <param name="useRegexExtended">Use RegexExtended (default = true).</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false, bool throwException = false, bool useRegexExtended = true)
{
Check.NotNull(patterns, nameof(patterns));
@@ -76,7 +75,7 @@ namespace WireMock.Matchers
options |= RegexOptions.IgnoreCase;
}
_expressions = patterns.Select(p => new Regex(p.GetPattern(), options)).ToArray();
_expressions = patterns.Select(p => useRegexExtended ? new RegexExtended(p.GetPattern(), options) : new Regex(p.GetPattern(), options)).ToArray();
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>

View File

@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using WireMock.Validation;
namespace WireMock.RegularExpressions
{
/// <summary>
/// Extension to the <see cref="Regex"/> object, adding support for GUID tokens for matching on.
/// </summary>
public class RegexExtended : Regex
{
/// <inheritdoc cref="Regex"/>
public RegexExtended(string pattern) : this(pattern, RegexOptions.None)
{
}
/// <inheritdoc cref="Regex"/>
public RegexExtended(string pattern, RegexOptions options)
: this(pattern, options, Regex.InfiniteMatchTimeout)
{
}
/// <inheritdoc cref="Regex"/>
public RegexExtended(string pattern, RegexOptions options, TimeSpan matchTimeout)
: base(ReplaceGuidPattern(pattern), options, matchTimeout)
{
}
// Dictionary of various Guid tokens with a corresponding regular expression pattern to use instead.
private static readonly Dictionary<string, string> GuidTokenPatterns = new Dictionary<string, string>
{
// Lower case format `B` Guid pattern
{ @"\guidb", @"(\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\})" },
// Upper case format `B` Guid pattern
{ @"\GUIDB", @"(\{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\})" },
// Lower case format `D` Guid pattern
{ @"\guidd", "([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12})" },
// Upper case format `D` Guid pattern
{ @"\GUIDD", "([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12})" },
// Lower case format `N` Guid pattern
{ @"\guidn", "([a-z0-9]{32})" },
// Upper case format `N` Guid pattern
{ @"\GUIDN", "([A-Z0-9]{32})" },
// Lower case format `P` Guid pattern
{ @"\guidp", @"(\([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\))" },
// Upper case format `P` Guid pattern
{ @"\GUIDP", @"(\([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\))" },
// Lower case format `X` Guid pattern
{ @"\guidx", @"(\{0x[a-f0-9]{8},0x[a-f0-9]{4},0x[a-f0-9]{4},\{(0x[a-f0-9]{2},){7}(0x[a-f0-9]{2})\}\})" },
// Upper case format `X` Guid pattern
{ @"\GUIDX", @"(\{0x[A-F0-9]{8},0x[A-F0-9]{4},0x[A-F0-9]{4},\{(0x[A-F0-9]{2},){7}(0x[A-F0-9]{2})\}\})" },
};
/// <summary>
/// Replaces all instances of valid GUID tokens with the correct regular expression to match.
/// </summary>
/// <param name="pattern">Pattern to replace token for.</param>
private static string ReplaceGuidPattern(string pattern)
{
Check.NotNull(pattern, nameof(pattern));
foreach (var tokenPattern in GuidTokenPatterns)
{
pattern = pattern.Replace(tokenPattern.Key, tokenPattern.Value);
}
return pattern;
}
}
}

View File

@@ -41,6 +41,7 @@ namespace WireMock.Serialization
var matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
bool ignoreCase = matcher.IgnoreCase == true;
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
bool useRegexExtended = _settings.UseRegexExtended == true;
switch (matcherName)
{
@@ -65,7 +66,7 @@ namespace WireMock.Serialization
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0], throwExceptionWhenMatcherFails);
case nameof(RegexMatcher):
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails, useRegexExtended);
case nameof(JsonMatcher):
object valueForJsonMatcher = matcher.Pattern ?? matcher.Patterns;

View File

@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using WireMock.Matchers;
@@ -9,7 +9,7 @@ namespace WireMock.Server
{
public partial class WireMockServer
{
private readonly RegexMatcher _adminFilesFilenamePathMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, @"^\/__admin\/files\/.*$");
private readonly RegexMatcher _adminFilesFilenamePathMatcher = new RegexMatcher(@"^\/__admin\/files\/.*$");
private static readonly Encoding[] FileBodyIsString = { Encoding.UTF8, Encoding.ASCII };
#region Files/{filename}

View File

@@ -1,9 +1,11 @@
using System;
using System.Text.RegularExpressions;
using HandlebarsDotNet;
using JetBrains.Annotations;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.RegularExpressions;
#if USE_ASPNETCORE
using Microsoft.Extensions.DependencyInjection;
#endif
@@ -215,5 +217,11 @@ namespace WireMock.Settings
/// </summary>
[PublicAPI]
IWebhookSettings WebhookSettings { get; set; }
/// <summary>
/// Use the <see cref="RegexExtended"/> instead of the default <see cref="Regex"/>.
/// </summary>
[PublicAPI]
bool? UseRegexExtended { get; }
}
}

View File

@@ -151,5 +151,9 @@ namespace WireMock.Settings
/// <inheritdoc cref="IWireMockServerSettings.WebhookSettings"/>
[PublicAPI]
public IWebhookSettings WebhookSettings { get; set; }
/// <inheritdoc cref="IWireMockServerSettings.UseRegexExtended"/>
[PublicAPI]
public bool? UseRegexExtended { get; set; } = true;
}
}

View File

@@ -48,7 +48,8 @@ namespace WireMock.Settings
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"),
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails")
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails"),
UseRegexExtended = parser.GetBoolValue(nameof(IWireMockServerSettings.UseRegexExtended), true)
};
if (logger != null)

View File

@@ -51,7 +51,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
@@ -88,7 +88,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.6" />
<PackageReference Include="Microsoft.Owin" Version="4.0.0" />
<PackageReference Include="Microsoft.Owin" Version="4.1.1" />
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="4.0.0" />
<PackageReference Include="Microsoft.Owin.Hosting" Version="4.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />

View File

@@ -34,7 +34,7 @@
<ItemGroup>
<!--<PackageReference Include="AnyOf" Version="0.1.0" />-->
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" PrivateAssets="All" />
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

View File

@@ -1,3 +1,5 @@
using System;
using FluentAssertions;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -70,6 +72,32 @@ namespace WireMock.Net.Tests.Matchers
Check.That(result).IsEqualTo(0.0d);
}
[Fact]
public void RegexMatcher_IsMatch_RegexExtended_Guid()
{
// Assign
var matcher = new RegexMatcher(@"\GUIDB", true);
// Act
double result = matcher.IsMatch(Guid.NewGuid().ToString("B"));
// Assert
result.Should().Be(1.0);
}
[Fact]
public void RegexMatcher_IsMatch_Regex_Guid()
{
// Assign
var matcher = new RegexMatcher(@"\GUIDB", true, false, false);
// Act
double result = matcher.IsMatch(Guid.NewGuid().ToString("B"));
// Assert
result.Should().Be(0);
}
[Fact]
public void RegexMatcher_IsMatch_IgnoreCase()
{

View File

@@ -0,0 +1,100 @@
using System;
using NFluent;
using WireMock.RegularExpressions;
using Xunit;
namespace WireMock.Net.Tests.RegularExpressions
{
public class RegexExtendedTests
{
/// <summary>
/// Input guid used for testing
/// </summary>
public Guid InputGuid => Guid.NewGuid();
[Fact]
public void RegexExtended_GuidB_Pattern()
{
var guidbUpper = @".*\GUIDB.*";
var guidbLower = @".*\guidb.*";
var inputLower = InputGuid.ToString("B");
var inputUpper = InputGuid.ToString("B").ToUpper();
var regexLower = new RegexExtended(guidbLower);
var regexUpper = new RegexExtended(guidbUpper);
Check.That(regexLower.IsMatch(inputLower)).Equals(true);
Check.That(regexLower.IsMatch(inputUpper)).Equals(false);
Check.That(regexUpper.IsMatch(inputUpper)).Equals(true);
Check.That(regexUpper.IsMatch(inputLower)).Equals(false);
}
[Fact]
public void RegexExtended_GuidD_Pattern()
{
var guiddUpper = @".*\GUIDD.*";
var guiddLower = @".*\guidd.*";
var inputLower = InputGuid.ToString("D");
var inputUpper = InputGuid.ToString("D").ToUpper();
var regexLower = new RegexExtended(guiddLower);
var regexUpper = new RegexExtended(guiddUpper);
Check.That(regexLower.IsMatch(inputLower)).Equals(true);
Check.That(regexLower.IsMatch(inputUpper)).Equals(false);
Check.That(regexUpper.IsMatch(inputUpper)).Equals(true);
Check.That(regexUpper.IsMatch(inputLower)).Equals(false);
}
[Fact]
public void RegexExtended_GuidN_Pattern()
{
var guidnUpper = @".*\GUIDN.*";
var guidnLower = @".*\guidn.*";
var inputLower = InputGuid.ToString("N");
var inputUpper = InputGuid.ToString("N").ToUpper();
var regexLower = new RegexExtended(guidnLower);
var regexUpper = new RegexExtended(guidnUpper);
Check.That(regexLower.IsMatch(inputLower)).Equals(true);
Check.That(regexLower.IsMatch(inputUpper)).Equals(false);
Check.That(regexUpper.IsMatch(inputUpper)).Equals(true);
Check.That(regexUpper.IsMatch(inputLower)).Equals(false);
}
[Fact]
public void RegexExtended_GuidP_Pattern()
{
var guidpUpper = @".*\GUIDP.*";
var guidpLower = @".*\guidp.*";
var inputLower = InputGuid.ToString("P");
var inputUpper = InputGuid.ToString("P").ToUpper();
var regexLower = new RegexExtended(guidpLower);
var regexUpper = new RegexExtended(guidpUpper);
Check.That(regexLower.IsMatch(inputLower)).Equals(true);
Check.That(regexLower.IsMatch(inputUpper)).Equals(false);
Check.That(regexUpper.IsMatch(inputUpper)).Equals(true);
Check.That(regexUpper.IsMatch(inputLower)).Equals(false);
}
[Fact]
public void RegexExtended_GuidX_Pattern()
{
var guidxUpper = @".*\GUIDX.*";
var guidxLower = @".*\guidx.*";
var inputLower = InputGuid.ToString("X");
var inputUpper = InputGuid.ToString("X").ToUpper().Replace("X", "x");
var regexLower = new RegexExtended(guidxLower);
var regexUpper = new RegexExtended(guidxUpper);
Check.That(regexLower.IsMatch(inputLower)).Equals(true);
Check.That(regexLower.IsMatch(inputUpper)).Equals(false);
Check.That(regexUpper.IsMatch(inputUpper)).Equals(true);
Check.That(regexUpper.IsMatch(inputLower)).Equals(false);
}
}
}

View File

@@ -32,8 +32,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Codecov" Version="1.10.0" />
<PackageReference Include="coverlet.msbuild" Version="3.0.2">
<PackageReference Include="Codecov" Version="1.13.0" />
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@@ -44,18 +44,18 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.16.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="RestEase" Version="1.5.5" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NFluent" Version="2.7.1" />
<PackageReference Include="NFluent" Version="2.7.2" />
<!--<PackageReference Include="OpenCover" Version="4.7.922" />-->
<!--<PackageReference Include="ReportGenerator" Version="4.8.1" />-->
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
@@ -6,6 +6,7 @@ using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using NFluent;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
@@ -93,7 +94,7 @@ namespace WireMock.Net.Tests
[Fact]
public async Task WireMockServer_Should_delay_responses_for_a_given_route()
{
// given
// Arrange
var server = WireMockServer.Start();
server
@@ -103,14 +104,14 @@ namespace WireMock.Net.Tests
.WithBody(@"{ msg: ""Hello world!""}")
.WithDelay(TimeSpan.FromMilliseconds(200)));
// when
// Act
var watch = new Stopwatch();
watch.Start();
await new HttpClient().GetStringAsync("http://localhost:" + server.Ports[0] + "/foo");
watch.Stop();
// then
Check.That(watch.ElapsedMilliseconds).IsStrictlyGreaterThan(200);
// Asser.
watch.ElapsedMilliseconds.Should().BeGreaterOrEqualTo(200);
server.Stop();
}