Compare commits

..

2 Commits

Author SHA1 Message Date
Stef Heyenrath
e208e0f6bf . 2024-07-29 19:51:47 +02:00
Stef Heyenrath
f18c2ce324 Use AppendGuidToSavedMappingFile in local proxy settings 2024-07-29 19:00:42 +02:00
7 changed files with 58 additions and 64 deletions

View File

@@ -45,7 +45,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" /> <PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' "> <ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">

View File

@@ -37,6 +37,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="8.0.0" /> <PackageReference Include="Aspire.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,7 +1,6 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using AnyOfTypes; using AnyOfTypes;
using Stef.Validation; using Stef.Validation;
using WireMock.Models; using WireMock.Models;
@@ -28,7 +27,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use. (default = "Or")</param> /// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
public FormUrlEncodedMatcher( public FormUrlEncodedMatcher(
AnyOf<string, StringPattern> pattern, AnyOf<string, StringPattern> pattern,
bool ignoreCase = false, bool ignoreCase = false,
@@ -43,7 +42,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use. (default = "Or")</param> /// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
public FormUrlEncodedMatcher( public FormUrlEncodedMatcher(
MatchBehaviour matchBehaviour, MatchBehaviour matchBehaviour,
AnyOf<string, StringPattern> pattern, AnyOf<string, StringPattern> pattern,
@@ -58,7 +57,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// </summary> /// </summary>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use. (default = "Or")</param> /// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
public FormUrlEncodedMatcher( public FormUrlEncodedMatcher(
AnyOf<string, StringPattern>[] patterns, AnyOf<string, StringPattern>[] patterns,
bool ignoreCase = false, bool ignoreCase = false,
@@ -73,7 +72,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
/// <param name="matchBehaviour">The match behaviour.</param> /// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param> /// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param> /// <param name="ignoreCase">Ignore the case from the pattern.</param>
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use. (default = "Or")</param> /// <param name="matchOperator">The <see cref="Matchers.MatchOperator"/> to use. (default = "Or")</param>
public FormUrlEncodedMatcher( public FormUrlEncodedMatcher(
MatchBehaviour matchBehaviour, MatchBehaviour matchBehaviour,
AnyOf<string, StringPattern>[] patterns, AnyOf<string, StringPattern>[] patterns,
@@ -113,20 +112,7 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
return new MatchResult(MatchScores.Mismatch); return new MatchResult(MatchScores.Mismatch);
} }
var matches = GetMatches(inputNameValueCollection);
var score = MatchScores.ToScore(matches, MatchOperator);
return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score));
}
private bool[] GetMatches(IDictionary<string, string> inputNameValueCollection)
{
var matches = new List<bool>(); var matches = new List<bool>();
if (_pairs.Count > inputNameValueCollection.Count)
{
matches.AddRange(Enumerable.Repeat(false, _pairs.Count - inputNameValueCollection.Count));
}
foreach (var inputKeyValuePair in inputNameValueCollection) foreach (var inputKeyValuePair in inputNameValueCollection)
{ {
var match = false; var match = false;
@@ -146,7 +132,8 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher
matches.Add(match); matches.Add(match);
} }
return matches.ToArray(); var score = MatchScores.ToScore(matches.ToArray(), MatchOperator);
return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score));
} }
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -10,28 +10,22 @@ namespace WireMock.Serialization;
/// <summary> /// <summary>
/// Creates sanitized file names for mappings /// Creates sanitized file names for mappings
/// </summary> /// </summary>
public class MappingFileNameSanitizer internal class MappingFileNameSanitizer
{ {
private const string SpaceChar = " ";
private const char ReplaceChar = '_'; private const char ReplaceChar = '_';
private readonly WireMockServerSettings _settings;
public MappingFileNameSanitizer(WireMockServerSettings settings)
{
_settings = Guard.NotNull(settings);
}
/// <summary> /// <summary>
/// Creates sanitized file names for mappings /// Creates sanitized file names for mappings
/// </summary> /// </summary>
public string BuildSanitizedFileName(IMapping mapping) public string BuildSanitizedFileName(IMapping mapping, ProxyAndRecordSettings? proxyAndRecordSettings)
{ {
string name; string name;
if (!string.IsNullOrEmpty(mapping.Title)) if (!string.IsNullOrEmpty(mapping.Title))
{ {
// remove 'Proxy Mapping for ' and an extra space character after the HTTP request method // remove 'Proxy Mapping for ' and an extra space character after the HTTP request method
name = mapping.Title.Replace(ProxyAndRecordSettings.DefaultPrefixForSavedMappingFile, "").Replace(' '.ToString(), string.Empty); name = mapping.Title!.Replace(ProxyAndRecordSettings.DefaultPrefixForSavedMappingFile, string.Empty).Replace(SpaceChar, string.Empty);
if (_settings.ProxyAndRecordSettings?.AppendGuidToSavedMappingFile == true) if (proxyAndRecordSettings?.AppendGuidToSavedMappingFile == true)
{ {
name += $"{ReplaceChar}{mapping.Guid}"; name += $"{ReplaceChar}{mapping.Guid}";
} }
@@ -41,10 +35,11 @@ public class MappingFileNameSanitizer
name = mapping.Guid.ToString(); name = mapping.Guid.ToString();
} }
if (!string.IsNullOrEmpty(_settings.ProxyAndRecordSettings?.PrefixForSavedMappingFile)) if (!string.IsNullOrEmpty(proxyAndRecordSettings?.PrefixForSavedMappingFile))
{ {
name = $"{_settings.ProxyAndRecordSettings.PrefixForSavedMappingFile}{ReplaceChar}{name}"; name = $"{proxyAndRecordSettings.PrefixForSavedMappingFile}{ReplaceChar}{name}";
} }
return $"{Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, ReplaceChar))}.json"; return $"{Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, ReplaceChar))}.json";
} }
} }

View File

@@ -1,24 +1,56 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using Riok.Mapperly.Abstractions; using Nelibur.ObjectMapper;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Admin.Settings; using WireMock.Admin.Settings;
using WireMock.Settings; using WireMock.Settings;
namespace WireMock.Util; namespace WireMock.Util;
[Mapper] internal sealed class TinyMapperUtils
internal static partial class TinyMapperUtils
{ {
public static partial ProxyAndRecordSettingsModel? Map(ProxyAndRecordSettings? instance); public static TinyMapperUtils Instance { get; } = new();
public static partial ProxyAndRecordSettings? Map(ProxyAndRecordSettingsModel? model); private TinyMapperUtils()
{
TinyMapper.Bind<ProxyAndRecordSettings, ProxyAndRecordSettingsModel>();
TinyMapper.Bind<WebProxySettings, WebProxySettingsModel>();
TinyMapper.Bind<WebProxySettings, WebProxyModel>();
TinyMapper.Bind<ProxyUrlReplaceSettings, ProxyUrlReplaceSettingsModel>();
public static partial ProxyUrlReplaceSettingsModel? Map(ProxyUrlReplaceSettings? instance); TinyMapper.Bind<ProxyAndRecordSettingsModel, ProxyAndRecordSettings>();
TinyMapper.Bind<WebProxySettingsModel, WebProxySettings>();
TinyMapper.Bind<WebProxyModel, WebProxySettings>();
TinyMapper.Bind<ProxyUrlReplaceSettingsModel, ProxyUrlReplaceSettings>();
}
public static partial ProxyUrlReplaceSettings? Map(ProxyUrlReplaceSettingsModel? model); public ProxyAndRecordSettingsModel? Map(ProxyAndRecordSettings? instance)
{
return instance == null ? null : TinyMapper.Map<ProxyAndRecordSettingsModel>(instance);
}
public static partial WebProxyModel? Map(WebProxySettings? instance); public ProxyAndRecordSettings? Map(ProxyAndRecordSettingsModel? model)
{
return model == null ? null : TinyMapper.Map<ProxyAndRecordSettings>(model);
}
public static partial WebProxySettings? Map(WebProxyModel? model); public ProxyUrlReplaceSettingsModel? Map(ProxyUrlReplaceSettings? instance)
{
return instance == null ? null : TinyMapper.Map<ProxyUrlReplaceSettingsModel>(instance);
}
public ProxyUrlReplaceSettings? Map(ProxyUrlReplaceSettingsModel? model)
{
return model == null ? null : TinyMapper.Map<ProxyUrlReplaceSettings>(model);
}
public WebProxyModel? Map(WebProxySettings? instance)
{
return instance == null ? null : TinyMapper.Map<WebProxyModel>(instance);
}
public WebProxySettings? Map(WebProxyModel? model)
{
return model == null ? null : TinyMapper.Map<WebProxySettings>(model);
}
} }

View File

@@ -63,10 +63,10 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" /> <PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" />
<PackageReference Include="NSwag.Core" Version="13.16.1" /> <PackageReference Include="NSwag.Core" Version="13.16.1" />
<PackageReference Include="Riok.Mapperly" Version="3.6.0" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" /> <PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="JmesPath.Net" Version="1.0.125" /> <PackageReference Include="JmesPath.Net" Version="1.0.125" />
<PackageReference Include="AnyOf" Version="0.3.0" /> <PackageReference Include="AnyOf" Version="0.3.0" />
<PackageReference Include="TinyMapper" Version="3.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' "> <ItemGroup Condition=" '$(TargetFramework)' != 'netstandard1.3' ">

View File

@@ -75,25 +75,4 @@ public class FormUrlEncodedMatcherTest
// Assert // Assert
score.Should().Be(expected); score.Should().Be(expected);
} }
[Fact]
public async Task FormUrlEncodedMatcher_IsMatch_And_MatchAllProperties()
{
// Arrange
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("name", "John Doe"),
new KeyValuePair<string, string>("email", "johndoe@example.com")
});
var contentAsString = await content.ReadAsStringAsync();
// The expectation is that the matcher requires all properties to be present in the content.
var matcher = new FormUrlEncodedMatcher(["name=*", "email=*", "required=*"], matchOperator: MatchOperator.And);
// Act
var score = matcher.IsMatch(contentAsString).IsPerfect();
// Assert
score.Should().BeFalse();
}
} }