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/=Flurl/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=funcs/@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/=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/=Heyenrath/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Jmes/@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> <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)) if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
{ {
var mappingModels = DeserializeJsonToArray<MappingModel>(value); var mappingModels = DeserializeJsonToArray<MappingModel>(value);
foreach (var mappingModel in mappingModels)
{
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename)) if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out Guid guidFromFilename))
{ {
ConvertMappingAndRegisterAsRespondProvider(mappingModel, guidFromFilename, path); ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
} }
else else
{ {
ConvertMappingAndRegisterAsRespondProvider(mappingModel, null, path); ConvertMappingsAndRegisterAsRespondProvider(mappingModels, path);
}
} }
return true; return true;
@@ -422,10 +419,7 @@ public partial class WireMockServer
return ResponseMessageBuilder.Create("Mapping added", 201, guid); return ResponseMessageBuilder.Create("Mapping added", 201, guid);
} }
foreach (var mappingModel in mappingModels) ConvertMappingsAndRegisterAsRespondProvider(mappingModels);
{
ConvertMappingAndRegisterAsRespondProvider(mappingModel);
}
return ResponseMessageBuilder.Create("Mappings added", 201); return ResponseMessageBuilder.Create("Mappings added", 201);
} }

View File

@@ -14,6 +14,25 @@ namespace WireMock.Server;
public partial class WireMockServer 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) private Guid? ConvertMappingAndRegisterAsRespondProvider(MappingModel mappingModel, Guid? guid = null, string? path = null)
{ {
Guard.NotNull(mappingModel); Guard.NotNull(mappingModel);
@@ -37,9 +56,10 @@ public partial class WireMockServer
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value); 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) if (path != null)
@@ -101,7 +121,7 @@ public partial class WireMockServer
else else
{ {
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP); 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()); requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
} }
@@ -119,7 +139,7 @@ public partial class WireMockServer
else else
{ {
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path); var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
if (pathModel?.Matchers != null) if (pathModel.Matchers != null)
{ {
var matchOperator = StringUtils.ParseMatchOperator(pathModel.MatchOperator); var matchOperator = StringUtils.ParseMatchOperator(pathModel.MatchOperator);
requestBuilder = requestBuilder.WithPath(matchOperator, pathModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray()); requestBuilder = requestBuilder.WithPath(matchOperator, pathModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
@@ -137,7 +157,7 @@ public partial class WireMockServer
else else
{ {
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url); var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
if (urlModel?.Matchers != null) if (urlModel.Matchers != null)
{ {
var matchOperator = StringUtils.ParseMatchOperator(urlModel.MatchOperator); var matchOperator = StringUtils.ParseMatchOperator(urlModel.MatchOperator);
requestBuilder = requestBuilder.WithUrl(matchOperator, urlModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray()); requestBuilder = requestBuilder.WithUrl(matchOperator, urlModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());

View File

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

View File

@@ -19,10 +19,10 @@ using WireMock.Server;
using WireMock.Settings; using WireMock.Settings;
using Xunit; using Xunit;
namespace WireMock.Net.Tests namespace WireMock.Net.Tests;
public class WireMockAdminApiTests
{ {
public class WireMockAdminApiTests
{
[Fact] [Fact]
public async Task IWireMockAdminApi_GetSettingsAsync() public async Task IWireMockAdminApi_GetSettingsAsync()
{ {
@@ -159,6 +159,57 @@ namespace WireMock.Net.Tests
server.Stop(); 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] [Fact]
public async Task IWireMockAdminApi_FindRequestsAsync() public async Task IWireMockAdminApi_FindRequestsAsync()
{ {
@@ -552,6 +603,5 @@ namespace WireMock.Net.Tests
server.Stop(); server.Stop();
} }
}
} }
#endif #endif

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@@ -17,10 +16,10 @@ using WireMock.Server;
using WireMock.Settings; using WireMock.Settings;
using Xunit; using Xunit;
namespace WireMock.Net.Tests namespace WireMock.Net.Tests;
public class WireMockServerAdminTests
{ {
public class WireMockServerAdminTests
{
// For for AppVeyor + OpenCover // For for AppVeyor + OpenCover
private string GetCurrentFolder() private string GetCurrentFolder()
{ {
@@ -472,12 +471,11 @@ namespace WireMock.Net.Tests
var response = await new HttpClient().SendAsync(request).ConfigureAwait(false); var response = await new HttpClient().SendAsync(request).ConfigureAwait(false);
// Assert // 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(guid1.Value)).IsFalse();
Check.That(guids.Contains(guid2.Value)).IsFalse(); Check.That(guids.Contains(guid2.Value)).IsFalse();
Check.That(guids.Contains(guid3.Value)).IsTrue(); Check.That(guids.Contains(guid3.Value)).IsTrue();
Check.That(response.StatusCode).Equals(HttpStatusCode.OK); Check.That(response.StatusCode).Equals(HttpStatusCode.OK);
Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals($"{{\"Status\":\"Mappings deleted. Affected GUIDs: [{guid1}, {guid2}]\"}}"); Check.That(await response.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals($"{{\"Status\":\"Mappings deleted. Affected GUIDs: [{guid1}, {guid2}]\"}}");
} }
}
} }