Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WireMockProtoFileResolver and add tests for ProtoBufUtils #1252

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/WireMock.Net.Abstractions/Models/IdOrTexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public readonly struct IdOrTexts
public string? Id { get; }

/// <summary>
/// The Text.
/// The Texts.
/// </summary>
public IReadOnlyList<string> Texts { get; }

Expand All @@ -41,7 +41,7 @@ public IdOrTexts(string? id, IReadOnlyList<string> texts)
}

/// <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>
Expand Down
35 changes: 28 additions & 7 deletions src/WireMock.Net/Util/WireMockProtoFileResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@

#if PROTOBUF
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using ProtoBufJsonConverter;
using Stef.Validation;

namespace WireMock.Util;

/// <summary>

Check warning on line 13 in src/WireMock.Net/Util/WireMockProtoFileResolver.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/Util/WireMockProtoFileResolver.cs#L13

Added line #L13 was not covered by tests
/// 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>

Check warning on line 18 in src/WireMock.Net/Util/WireMockProtoFileResolver.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/Util/WireMockProtoFileResolver.cs#L18

Added line #L18 was not covered by tests
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;
}

Check warning on line 28 in src/WireMock.Net/Util/WireMockProtoFileResolver.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/Util/WireMockProtoFileResolver.cs#L28

Added line #L28 was not covered by tests

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);
}
}
}
Expand All @@ -42,5 +51,17 @@

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;

Check warning on line 63 in src/WireMock.Net/Util/WireMockProtoFileResolver.cs

View check run for this annotation

Codecov / codecov/patch

src/WireMock.Net/Util/WireMockProtoFileResolver.cs#L63

Added line #L63 was not covered by tests
return false;
}
}
#endif
41 changes: 41 additions & 0 deletions test/WireMock.Net.Tests/Grpc/ProtoBufUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions test/WireMock.Net.Tests/Grpc/greet1.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

import "request.proto";

package greet;

service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloReply {
string message = 1;
}
8 changes: 8 additions & 0 deletions test/WireMock.Net.Tests/Grpc/request.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// request.proto
syntax = "proto3";

package greet;

message HelloRequest {
string name = 1;
}
8 changes: 8 additions & 0 deletions test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Loading