Skip to content

Commit

Permalink
Added title (linked to #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Mar 15, 2017
1 parent b09b882 commit 37de97e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 16 deletions.
8 changes: 8 additions & 0 deletions src/WireMock.Net/Admin/Mappings/MappingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public class MappingModel
/// </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>
Expand Down
8 changes: 8 additions & 0 deletions src/WireMock.Net/Admin/Requests/LogEntryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public class LogEntryModel
/// </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>
Expand Down
8 changes: 8 additions & 0 deletions src/WireMock.Net/Logging/LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,13 @@ public class LogEntry
/// 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; }
}
}
27 changes: 19 additions & 8 deletions src/WireMock.Net/Mapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Matchers.Request;

namespace WireMock
Expand All @@ -10,20 +11,28 @@ namespace WireMock
public class Mapping
{
/// <summary>
/// Gets the priority.
/// Gets the unique identifier.
/// </summary>
/// <value>
/// The priority.
/// The unique identifier.
/// </value>
public int Priority { get; }
public Guid Guid { get; }

/// <summary>
/// Gets the unique identifier.
/// Gets the unique title.
/// </summary>
/// <value>
/// The unique identifier.
/// The unique title.
/// </value>
public Guid Guid { get; }
public string Title { get; }

/// <summary>
/// Gets the priority.
/// </summary>
/// <value>
/// The priority.
/// </value>
public int Priority { get; }

/// <summary>
/// The Request matcher.
Expand All @@ -38,14 +47,16 @@ public class Mapping
/// <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;
}
Expand Down
32 changes: 25 additions & 7 deletions src/WireMock.Net/Server/FluentMockServer.Admin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ private void ReadStaticMappings()

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);
}
}

Expand Down Expand Up @@ -142,9 +146,12 @@ private ResponseMessage MappingPut(RequestMessage requestMessage)
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" };
}
Expand All @@ -171,13 +178,19 @@ private ResponseMessage MappingsSave(RequestMessage requestMessage)
{
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>();
Expand Down Expand Up @@ -230,6 +243,9 @@ private void DeserializeAndAddMapping(string json, Guid? guid = null)
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);

Expand Down Expand Up @@ -315,6 +331,7 @@ private LogEntryModel ToLogEntryModel(LogEntry logEntry)
} : null
},
MappingGuid = logEntry.MappingGuid,
MappingTitle = logEntry.MappingTitle,
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
{
TotalScore = logEntry.RequestMatchResult.TotalScore,
Expand Down Expand Up @@ -472,6 +489,7 @@ private MappingModel ToMappingModel(Mapping mapping)
return new MappingModel
{
Guid = mapping.Guid,
Title = mapping.Title,
Priority = mapping.Priority,
Request = new RequestModel
{
Expand Down Expand Up @@ -503,7 +521,7 @@ private MappingModel ToMappingModel(Mapping mapping)
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(),
Expand Down
1 change: 1 addition & 0 deletions src/WireMock.Net/Server/FluentMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ private async void HandleRequestAsync(HttpListenerContext ctx)
RequestMessage = request,
ResponseMessage = response,
MappingGuid = targetMapping?.Guid,
MappingTitle = targetMapping?.Title,
RequestMatchResult = requestMatchResult
};

Expand Down
7 changes: 7 additions & 0 deletions src/WireMock.Net/Server/IRespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public interface IRespondWithAProvider
/// <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>
Expand Down
15 changes: 14 additions & 1 deletion src/WireMock.Net/Server/RespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal class RespondWithAProvider : IRespondWithAProvider
{
private int _priority;
private Guid? _guid;
private string _title;

/// <summary>
/// The _registration callback.
Expand Down Expand Up @@ -41,7 +42,7 @@ public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestM
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>
Expand All @@ -66,6 +67,18 @@ public IRespondWithAProvider WithGuid(Guid guid)
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>
Expand Down

0 comments on commit 37de97e

Please sign in to comment.