mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:03:46 +01:00
Added title (linked to #21)
This commit is contained in:
@@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings
|
||||
/// </value>
|
||||
public Guid? Guid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the unique title.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The unique title.
|
||||
/// </value>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the priority.
|
||||
/// </summary>
|
||||
|
||||
@@ -39,6 +39,14 @@ namespace WireMock.Admin.Requests
|
||||
/// </value>
|
||||
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>
|
||||
/// Gets or sets the request match result.
|
||||
/// </summary>
|
||||
|
||||
@@ -47,5 +47,13 @@ namespace WireMock.Logging
|
||||
/// The mapping unique identifier.
|
||||
/// </value>
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
/// </summary>
|
||||
public class Mapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the priority.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The priority.
|
||||
/// </value>
|
||||
public int Priority { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique identifier.
|
||||
/// </summary>
|
||||
@@ -25,6 +18,22 @@ namespace WireMock
|
||||
/// </value>
|
||||
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>
|
||||
/// The Request matcher.
|
||||
/// </summary>
|
||||
@@ -38,14 +47,16 @@ namespace WireMock
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Mapping"/> class.
|
||||
/// </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="provider">The provider.</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;
|
||||
Guid = guid;
|
||||
Title = title;
|
||||
RequestMatcher = requestMatcher;
|
||||
Provider = provider;
|
||||
}
|
||||
|
||||
@@ -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<MappingModel>();
|
||||
@@ -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(),
|
||||
|
||||
@@ -452,6 +452,7 @@ namespace WireMock.Server
|
||||
RequestMessage = request,
|
||||
ResponseMessage = response,
|
||||
MappingGuid = targetMapping?.Guid,
|
||||
MappingTitle = targetMapping?.Title,
|
||||
RequestMatchResult = requestMatchResult
|
||||
};
|
||||
|
||||
|
||||
@@ -14,6 +14,13 @@ namespace WireMock.Server
|
||||
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
|
||||
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>
|
||||
/// Define a unique identifier for this mapping.
|
||||
/// </summary>
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace WireMock.Server
|
||||
{
|
||||
private int _priority;
|
||||
private Guid? _guid;
|
||||
private string _title;
|
||||
|
||||
/// <summary>
|
||||
/// 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,6 +67,18 @@ namespace WireMock.Server
|
||||
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>
|
||||
/// Define the priority for this mapping.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user