mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-01 07:03:29 +02:00
Add Random Regex (using Fare) (#236)
* Xeger * update example * 1.0.7 * Fix tests for Handlebars_Xeger * ReplaceNodeValue * 1.0.5 * readme
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<VersionPrefix>1.0.4.21</VersionPrefix>
|
<VersionPrefix>1.0.5</VersionPrefix>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<Choose>
|
<Choose>
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
https://github.com/StefH/GitHubReleaseNotes
|
https://github.com/StefH/GitHubReleaseNotes
|
||||||
|
|
||||||
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --version 1.0.4.21
|
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --version 1.0.5
|
||||||
@@ -5,10 +5,10 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
|
|||||||
* HTTP response stubbing, matchable on URL/Path, headers, cookies and body content patterns
|
* HTTP response stubbing, matchable on URL/Path, headers, cookies and body content patterns
|
||||||
* Runs in unit tests, as a standalone process, as windows service, as Azure or IIS or as docker
|
* Runs in unit tests, as a standalone process, as windows service, as Azure or IIS or as docker
|
||||||
* Configurable via a fluent DotNet API, JSON files and JSON over HTTP
|
* Configurable via a fluent DotNet API, JSON files and JSON over HTTP
|
||||||
* Record/playback of stubs
|
* Record/playback of stubs (proxying)
|
||||||
* Per-request conditional proxying
|
* Per-request conditional proxying
|
||||||
* Stateful behaviour simulation
|
* Stateful behaviour simulation
|
||||||
* Configurable response delays
|
* Response transformation
|
||||||
|
|
||||||
## Info
|
## Info
|
||||||
| | |
|
| | |
|
||||||
|
|||||||
@@ -388,11 +388,10 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithStatusCode(500)
|
.WithStatusCode(500)
|
||||||
.WithBody(requestMessage =>
|
.WithBody(requestMessage =>
|
||||||
{
|
{
|
||||||
string returnStr = JsonConvert.SerializeObject(new
|
return JsonConvert.SerializeObject(new
|
||||||
{
|
{
|
||||||
Message = "Test error"
|
Message = "Test error"
|
||||||
});
|
});
|
||||||
return returnStr;
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -402,22 +401,28 @@ namespace WireMock.Net.ConsoleApplication
|
|||||||
.WithHeader("Content-Type", "application/json")
|
.WithHeader("Content-Type", "application/json")
|
||||||
.WithBodyAsJson(new
|
.WithBodyAsJson(new
|
||||||
{
|
{
|
||||||
|
Xeger1 = "{{Xeger \"\\w{4}\\d{5}\"}}",
|
||||||
|
Xeger2 = "{{Xeger \"\\d{5}\"}}",
|
||||||
|
TextRegexPostcode = "{{Random Type=\"TextRegex\" Pattern=\"[1-9][0-9]{3}[A-Z]{2}\"}}",
|
||||||
Text = "{{Random Type=\"Text\" Min=8 Max=20}}",
|
Text = "{{Random Type=\"Text\" Min=8 Max=20}}",
|
||||||
TextLipsum = "{{Random Type=\"TextLipsum\"}}",
|
TextLipsum = "{{Random Type=\"TextLipsum\"}}",
|
||||||
|
IBAN = "{{Random Type=\"IBAN\" CountryCode=\"NL\"}}",
|
||||||
TimeSpan1 = "{{Random Type=\"TimeSpan\" Format=\"c\" IncludeMilliseconds=false}}",
|
TimeSpan1 = "{{Random Type=\"TimeSpan\" Format=\"c\" IncludeMilliseconds=false}}",
|
||||||
TimeSpan2 = "{{Random Type=\"TimeSpan\"}}",
|
TimeSpan2 = "{{Random Type=\"TimeSpan\"}}",
|
||||||
DateTime1 = "{{Random Type=\"DateTime\"}}",
|
DateTime1 = "{{Random Type=\"DateTime\"}}",
|
||||||
DateTimeNow = DateTime.Now,
|
DateTimeNow = DateTime.Now,
|
||||||
DateTimeToString = DateTime.Now.ToString("s", CultureInfo.InvariantCulture),
|
DateTimeNowToString = DateTime.Now.ToString("s", CultureInfo.InvariantCulture),
|
||||||
Guid1 = "{{Random Type=\"Guid\" Uppercase=false}}",
|
Guid1 = "{{Random Type=\"Guid\" Uppercase=false}}",
|
||||||
Guid2 = "{{Random Type=\"Guid\"}}",
|
Guid2 = "{{Random Type=\"Guid\"}}",
|
||||||
Integer1 = "{{Random Type=\"Integer\" Min=1000 Max=9999}}",
|
Boolean = "{{Random Type=\"Boolean\"}}",
|
||||||
Integer2 = "{{#Random Type=\"Integer\" Min=10000000 Max=99999999}}{{this}}{{/Random}}",
|
Integer = "{{Random Type=\"Integer\" Min=1000 Max=9999}}",
|
||||||
Double1 = "{{Random Type=\"Double\" Min=10 Max=99}}",
|
Long = "{{#Random Type=\"Long\" Min=10000000 Max=99999999}}{{this}}{{/Random}}",
|
||||||
Double2 = "{{Random Type=\"Double\" Min=100 Max=999}}",
|
Double = "{{Random Type=\"Double\" Min=10 Max=99}}",
|
||||||
|
Float = "{{Random Type=\"Float\" Min=100 Max=999}}",
|
||||||
IP4Address = "{{Random Type=\"IPv4Address\" Min=\"10.2.3.4\"}}",
|
IP4Address = "{{Random Type=\"IPv4Address\" Min=\"10.2.3.4\"}}",
|
||||||
IP6Address = "{{Random Type=\"IPv6Address\"}}",
|
IP6Address = "{{Random Type=\"IPv6Address\"}}",
|
||||||
MACAddress = "{{Random Type=\"MACAddress\" Separator=\"-\"}}"
|
MACAddress = "{{Random Type=\"MACAddress\" Separator=\"-\"}}",
|
||||||
|
StringListValue = "{{Random Type=\"StringList\" Values=[\"a\", \"b\", \"c\"]}}"
|
||||||
})
|
})
|
||||||
.WithTransformer()
|
.WithTransformer()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace WireMock.Transformers
|
|||||||
HandleBarsLinq.Register(handlebarsContext);
|
HandleBarsLinq.Register(handlebarsContext);
|
||||||
|
|
||||||
HandleBarsRandom.Register(handlebarsContext);
|
HandleBarsRandom.Register(handlebarsContext);
|
||||||
|
|
||||||
|
HandleBarsXeger.Register(handlebarsContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
39
src/WireMock.Net/Transformers/HandleBarsXeger.cs
Normal file
39
src/WireMock.Net/Transformers/HandleBarsXeger.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using System;
|
||||||
|
using Fare;
|
||||||
|
using HandlebarsDotNet;
|
||||||
|
using WireMock.Validation;
|
||||||
|
|
||||||
|
namespace WireMock.Transformers
|
||||||
|
{
|
||||||
|
internal static class HandleBarsXeger
|
||||||
|
{
|
||||||
|
public static void Register(IHandlebars handlebarsContext)
|
||||||
|
{
|
||||||
|
handlebarsContext.RegisterHelper("Xeger", (writer, context, arguments) =>
|
||||||
|
{
|
||||||
|
string value = ParseArgumentAndGenerate(arguments);
|
||||||
|
writer.Write(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
handlebarsContext.RegisterHelper("Xeger", (writer, options, context, arguments) =>
|
||||||
|
{
|
||||||
|
string value = ParseArgumentAndGenerate(arguments);
|
||||||
|
options.Template(writer, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ParseArgumentAndGenerate(object[] arguments)
|
||||||
|
{
|
||||||
|
Check.Condition(arguments, args => args.Length == 1, nameof(arguments));
|
||||||
|
Check.NotNull(arguments[0], "arguments[0]");
|
||||||
|
|
||||||
|
switch (arguments[0])
|
||||||
|
{
|
||||||
|
case string pattern:
|
||||||
|
return new Xeger(pattern).Generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars Xeger.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -120,23 +120,34 @@ namespace WireMock.Transformers
|
|||||||
string transformedString = templateForStringValue(template);
|
string transformedString = templateForStringValue(template);
|
||||||
if (!string.Equals(stringValue, transformedString))
|
if (!string.Equals(stringValue, transformedString))
|
||||||
{
|
{
|
||||||
JToken value;
|
ReplaceNodeValue(node, transformedString);
|
||||||
try
|
|
||||||
{
|
|
||||||
// Try to convert this string into a real JsonObject
|
|
||||||
value = JToken.Parse(transformedString);
|
|
||||||
}
|
|
||||||
catch (JsonException)
|
|
||||||
{
|
|
||||||
// Ignore JsonException and just convert to JToken
|
|
||||||
value = transformedString;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.Replace(value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ReplaceNodeValue(JToken node, string stringValue)
|
||||||
|
{
|
||||||
|
if (bool.TryParse(stringValue, out bool valueAsBoolean))
|
||||||
|
{
|
||||||
|
node.Replace(valueAsBoolean);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JToken value;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Try to convert this string into a JsonObject
|
||||||
|
value = JToken.Parse(stringValue);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
// Ignore JsonException and just keep string value and convert to JToken
|
||||||
|
value = stringValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
node.Replace(value);
|
||||||
|
}
|
||||||
|
|
||||||
private static void TransformBodyAsString(object template, ResponseMessage original, ResponseMessage responseMessage)
|
private static void TransformBodyAsString(object template, ResponseMessage original, ResponseMessage responseMessage)
|
||||||
{
|
{
|
||||||
var templateBody = HandlebarsContext.Compile(original.BodyData.BodyAsString);
|
var templateBody = HandlebarsContext.Compile(original.BodyData.BodyAsString);
|
||||||
|
|||||||
@@ -58,7 +58,7 @@
|
|||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9" />
|
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9" />
|
||||||
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.4" />
|
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.7" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
|
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
|
||||||
|
|||||||
@@ -34,9 +34,30 @@ namespace WireMock.Net.Tests.ResponseBuilders
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
JObject j = JObject.FromObject(responseMessage.BodyData.BodyAsJson);
|
JObject j = JObject.FromObject(responseMessage.BodyData.BodyAsJson);
|
||||||
Check.That(j["Text"]).IsNotNull();
|
Check.That(j["Text"].Value<string>()).IsNotEmpty();
|
||||||
Check.That(j["Integer"].Value<int>()).IsEqualTo(1000);
|
Check.That(j["Integer"].Value<int>()).IsEqualTo(1000);
|
||||||
Check.That(j["Long"].Value<int>()).IsStrictlyGreaterThan(77777777).And.IsStrictlyLessThan(99999999);
|
Check.That(j["Long"].Value<long>()).IsStrictlyGreaterThan(77777777).And.IsStrictlyLessThan(99999999);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponseAsync_Handlebars_Random1_Boolean()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithBodyAsJson(new
|
||||||
|
{
|
||||||
|
Value = "{{Random Type=\"Boolean\"}}"
|
||||||
|
})
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
JObject j = JObject.FromObject(responseMessage.BodyData.BodyAsJson);
|
||||||
|
Check.That(j["Value"].Type).IsEqualTo(JTokenType.Boolean);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using NFluent;
|
||||||
|
using WireMock.Models;
|
||||||
|
using WireMock.ResponseBuilders;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.ResponseBuilders
|
||||||
|
{
|
||||||
|
public class ResponseWithHandlebarsXegerTests
|
||||||
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponseAsync_Handlebars_Xeger1()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithBodyAsJson(new
|
||||||
|
{
|
||||||
|
Number = "{{Xeger \"[1-9]{1}\\d{3}\"}}",
|
||||||
|
Postcode = "{{Xeger \"[1-9][0-9]{3}[A-Z]{2}\"}}"
|
||||||
|
})
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
JObject j = JObject.FromObject(responseMessage.BodyData.BodyAsJson);
|
||||||
|
Check.That(j["Number"].Value<int>()).IsStrictlyGreaterThan(1000).And.IsStrictlyLessThan(9999);
|
||||||
|
Check.That(j["Postcode"].Value<string>()).IsNotEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Response_ProvideResponseAsync_Handlebars_Xeger2()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
|
||||||
|
|
||||||
|
var response = Response.Create()
|
||||||
|
.WithBodyAsJson(new
|
||||||
|
{
|
||||||
|
Number = "{{#Xeger \"[1-9]{1}\\d{3}\"}}{{this}}{{/Xeger}}",
|
||||||
|
Postcode = "{{#Xeger \"[1-9][0-9]{3}[A-Z]{2}\"}}{{this}}{{/Xeger}}"
|
||||||
|
})
|
||||||
|
.WithTransformer();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
JObject j = JObject.FromObject(responseMessage.BodyData.BodyAsJson);
|
||||||
|
Check.That(j["Number"].Value<int>()).IsStrictlyGreaterThan(1000).And.IsStrictlyLessThan(9999);
|
||||||
|
Check.That(j["Postcode"].Value<string>()).IsNotEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user