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
13 changed files with 274 additions and 350 deletions

View File

@@ -1,13 +1,3 @@
# 1.6.0 (16 August 2024)
- [#1042](https://github.com/WireMock-Net/WireMock.Net/pull/1042) - Update + add fluent builder methods [feature] contributed by [StefH](https://github.com/StefH)
- [#1109](https://github.com/WireMock-Net/WireMock.Net/pull/1109) - Add Aspire Extension [feature] contributed by [StefH](https://github.com/StefH)
- [#1148](https://github.com/WireMock-Net/WireMock.Net/pull/1148) - Use Guid.TryParseExact with format "D" contributed by [StefH](https://github.com/StefH)
- [#1157](https://github.com/WireMock-Net/WireMock.Net/pull/1157) - Fix FormUrlEncodedMatcher (MatchOperator.And) [bug] contributed by [StefH](https://github.com/StefH)
- [#1158](https://github.com/WireMock-Net/WireMock.Net/pull/1158) - Allow setting Content-Length header on the response [feature] contributed by [StefH](https://github.com/StefH)
- [#720](https://github.com/WireMock-Net/WireMock.Net/issues/720) - Response Header Content-Length not available when call HEAD Method [feature]
- [#1145](https://github.com/WireMock-Net/WireMock.Net/issues/1145) - Response is auto converting string to guid [bug]
- [#1156](https://github.com/WireMock-Net/WireMock.Net/issues/1156) - FormUrlEncodedMatcher is not requiring to match all properties when MatchOperator.And [bug]
# 1.5.62 (27 July 2024) # 1.5.62 (27 July 2024)
- [#1147](https://github.com/WireMock-Net/WireMock.Net/pull/1147) - Add FormUrlEncodedMatcher [feature] contributed by [StefH](https://github.com/StefH) - [#1147](https://github.com/WireMock-Net/WireMock.Net/pull/1147) - Add FormUrlEncodedMatcher [feature] contributed by [StefH](https://github.com/StefH)
- [#1143](https://github.com/WireMock-Net/WireMock.Net/issues/1143) - FormEncoded Request fails (404 Not Found) if key value pairs order in mapping is different from request body order [bug] - [#1143](https://github.com/WireMock-Net/WireMock.Net/issues/1143) - FormEncoded Request fails (404 Not Found) if key value pairs order in mapping is different from request body order [bug]

View File

@@ -4,7 +4,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<VersionPrefix>1.6.0</VersionPrefix> <VersionPrefix>1.5.62</VersionPrefix>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon> <PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl> <PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> <PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
@@ -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

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

View File

@@ -1,11 +1,5 @@
# 1.6.0 (16 August 2024) # 1.5.62 (27 July 2024)
- #1042 Update + add fluent builder methods [feature] - #1147 Add FormUrlEncodedMatcher [feature]
- #1109 Add Aspire Extension [feature] - #1143 FormEncoded Request fails (404 Not Found) if key value pairs order in mapping is different from request body order [bug]
- #1148 Use Guid.TryParseExact with format &quot;D&quot;
- #1157 Fix FormUrlEncodedMatcher (MatchOperator.And) [bug]
- #1158 Allow setting Content-Length header on the response [feature]
- #720 Response Header Content-Length not available when call HEAD Method [feature]
- #1145 Response is auto converting string to guid [bug]
- #1156 FormUrlEncodedMatcher is not requiring to match all properties when MatchOperator.And [bug]
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -137,6 +137,7 @@ message HelloReply {
public static void Run() public static void Run()
{ {
RunOnLocal(); RunOnLocal();
return;
var mappingBuilder = new MappingBuilder(); var mappingBuilder = new MappingBuilder();
mappingBuilder mappingBuilder
@@ -307,6 +308,17 @@ message HelloReply {
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithBody("GraphQL is ok") .WithBody("GraphQL is ok")
); );
//server
// .AddGraphQLSchema("my-graphql", TestSchema, customScalars)
// .Given(Request.Create()
// .WithPath("/graphql2")
// .UsingPost()
// )
// .WithGraphQLSchema("my-graphql")
// .RespondWith(Response.Create()
// .WithBody("GraphQL is ok")
// );
#endif #endif
#if MIMEKIT #if MIMEKIT
@@ -365,15 +377,6 @@ message HelloReply {
.WithHeader("Content-Type", "text/plain") .WithHeader("Content-Type", "text/plain")
); );
server
.Given(Request.Create()
.UsingHead()
.WithPath("/cl")
)
.RespondWith(Response.Create()
.WithHeader("Content-Length", "42")
);
server server
.Given(Request.Create() .Given(Request.Create()
.UsingMethod("GET") .UsingMethod("GET")

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

@@ -14,12 +14,12 @@ namespace WireMock.Http;
/// </summary> /// </summary>
internal static class HttpKnownHeaderNames internal static class HttpKnownHeaderNames
{ {
// - https://docs.microsoft.com/en-us/dotnet/api/system.net.webheadercollection.isrestricted // https://docs.microsoft.com/en-us/dotnet/api/system.net.webheadercollection.isrestricted
// - ContentLength is allowed per #720
private static readonly string[] RestrictedResponseHeaders = private static readonly string[] RestrictedResponseHeaders =
{ {
Accept, Accept,
Connection, Connection,
ContentLength,
ContentType, ContentType,
Date, // RFC1123Pattern Date, // RFC1123Pattern
Expect, Expect,

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

@@ -37,19 +37,12 @@ namespace WireMock.Owin.Mappers
private readonly Encoding _utf8NoBom = new UTF8Encoding(false); private readonly Encoding _utf8NoBom = new UTF8Encoding(false);
// https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx // https://msdn.microsoft.com/en-us/library/78h415ay(v=vs.110).aspx
private static readonly IDictionary<string, Action<IResponse, bool, WireMockList<string>>> ResponseHeadersToFix = #if !USE_ASPNETCORE
new Dictionary<string, Action<IResponse, bool, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
{ #else
{ HttpKnownHeaderNames.ContentType, (r, _, v) => r.ContentType = v.FirstOrDefault() }, private static readonly IDictionary<string, Action<IResponse, WireMockList<string>>> ResponseHeadersToFix = new Dictionary<string, Action<IResponse, WireMockList<string>>>(StringComparer.OrdinalIgnoreCase) {
{ HttpKnownHeaderNames.ContentLength, (r, hasBody, v) => #endif
{ { HttpKnownHeaderNames.ContentType, (r, v) => r.ContentType = v.FirstOrDefault() }
// Only set the Content-Length header if the response does not have a body
if (!hasBody && long.TryParse(v.FirstOrDefault(), out var contentLength))
{
r.ContentLength = contentLength;
}
}
}
}; };
/// <summary> /// <summary>
@@ -90,21 +83,23 @@ namespace WireMock.Owin.Mappers
} }
var statusCodeType = responseMessage.StatusCode?.GetType(); var statusCodeType = responseMessage.StatusCode?.GetType();
if (statusCodeType != null) switch (statusCodeType)
{
if (statusCodeType == typeof(int) || statusCodeType == typeof(int?) || statusCodeType.GetTypeInfo().IsEnum)
{ {
case { } when statusCodeType == typeof(int) || statusCodeType == typeof(int?) || statusCodeType.GetTypeInfo().IsEnum:
response.StatusCode = MapStatusCode((int)responseMessage.StatusCode!); response.StatusCode = MapStatusCode((int)responseMessage.StatusCode!);
} break;
else if (statusCodeType == typeof(string))
{ case { } when statusCodeType == typeof(string):
// Note: this case will also match on null // Note: this case will also match on null
int.TryParse(responseMessage.StatusCode as string, out var statusCodeTypeAsInt); int.TryParse(responseMessage.StatusCode as string, out var result);
response.StatusCode = MapStatusCode(statusCodeTypeAsInt); response.StatusCode = MapStatusCode(result);
} break;
default:
break;
} }
SetResponseHeaders(responseMessage, bytes, response); SetResponseHeaders(responseMessage, response);
if (bytes != null) if (bytes != null)
{ {
@@ -165,7 +160,7 @@ namespace WireMock.Owin.Mappers
return null; return null;
} }
private static void SetResponseHeaders(IResponseMessage responseMessage, byte[]? bytes, IResponse response) private static void SetResponseHeaders(IResponseMessage responseMessage, IResponse response)
{ {
// Force setting the Date header (#577) // Force setting the Date header (#577)
AppendResponseHeader( AppendResponseHeader(
@@ -184,11 +179,11 @@ namespace WireMock.Owin.Mappers
var value = item.Value; var value = item.Value;
if (ResponseHeadersToFix.TryGetValue(headerName, out var action)) if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
{ {
action?.Invoke(response, bytes != null, value); action?.Invoke(response, value);
} }
else else
{ {
// Check if this response header can be added (#148, #227 and #720) // Check if this response header can be added (#148 and #227)
if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName)) if (!HttpKnownHeaderNames.IsRestrictedResponseHeader(headerName))
{ {
AppendResponseHeader(response, headerName, value.ToArray()); AppendResponseHeader(response, headerName, value.ToArray());
@@ -211,7 +206,7 @@ namespace WireMock.Owin.Mappers
var value = item.Value; var value = item.Value;
if (ResponseHeadersToFix.TryGetValue(headerName, out var action)) if (ResponseHeadersToFix.TryGetValue(headerName, out var action))
{ {
action?.Invoke(response, false, value); action?.Invoke(response, value);
} }
else else
{ {

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

@@ -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();
}
} }

View File

@@ -25,8 +25,8 @@ using Response = Microsoft.AspNetCore.Http.HttpResponse;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
#endif #endif
namespace WireMock.Net.Tests.Owin.Mappers; namespace WireMock.Net.Tests.Owin.Mappers
{
public class OwinResponseMapperTests public class OwinResponseMapperTests
{ {
private static readonly Task CompletedTask = Task.FromResult(true); private static readonly Task CompletedTask = Task.FromResult(true);
@@ -299,3 +299,4 @@ public class OwinResponseMapperTests
_stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), 0, It.Is<int>(count => count >= 0), It.IsAny<CancellationToken>()), Times.Once); _stream.Verify(s => s.WriteAsync(It.IsAny<byte[]>(), 0, It.Is<int>(count => count >= 0), It.IsAny<CancellationToken>()), Times.Once);
} }
} }
}

View File

@@ -14,7 +14,6 @@ using FluentAssertions;
using Newtonsoft.Json; using Newtonsoft.Json;
using NFluent; using NFluent;
using WireMock.Admin.Mappings; using WireMock.Admin.Mappings;
using WireMock.Http;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Net.Tests.Serialization; using WireMock.Net.Tests.Serialization;
using WireMock.Net.Xunit; using WireMock.Net.Xunit;
@@ -352,7 +351,7 @@ public partial class WireMockServerTests
//} //}
[Fact] [Fact]
public async Task WireMockServer_Should_Exclude_RestrictedResponseHeader() public async Task WireMockServer_Should_exclude_restrictedResponseHeader()
{ {
// Assign // Assign
string path = $"/foo_{Guid.NewGuid()}"; string path = $"/foo_{Guid.NewGuid()}";
@@ -372,26 +371,6 @@ public partial class WireMockServerTests
server.Stop(); server.Stop();
} }
[Fact] // #720
public async Task WireMockServer_Should_AllowResponseHeaderContentLength_For_HEAD()
{
// Assign
const string length = "42";
var path = $"/cl_{Guid.NewGuid()}";
using var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath(path).UsingHead())
.RespondWith(Response.Create().WithHeader(HttpKnownHeaderNames.ContentLength, length));
// Act
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, path);
var response = await server.CreateClient().SendAsync(httpRequestMessage).ConfigureAwait(false);
// Assert
response.Content.Headers.GetValues(HttpKnownHeaderNames.ContentLength).Should().Contain(length);
}
#if !NET452 && !NET461 #if !NET452 && !NET461
[Theory] [Theory]
[InlineData("TRACE")] [InlineData("TRACE")]