mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +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
|
||||
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">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</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">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<GrpcServices>Client</GrpcServices>
|
||||
|
||||
Reference in New Issue
Block a user