mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-24 02:12:02 +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; }
|
public string? Id { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The Text.
|
/// The Texts.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<string> Texts { get; }
|
public IReadOnlyList<string> Texts { get; }
|
||||||
|
|
||||||
@@ -41,7 +41,7 @@ public readonly struct IdOrTexts
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <param name="id">Callback to process the id.</param>
|
/// <param name="id">Callback to process the id.</param>
|
||||||
/// <param name="texts">Callback to process the texts.</param>
|
/// <param name="texts">Callback to process the texts.</param>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#if PROTOBUF
|
#if PROTOBUF
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ProtoBufJsonConverter;
|
using ProtoBufJsonConverter;
|
||||||
@@ -9,21 +10,29 @@ using Stef.Validation;
|
|||||||
|
|
||||||
namespace WireMock.Util;
|
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
|
internal class WireMockProtoFileResolver : IProtoFileResolver
|
||||||
{
|
{
|
||||||
private readonly Dictionary<string, string> _files = new();
|
private readonly Dictionary<string, string> _files = new();
|
||||||
|
|
||||||
public WireMockProtoFileResolver(IReadOnlyCollection<string> protoDefinitions)
|
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));
|
_files.Add(validFileName, extraProtoDefinition);
|
||||||
if (firstNonEmptyLine != null)
|
|
||||||
{
|
|
||||||
_files.Add(firstNonEmptyLine.TrimStart(['\r', '\n', '/', ' ']), extraProtoDefinition);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,5 +51,17 @@ internal class WireMockProtoFileResolver : IProtoFileResolver
|
|||||||
|
|
||||||
throw new FileNotFoundException($"The ProtoDefinition '{path}' was not found.");
|
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
|
#endif
|
||||||
41
test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs
Normal file
41
test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// Copyright © WireMock.Net
|
||||||
|
|
||||||
|
#if PROTOBUF
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using WireMock.Util;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests.Grpc;
|
||||||
|
|
||||||
|
public class ProtoBufUtilsTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task GetProtoBufMessageWithHeader_MultipleProtoFiles()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var greet = await ReadProtoFileAsync("greet1.proto");
|
||||||
|
var request = await ReadProtoFileAsync("request.proto");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var responseBytes = await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(
|
||||||
|
[greet, request],
|
||||||
|
"greet.HelloRequest",
|
||||||
|
new
|
||||||
|
{
|
||||||
|
name = "hello"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task<string> ReadProtoFileAsync(string filename)
|
||||||
|
{
|
||||||
|
return File.ReadAllTextAsync(Path.Combine(Directory.GetCurrentDirectory(), "Grpc", filename));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
13
test/WireMock.Net.Tests/Grpc/greet1.proto
Normal file
13
test/WireMock.Net.Tests/Grpc/greet1.proto
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
import "request.proto";
|
||||||
|
|
||||||
|
package greet;
|
||||||
|
|
||||||
|
service Greeter {
|
||||||
|
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
message HelloReply {
|
||||||
|
string message = 1;
|
||||||
|
}
|
||||||
8
test/WireMock.Net.Tests/Grpc/request.proto
Normal file
8
test/WireMock.Net.Tests/Grpc/request.proto
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// request.proto
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package greet;
|
||||||
|
|
||||||
|
message HelloRequest {
|
||||||
|
string name = 1;
|
||||||
|
}
|
||||||
@@ -134,6 +134,14 @@
|
|||||||
<None Update="cert.pem">
|
<None Update="cert.pem">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
</None>
|
</None>
|
||||||
|
<None Update="Grpc\request.proto">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
<!--<GrpcServices>Client</GrpcServices>-->
|
||||||
|
</None>
|
||||||
|
<None Update="Grpc\greet1.proto">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
<!--<GrpcServices>Client</GrpcServices>-->
|
||||||
|
</None>
|
||||||
<None Update="Grpc\policy.proto">
|
<None Update="Grpc\policy.proto">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
<GrpcServices>Client</GrpcServices>
|
<GrpcServices>Client</GrpcServices>
|
||||||
|
|||||||
Reference in New Issue
Block a user