Added title (linked to #21)

This commit is contained in:
Stef Heyenrath
2017-03-15 12:36:08 +01:00
parent b09b882ad1
commit 37de97ed5d
8 changed files with 92 additions and 18 deletions

View File

@@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings
/// </value> /// </value>
public Guid? Guid { get; set; } public Guid? Guid { get; set; }
/// <summary>
/// Gets or sets the unique title.
/// </summary>
/// <value>
/// The unique title.
/// </value>
public string Title { get; set; }
/// <summary> /// <summary>
/// Gets or sets the priority. /// Gets or sets the priority.
/// </summary> /// </summary>

View File

@@ -39,6 +39,14 @@ namespace WireMock.Admin.Requests
/// </value> /// </value>
public Guid? MappingGuid { get; set; } public Guid? MappingGuid { get; set; }
/// <summary>
/// Gets or sets the mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
public string MappingTitle { get; set; }
/// <summary> /// <summary>
/// Gets or sets the request match result. /// Gets or sets the request match result.
/// </summary> /// </summary>

View File

@@ -47,5 +47,13 @@ namespace WireMock.Logging
/// The mapping unique identifier. /// The mapping unique identifier.
/// </value> /// </value>
public Guid? MappingGuid { get; set; } public Guid? MappingGuid { get; set; }
/// <summary>
/// Gets or sets the mapping unique title.
/// </summary>
/// <value>
/// The mapping unique title.
/// </value>
public string MappingTitle { get; set; }
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
namespace WireMock namespace WireMock
@@ -9,14 +10,6 @@ namespace WireMock
/// </summary> /// </summary>
public class Mapping public class Mapping
{ {
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }
/// <summary> /// <summary>
/// Gets the unique identifier. /// Gets the unique identifier.
/// </summary> /// </summary>
@@ -25,6 +18,22 @@ namespace WireMock
/// </value> /// </value>
public Guid Guid { get; } public Guid Guid { get; }
/// <summary>
/// Gets the unique title.
/// </summary>
/// <value>
/// The unique title.
/// </value>
public string Title { get; }
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }
/// <summary> /// <summary>
/// The Request matcher. /// The Request matcher.
/// </summary> /// </summary>
@@ -38,14 +47,16 @@ namespace WireMock
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class. /// Initializes a new instance of the <see cref="Mapping"/> class.
/// </summary> /// </summary>
/// <param name="guid">The the unique identifier.</param> /// <param name="guid">The unique identifier.</param>
/// <param name="title">The unique title (can be null_.</param>
/// <param name="requestMatcher">The request matcher.</param> /// <param name="requestMatcher">The request matcher.</param>
/// <param name="provider">The provider.</param> /// <param name="provider">The provider.</param>
/// <param name="priority">The priority for this mapping.</param> /// <param name="priority">The priority for this mapping.</param>
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; Priority = priority;
Guid = guid; Guid = guid;
Title = title;
RequestMatcher = requestMatcher; RequestMatcher = requestMatcher;
Provider = provider; Provider = provider;
} }

View File

@@ -44,8 +44,12 @@ namespace WireMock.Server
foreach (string filename in Directory.EnumerateFiles(Directory.GetCurrentDirectory() + AdminMappingsFolder)) foreach (string filename in Directory.EnumerateFiles(Directory.GetCurrentDirectory() + AdminMappingsFolder))
{ {
var json = File.ReadAllText(filename); string filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
DeserializeAndAddMapping(json, Guid.Parse(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 requestBuilder = InitRequestBuilder(mappingModel.Request);
var responseBuilder = InitResponseBuilder(mappingModel.Response); var responseBuilder = InitResponseBuilder(mappingModel.Response);
Given(requestBuilder) IRespondWithAProvider respondProvider = Given(requestBuilder).WithGuid(guid);
.WithGuid(guid)
.RespondWith(responseBuilder); if (!string.IsNullOrEmpty(mappingModel.Title))
respondProvider = respondProvider.WithTitle(mappingModel.Title);
respondProvider.RespondWith(responseBuilder);
return new ResponseMessage { Body = "Mapping added or updated" }; return new ResponseMessage { Body = "Mapping added or updated" };
} }
@@ -171,13 +178,19 @@ namespace WireMock.Server
{ {
var model = ToMappingModel(mapping); var model = ToMappingModel(mapping);
string json = JsonConvert.SerializeObject(model, _settings); 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" }; 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) private ResponseMessage MappingsGet(RequestMessage requestMessage)
{ {
var result = new List<MappingModel>(); var result = new List<MappingModel>();
@@ -230,6 +243,9 @@ namespace WireMock.Server
respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value); respondProvider = respondProvider.WithGuid(mappingModel.Guid.Value);
} }
if (!string.IsNullOrEmpty(mappingModel.Title))
respondProvider = respondProvider.WithTitle(mappingModel.Title);
if (mappingModel.Priority != null) if (mappingModel.Priority != null)
respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value); respondProvider = respondProvider.AtPriority(mappingModel.Priority.Value);
@@ -315,6 +331,7 @@ namespace WireMock.Server
} : null } : null
}, },
MappingGuid = logEntry.MappingGuid, MappingGuid = logEntry.MappingGuid,
MappingTitle = logEntry.MappingTitle,
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
{ {
TotalScore = logEntry.RequestMatchResult.TotalScore, TotalScore = logEntry.RequestMatchResult.TotalScore,
@@ -472,6 +489,7 @@ namespace WireMock.Server
return new MappingModel return new MappingModel
{ {
Guid = mapping.Guid, Guid = mapping.Guid,
Title = mapping.Title,
Priority = mapping.Priority, Priority = mapping.Priority,
Request = new RequestModel Request = new RequestModel
{ {
@@ -503,7 +521,7 @@ namespace WireMock.Server
Funcs = Map(cm.Funcs) Funcs = Map(cm.Funcs)
}).ToList() : null, }).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, Name = pm.Key,
Values = pm.Values?.ToList(), Values = pm.Values?.ToList(),

View File

@@ -452,6 +452,7 @@ namespace WireMock.Server
RequestMessage = request, RequestMessage = request,
ResponseMessage = response, ResponseMessage = response,
MappingGuid = targetMapping?.Guid, MappingGuid = targetMapping?.Guid,
MappingTitle = targetMapping?.Title,
RequestMatchResult = requestMatchResult RequestMatchResult = requestMatchResult
}; };

View File

@@ -14,6 +14,13 @@ namespace WireMock.Server
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns> /// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid); IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define a unique title for this mapping.
/// </summary>
/// <param name="title">The unique title.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithTitle(string title);
/// <summary> /// <summary>
/// Define a unique identifier for this mapping. /// Define a unique identifier for this mapping.
/// </summary> /// </summary>

View File

@@ -10,6 +10,7 @@ namespace WireMock.Server
{ {
private int _priority; private int _priority;
private Guid? _guid; private Guid? _guid;
private string _title;
/// <summary> /// <summary>
/// The _registration callback. /// The _registration callback.
@@ -41,7 +42,7 @@ namespace WireMock.Server
public void RespondWith(IResponseProvider provider) public void RespondWith(IResponseProvider provider)
{ {
var mappingGuid = _guid ?? Guid.NewGuid(); var mappingGuid = _guid ?? Guid.NewGuid();
_registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority)); _registrationCallback(new Mapping(mappingGuid, _title, _requestMatcher, provider, _priority));
} }
/// <summary> /// <summary>
@@ -66,6 +67,18 @@ namespace WireMock.Server
return this; return this;
} }
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="title">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
public IRespondWithAProvider WithTitle(string title)
{
_title = title;
return this;
}
/// <summary> /// <summary>
/// Define the priority for this mapping. /// Define the priority for this mapping.
/// </summary> /// </summary>