Add check for duplicate Guids when posting multiple mappings in one request (#795)

* Add check for DuplicateGuids

* Add check for duplicate Guids when posting mapping(s)

* mappingModels

* fix ut
This commit is contained in:
Stef Heyenrath
2022-08-22 20:05:52 +02:00
committed by GitHub
parent 330559b9fd
commit f0d6ed26bc
6 changed files with 935 additions and 872 deletions

View File

@@ -16,6 +16,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Flurl/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcs/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=guidb/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Guids/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Heyenrath/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jmes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pacticipant/@EntryIndexedValue">True</s:Boolean>

View File

@@ -188,16 +188,13 @@ public partial class WireMockServer
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
{
var mappingModels = DeserializeJsonToArray<MappingModel>(value);
foreach (var mappingModel in mappingModels)
{
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
{
ConvertMappingAndRegisterAsRespondProvider(mappingModel, guidFromFilename, path);
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
}
else
{
ConvertMappingAndRegisterAsRespondProvider(mappingModel, null, path);
}
ConvertMappingsAndRegisterAsRespondProvider(mappingModels, path);
}
return true;
@@ -422,10 +419,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create("Mapping added", 201, guid);
}
foreach (var mappingModel in mappingModels)
{
ConvertMappingAndRegisterAsRespondProvider(mappingModel);
}
ConvertMappingsAndRegisterAsRespondProvider(mappingModels);
return ResponseMessageBuilder.Create("Mappings added", 201);
}

View File

@@ -14,6 +14,25 @@ namespace WireMock.Server;
public partial class WireMockServer
{
private void ConvertMappingsAndRegisterAsRespondProvider(MappingModel[] mappingModels, string? path = null)
{
var duplicateGuids = mappingModels
.Where(m => m.Guid != null)
.GroupBy(m => m.Guid)
.Where(g => g.Count() > 1)
.Select(g => $"'{g.Key}'")
.ToArray();
if (duplicateGuids.Any())
{
throw new ArgumentException($"The following Guids are duplicate : {string.Join(",", duplicateGuids)}", nameof(mappingModels));
}
foreach (var mappingModel in mappingModels)
{
ConvertMappingAndRegisterAsRespondProvider(mappingModel, null, path);
}
}
private Guid? ConvertMappingAndRegisterAsRespondProvider(MappingModel mappingModel, Guid? guid = null, string? path = null)
{
Guard.NotNull(mappingModel);
@@ -37,9 +56,10 @@ public partial class WireMockServer
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
}
if (mappingModel.TimeSettings != null)
var timeSettings = TimeSettingsMapper.Map(mappingModel.TimeSettings);
if (timeSettings != null)
{
respondProvider = respondProvider.WithTimeSettings(TimeSettingsMapper.Map(mappingModel.TimeSettings));
respondProvider = respondProvider.WithTimeSettings(timeSettings);
}
if (path != null)
@@ -101,7 +121,7 @@ public partial class WireMockServer
else
{
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel?.Matchers != null)
if (clientIPModel.Matchers != null)
{
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
}
@@ -119,7 +139,7 @@ public partial class WireMockServer
else
{
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
if (pathModel?.Matchers != null)
if (pathModel.Matchers != null)
{
var matchOperator = StringUtils.ParseMatchOperator(pathModel.MatchOperator);
requestBuilder = requestBuilder.WithPath(matchOperator, pathModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
@@ -137,7 +157,7 @@ public partial class WireMockServer
else
{
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
if (urlModel?.Matchers != null)
if (urlModel.Matchers != null)
{
var matchOperator = StringUtils.ParseMatchOperator(urlModel.MatchOperator);
requestBuilder = requestBuilder.WithUrl(matchOperator, urlModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());

View File

@@ -73,7 +73,7 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net461'">
<PackageReference Include="FluentAssertions" Version="6.5.1" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net452'">

View File

@@ -19,8 +19,8 @@ using WireMock.Server;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests
{
namespace WireMock.Net.Tests;
public class WireMockAdminApiTests
{
[Fact]
@@ -159,6 +159,57 @@ namespace WireMock.Net.Tests
server.Stop();
}
[Fact]
public async Task IWireMockAdminApi_PostMappingsAsync_WithDuplicateGuids_Should_Return_400()
{
// Arrange
var guid = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
var server = WireMockServer.StartWithAdminInterface();
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
// Act
var model1WithGuid = new MappingModel
{
Guid = guid,
Request = new RequestModel { Path = "/1g" },
Response = new ResponseModel { Body = "txt 1g" },
Title = "test 1g"
};
var model2WithGuid = new MappingModel
{
Guid = guid,
Request = new RequestModel { Path = "/2g" },
Response = new ResponseModel { Body = "txt 2g" },
Title = "test 2g"
};
var model1 = new MappingModel
{
Request = new RequestModel { Path = "/1" },
Response = new ResponseModel { Body = "txt 1" },
Title = "test 1"
};
var model2 = new MappingModel
{
Request = new RequestModel { Path = "/2" },
Response = new ResponseModel { Body = "txt 2" },
Title = "test 2"
};
var models = new[]
{
model1WithGuid,
model2WithGuid,
model1,
model2
};
var sutMethod = async () => await api.PostMappingsAsync(models);
var exceptionAssertions = await sutMethod.Should().ThrowAsync<ApiException>();
exceptionAssertions.Which.Content.Should().Be(@"{""Status"":""The following Guids are duplicate : '1b731398-4a5b-457f-a6e3-d65e541c428f' (Parameter 'mappingModels')""}");
server.Stop();
}
[Fact]
public async Task IWireMockAdminApi_FindRequestsAsync()
{
@@ -553,5 +604,4 @@ namespace WireMock.Net.Tests
server.Stop();
}
}
}
#endif

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
@@ -17,8 +16,8 @@ using WireMock.Server;
using WireMock.Settings;
using Xunit;
namespace WireMock.Net.Tests
{
namespace WireMock.Net.Tests;
public class WireMockServerAdminTests
{
// For for AppVeyor + OpenCover
@@ -472,7 +471,7 @@ namespace WireMock.Net.Tests
var response = await new HttpClient().SendAsync(request).ConfigureAwait(false);
// Assert
IEnumerable<Guid> guids = server.MappingModels.Select(mapping => mapping.Guid.Value);
var guids = server.MappingModels.Select(mapping => mapping.Guid!.Value).ToArray();
Check.That(guids.Contains(guid1.Value)).IsFalse();
Check.That(guids.Contains(guid2.Value)).IsFalse();
Check.That(guids.Contains(guid3.Value)).IsTrue();
@@ -480,4 +479,3 @@ namespace WireMock.Net.Tests
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals($"{{\"Status\":\"Mappings deleted. Affected GUIDs: [{guid1}, {guid2}]\"}}");
}
}
}