diff --git a/src/WireMock.Net/Admin/Mappings/MappingModel.cs b/src/WireMock.Net/Admin/Mappings/MappingModel.cs index 797fa4fd..b21c371e 100644 --- a/src/WireMock.Net/Admin/Mappings/MappingModel.cs +++ b/src/WireMock.Net/Admin/Mappings/MappingModel.cs @@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings /// public Guid? Guid { get; set; } + /// + /// Gets or sets the unique title. + /// + /// + /// The unique title. + /// + public string Title { get; set; } + /// /// Gets or sets the priority. /// diff --git a/src/WireMock.Net/Admin/Requests/LogEntryModel.cs b/src/WireMock.Net/Admin/Requests/LogEntryModel.cs index 76e7a6ea..d9d5263c 100644 --- a/src/WireMock.Net/Admin/Requests/LogEntryModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogEntryModel.cs @@ -39,6 +39,14 @@ namespace WireMock.Admin.Requests /// public Guid? MappingGuid { get; set; } + /// + /// Gets or sets the mapping unique title. + /// + /// + /// The mapping unique title. + /// + public string MappingTitle { get; set; } + /// /// Gets or sets the request match result. /// diff --git a/src/WireMock.Net/Logging/LogEntry.cs b/src/WireMock.Net/Logging/LogEntry.cs index 35c9d01c..a1d17d34 100644 --- a/src/WireMock.Net/Logging/LogEntry.cs +++ b/src/WireMock.Net/Logging/LogEntry.cs @@ -47,5 +47,13 @@ namespace WireMock.Logging /// The mapping unique identifier. /// public Guid? MappingGuid { get; set; } + + /// + /// Gets or sets the mapping unique title. + /// + /// + /// The mapping unique title. + /// + public string MappingTitle { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Mapping.cs b/src/WireMock.Net/Mapping.cs index ef24c85c..c7d3c5a8 100644 --- a/src/WireMock.Net/Mapping.cs +++ b/src/WireMock.Net/Mapping.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using JetBrains.Annotations; using WireMock.Matchers.Request; namespace WireMock @@ -9,14 +10,6 @@ namespace WireMock /// public class Mapping { - /// - /// Gets the priority. - /// - /// - /// The priority. - /// - public int Priority { get; } - /// /// Gets the unique identifier. /// @@ -25,6 +18,22 @@ namespace WireMock /// public Guid Guid { get; } + /// + /// Gets the unique title. + /// + /// + /// The unique title. + /// + public string Title { get; } + + /// + /// Gets the priority. + /// + /// + /// The priority. + /// + public int Priority { get; } + /// /// The Request matcher. /// @@ -38,14 +47,16 @@ namespace WireMock /// /// Initializes a new instance of the class. /// - /// The the unique identifier. + /// The unique identifier. + /// The unique title (can be null_. /// The request matcher. /// The provider. /// The priority for this mapping. - public Mapping(Guid guid, IRequestMatcher requestMatcher, IResponseProvider provider, int priority) + public Mapping(Guid guid, [CanBeNull] string title, IRequestMatcher requestMatcher, IResponseProvider provider, int priority) { Priority = priority; Guid = guid; + Title = title; RequestMatcher = requestMatcher; Provider = provider; } diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index bc33e20e..3a08b6f0 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -44,8 +44,12 @@ namespace WireMock.Server foreach (string filename in Directory.EnumerateFiles(Directory.GetCurrentDirectory() + AdminMappingsFolder)) { - var json = File.ReadAllText(filename); - DeserializeAndAddMapping(json, Guid.Parse(Path.GetFileNameWithoutExtension(filename))); + string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename); + Guid guid; + if (!Guid.TryParse(filenameWithoutExtension, out guid)) + guid = Guid.NewGuid(); + + DeserializeAndAddMapping(File.ReadAllText(filename), guid); } } @@ -142,9 +146,12 @@ namespace WireMock.Server var requestBuilder = InitRequestBuilder(mappingModel.Request); var responseBuilder = InitResponseBuilder(mappingModel.Response); - Given(requestBuilder) - .WithGuid(guid) - .RespondWith(responseBuilder); + IRespondWithAProvider respondProvider = Given(requestBuilder).WithGuid(guid); + + if (!string.IsNullOrEmpty(mappingModel.Title)) + respondProvider = respondProvider.WithTitle(mappingModel.Title); + + respondProvider.RespondWith(responseBuilder); return new ResponseMessage { Body = "Mapping added or updated" }; } @@ -171,13 +178,19 @@ namespace WireMock.Server { var model = ToMappingModel(mapping); string json = JsonConvert.SerializeObject(model, _settings); + string filename = !string.IsNullOrEmpty(mapping.Title) ? SanitizeFileName(mapping.Title) : mapping.Guid.ToString(); - File.WriteAllText(Path.Combine(folder, mapping.Guid + ".json"), json); + File.WriteAllText(Path.Combine(folder, filename + ".json"), json); } return new ResponseMessage { Body = "Mappings saved to disk" }; } + private static string SanitizeFileName(string name, char replaceChar = '_') + { + return Path.GetInvalidFileNameChars().Aggregate(name, (current, c) => current.Replace(c, replaceChar)); + } + private ResponseMessage MappingsGet(RequestMessage requestMessage) { var result = new List(); @@ -230,6 +243,9 @@ namespace WireMock.Server respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value); } + if (!string.IsNullOrEmpty(mappingModel.Title)) + respondProvider = respondProvider.WithTitle(mappingModel.Title); + if (mappingModel.Priority != null) respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value); @@ -315,6 +331,7 @@ namespace WireMock.Server } : null }, MappingGuid = logEntry.MappingGuid, + MappingTitle = logEntry.MappingTitle, RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel { TotalScore = logEntry.RequestMatchResult.TotalScore, @@ -472,6 +489,7 @@ namespace WireMock.Server return new MappingModel { Guid = mapping.Guid, + Title = mapping.Title, Priority = mapping.Priority, Request = new RequestModel { @@ -503,7 +521,7 @@ namespace WireMock.Server Funcs = Map(cm.Funcs) }).ToList() : null, - Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers?.Select(pm => new ParamModel + Params = paramsMatchers != null && paramsMatchers.Any() ? paramsMatchers.Select(pm => new ParamModel { Name = pm.Key, Values = pm.Values?.ToList(), diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 0788e7b8..d5c553a0 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -452,6 +452,7 @@ namespace WireMock.Server RequestMessage = request, ResponseMessage = response, MappingGuid = targetMapping?.Guid, + MappingTitle = targetMapping?.Title, RequestMatchResult = requestMatchResult }; diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index a0c54131..1ee3c536 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -14,6 +14,13 @@ namespace WireMock.Server /// The . IRespondWithAProvider WithGuid(Guid guid); + /// + /// Define a unique title for this mapping. + /// + /// The unique title. + /// The . + IRespondWithAProvider WithTitle(string title); + /// /// Define a unique identifier for this mapping. /// diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index afae9b22..843a5448 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -10,6 +10,7 @@ namespace WireMock.Server { private int _priority; private Guid? _guid; + private string _title; /// /// The _registration callback. @@ -41,7 +42,7 @@ namespace WireMock.Server public void RespondWith(IResponseProvider provider) { var mappingGuid = _guid ?? Guid.NewGuid(); - _registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority)); + _registrationCallback(new Mapping(mappingGuid, _title, _requestMatcher, provider, _priority)); } /// @@ -66,6 +67,18 @@ namespace WireMock.Server return this; } + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + public IRespondWithAProvider WithTitle(string title) + { + _title = title; + + return this; + } + /// /// Define the priority for this mapping. ///