mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-18 16:17:10 +01:00
Update WireMockProtoFileResolver and add tests for ProtoBufUtils (#1252)
* Update WireMockProtoFileResolver and add tests for ProtoBufUtils * .
This commit is contained in:
@@ -16,7 +16,7 @@ public readonly struct IdOrTexts
|
||||
public string? Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The Text.
|
||||
/// The Texts.
|
||||
/// </summary>
|
||||
public IReadOnlyList<string> Texts { get; }
|
||||
|
||||
@@ -41,7 +41,7 @@ public readonly struct IdOrTexts
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When Id is defined, return process the Id, else process the Texts.
|
||||
/// When Id is defined, process the Id, else process the Texts.
|
||||
/// </summary>
|
||||
/// <param name="id">Callback to process the id.</param>
|
||||
/// <param name="texts">Callback to process the texts.</param>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#if PROTOBUF
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using ProtoBufJsonConverter;
|
||||
@@ -9,21 +10,29 @@ using Stef.Validation;
|
||||
|
||||
namespace WireMock.Util;
|
||||
|
||||
/// <summary>
|
||||
/// This resolver is used to resolve the extra ProtoDefinition files.
|
||||
/// It assumes that:
|
||||
/// - the first ProtoDefinition file is the main ProtoDefinition file.
|
||||
/// - the first commented line of each extra ProtoDefinition file is the filename which is used in the import of the other ProtoDefinition file(s).
|
||||
/// </summary>
|
||||
internal class WireMockProtoFileResolver : IProtoFileResolver
|
||||
{
|
||||
private readonly Dictionary<string, string> _files = new();
|
||||
|
||||
public WireMockProtoFileResolver(IReadOnlyCollection<string> protoDefinitions)
|
||||
{
|
||||
if (Guard.NotNullOrEmpty(protoDefinitions).Count() > 1)
|
||||
if (Guard.NotNullOrEmpty(protoDefinitions).Count() <= 1)
|
||||
{
|
||||
foreach (var extraProtoDefinition in protoDefinitions.Skip(1))
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var extraProtoDefinition in protoDefinitions.Skip(1))
|
||||
{
|
||||
var firstNonEmptyLine = extraProtoDefinition.Split(['\r', '\n']).FirstOrDefault(l => !string.IsNullOrEmpty(l));
|
||||
if (firstNonEmptyLine != null && TryGetValidFileName(firstNonEmptyLine.TrimStart(['/', ' ']), out var validFileName))
|
||||
{
|
||||
var firstNonEmptyLine = extraProtoDefinition.Split(['\r', '\n']).FirstOrDefault(l => !string.IsNullOrEmpty(l));
|
||||
if (firstNonEmptyLine != null)
|
||||
{
|
||||
_files.Add(firstNonEmptyLine.TrimStart(['\r', '\n', '/', ' ']), extraProtoDefinition);
|
||||
}
|
||||
_files.Add(validFileName, extraProtoDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,5 +51,17 @@ internal class WireMockProtoFileResolver : IProtoFileResolver
|
||||
|
||||
throw new FileNotFoundException($"The ProtoDefinition '{path}' was not found.");
|
||||
}
|
||||
|
||||
private static bool TryGetValidFileName(string fileName, [NotNullWhen(true)] out string? validFileName)
|
||||
{
|
||||
if (!fileName.Any(c => Path.GetInvalidFileNameChars().Contains(c)))
|
||||
{
|
||||
validFileName = fileName;
|
||||
return true;
|
||||
}
|
||||
|
||||
validFileName = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user