Skip to content

Commit

Permalink
Use Guid.TryParseExact with format "D" (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Jul 29, 2024
1 parent 4b12f34 commit 8dcf35d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
7 changes: 5 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
</Choose>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.29.0.95321">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -52,7 +57,5 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> -->
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/WireMock.Net/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public bool ReadStaticMappingAndAddOrUpdate(string path)
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
{
var mappingModels = DeserializeJsonToArray<MappingModel>(value);
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
{
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
}
Expand Down Expand Up @@ -783,7 +783,7 @@ private void EnhancedFileSystemWatcherDeleted(object sender, FileSystemEventArgs
_settings.Logger.Info("MappingFile deleted : '{0}'", args.FullPath);
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(args.FullPath);

if (Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
if (Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
{
DeleteMapping(guidFromFilename);
}
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Util/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static class StringUtils
s => int.TryParse(s, out var result) ? (true, result) : (false, s),
s => long.TryParse(s, out var result) ? (true, result) : (false, s),
s => double.TryParse(s, out var result) ? (true, result) : (false, s),
s => Guid.TryParse(s, out var result) ? (true, result) : (false, s),
s => Guid.TryParseExact(s, "D", out var result) ? (true, result) : (false, s),
s => TimeSpan.TryParse(s, out var result) ? (true, result) : (false, s),
s => DateTime.TryParse(s, out var result) ? (true, result) : (false, s),
s =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public async Task Response_ProvideResponseAsync_Handlebars_Random1_Integer(Repla
[Theory]
[InlineData(ReplaceNodeOptions.EvaluateAndTryToConvert, JTokenType.Guid)]
[InlineData(ReplaceNodeOptions.Evaluate, JTokenType.String)]
public async Task Response_ProvideResponseAsync_Handlebars_Random1_Guid(ReplaceNodeOptions options, JTokenType expected)
public async Task Response_ProvideResponseAsync_Handlebars_Random_Guid(ReplaceNodeOptions options, JTokenType expected)
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);
Expand All @@ -114,7 +114,8 @@ public async Task Response_ProvideResponseAsync_Handlebars_Random1_Guid(ReplaceN
.WithBodyAsJson(new
{
Guid1 = "{{Random Type=\"Guid\" Uppercase=false}}",
Guid2 = "{{Random Type=\"Guid\"}}"
Guid2 = "{{Random Type=\"Guid\"}}",
Guid3 = "{{ String.Replace (Random Type=\"Guid\") \"-\" \"\" }}"
})
.WithTransformer(options);

Expand All @@ -127,6 +128,27 @@ public async Task Response_ProvideResponseAsync_Handlebars_Random1_Guid(ReplaceN
jObject["Guid2"]!.Type.Should().Be(expected);
}

[Fact]
public async Task Response_ProvideResponseAsync_Handlebars_Random_StringReplaceGuid()
{
// Assign
var request = new RequestMessage(new UrlDetails("http://localhost:1234"), "GET", ClientIp);

var responseBuilder = Response.Create()
.WithBodyAsJson(new
{
MyGuid = "{{ String.Replace (Random Type=\"Guid\") \"-\" \"\" }}"
})
.WithTransformer();

// Act
var response = await responseBuilder.ProvideResponseAsync(_mappingMock.Object, request, _settings).ConfigureAwait(false);

// Assert
var jObject = JObject.FromObject(response.Message.BodyData!.BodyAsJson!);
jObject["MyGuid"]!.Type.Should().Be(JTokenType.String);
}

[Fact]
public async Task Response_ProvideResponseAsync_Handlebars_Random1_StringList()
{
Expand Down
6 changes: 5 additions & 1 deletion test/WireMock.Net.Tests/Util/StringUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void TryConvertToDouble_ShouldWorkCorrectly(string input, double expected
isConverted.Should().Be(expectedConversion);
if (isConverted)
{
((double) convertedValue).Should().BeApproximately(expectedValue, 0.01);
((double)convertedValue).Should().BeApproximately(expectedValue, 0.01);
}
else
{
Expand All @@ -106,6 +106,10 @@ public void TryConvertToDouble_ShouldWorkCorrectly(string input, double expected
}

[Theory]
[InlineData("3F2504E04F8911D39A0C0305E82C3301", false)]
[InlineData("{3F2504E04F8911D39A0C0305E82C3301}", false)]
[InlineData("(3F2504E04F8911D39A0C0305E82C3301)", false)]
[InlineData("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}", false)]
[InlineData("3F2504E0-4F89-11D3-9A0C-0305E82C3301", true)]
[InlineData("00000000-0000-0000-0000-000000000000", true)]
[InlineData("3f2504e0-4f89-11d3-9a0c-0305e82c3301", true)] // Lowercase Guid
Expand Down

0 comments on commit 8dcf35d

Please sign in to comment.