Fix google protobuf WellKnownTypes: Empty, Duration and Timestamp (#1231)

* Fix google protobuf WellKnownTypes: Timestamp and Duration

* Fix protobuf Empty

* .

* small refactor

* 006

* fix

* policy

* ---

* <PackageReference Include="ProtoBufJsonConverter" Version="0.7.0" />
This commit is contained in:
Stef Heyenrath
2025-01-09 18:50:16 +01:00
committed by GitHub
parent 9c94324cff
commit 44c1c7aaa8
15 changed files with 413 additions and 79 deletions

View File

@@ -1,6 +1,5 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using JetBrains.Annotations;
using System.Collections.Generic; using System.Collections.Generic;
namespace WireMock.Handlers; namespace WireMock.Handlers;
@@ -21,69 +20,69 @@ public interface IFileSystemHandler
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <returns>true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.</returns> /// <returns>true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.</returns>
bool FolderExists([NotNull] string path); bool FolderExists(string path);
/// <summary> /// <summary>
/// Creates all directories and subdirectories in the specified path unless they already exist. /// Creates all directories and subdirectories in the specified path unless they already exist.
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
void CreateFolder([NotNull] string path); void CreateFolder(string path);
/// <summary> /// <summary>
/// Returns an enumerable collection of file names in a specified path. /// Returns an enumerable collection of file names in a specified path.
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <param name="includeSubdirectories">A value indicating whether subdirectories should also included when enumerating files.</param> /// <param name="includeSubdirectories">A value indicating whether subdirectories should also be included when enumerating files.</param>
/// <returns>An enumerable collection of the full names (including paths) for the files in the directory (and optionally subdirectories) specified by path.</returns> /// <returns>An enumerable collection of the full names (including paths) for the files in the directory (and optionally subdirectories) specified by path.</returns>
IEnumerable<string> EnumerateFiles([NotNull] string path, bool includeSubdirectories); IEnumerable<string> EnumerateFiles(string path, bool includeSubdirectories);
/// <summary> /// <summary>
/// Read a static mapping file as text. /// Read a static mapping file as text.
/// </summary> /// </summary>
/// <param name="path">The path (folder + filename with .json extension).</param> /// <param name="path">The path (folder + filename with .json extension).</param>
/// <returns>The file content as text.</returns> /// <returns>The file content as text.</returns>
string ReadMappingFile([NotNull] string path); string ReadMappingFile(string path);
/// <summary> /// <summary>
/// Write the static mapping file. /// Write the static mapping file.
/// </summary> /// </summary>
/// <param name="path">The path (folder + filename with .json extension).</param> /// <param name="path">The path (folder + filename with .json extension).</param>
/// <param name="text">The text.</param> /// <param name="text">The text.</param>
void WriteMappingFile([NotNull] string path, [NotNull] string text); void WriteMappingFile(string path, string text);
/// <summary> /// <summary>
/// Read a response body file as byte[]. /// Read a response body file as byte[].
/// </summary> /// </summary>
/// <param name="path">The path or filename from the file to read.</param> /// <param name="path">The path or filename from the file to read.</param>
/// <returns>The file content as bytes.</returns> /// <returns>The file content as bytes.</returns>
byte[] ReadResponseBodyAsFile([NotNull] string path); byte[] ReadResponseBodyAsFile(string path);
/// <summary> /// <summary>
/// Read a response body file as text. /// Read a response body file as text.
/// </summary> /// </summary>
/// <param name="path">The path or filename from the file to read.</param> /// <param name="path">The path or filename from the file to read.</param>
/// <returns>The file content as text.</returns> /// <returns>The file content as text.</returns>
string ReadResponseBodyAsString([NotNull] string path); string ReadResponseBodyAsString(string path);
/// <summary> /// <summary>
/// Delete a file. /// Delete a file.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
void DeleteFile([NotNull] string filename); void DeleteFile(string filename);
/// <summary> /// <summary>
/// Determines whether the given path refers to an existing file on disk. /// Determines whether the given path refers to an existing file on disk.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <returns>true if path refers to an existing file; false if the file does not exist.</returns> /// <returns>true if path refers to an existing file; false if the file does not exist.</returns>
bool FileExists([NotNull] string filename); bool FileExists(string filename);
/// <summary> /// <summary>
/// Write a file. /// Write a file.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <param name="bytes">The bytes.</param> /// <param name="bytes">The bytes.</param>
void WriteFile([NotNull] string filename, [NotNull] byte[] bytes); void WriteFile(string filename, byte[] bytes);
/// <summary> /// <summary>
/// Write a file. /// Write a file.
@@ -91,21 +90,21 @@ public interface IFileSystemHandler
/// <param name="folder">The folder.</param> /// <param name="folder">The folder.</param>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <param name="bytes">The bytes.</param> /// <param name="bytes">The bytes.</param>
void WriteFile([NotNull] string folder, [NotNull] string filename, [NotNull] byte[] bytes); void WriteFile(string folder, string filename, byte[] bytes);
/// <summary> /// <summary>
/// Read a file as bytes. /// Read a file as bytes.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <returns>The file content as bytes.</returns> /// <returns>The file content as bytes.</returns>
byte[] ReadFile([NotNull] string filename); byte[] ReadFile(string filename);
/// <summary> /// <summary>
/// Read a file as string. /// Read a file as string.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <returns>The file content as a string.</returns> /// <returns>The file content as a string.</returns>
string ReadFileAsString([NotNull] string filename); string ReadFileAsString(string filename);
/// <summary> /// <summary>
/// Gets the folder where the unmatched requests should be stored. For local file system, this would be `{CurrentFolder}/requests/unmatched`. /// Gets the folder where the unmatched requests should be stored. For local file system, this would be `{CurrentFolder}/requests/unmatched`.
@@ -114,9 +113,9 @@ public interface IFileSystemHandler
string GetUnmatchedRequestsFolder(); string GetUnmatchedRequestsFolder();
/// <summary> /// <summary>
/// Write a unmatched request to the Unmatched RequestsFolder. /// Write an unmatched request to the Unmatched RequestsFolder.
/// </summary> /// </summary>
/// <param name="filename">The filename.</param> /// <param name="filename">The filename.</param>
/// <param name="text">The text.</param> /// <param name="text">The text.</param>
void WriteUnmatchedRequest([NotNull] string filename, [NotNull] string text); void WriteUnmatchedRequest(string filename, string text);
} }

View File

@@ -1,12 +1,12 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
#pragma warning disable CS1591
using WireMock.Extensions; using WireMock.Extensions;
using WireMock.Matchers; using WireMock.Matchers;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions; namespace WireMock.FluentAssertions;
#pragma warning disable CS1591
public partial class WireMockAssertions public partial class WireMockAssertions
{ {
[CustomAssertion] [CustomAssertion]

View File

@@ -1,6 +1,6 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq // Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq which is copied from https://github.com/StefH/JsonConverter
using System; using System;
using System.Collections; using System.Collections;
@@ -14,9 +14,7 @@ namespace WireMock.Json;
internal static class JObjectExtensions internal static class JObjectExtensions
{ {
private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonClassOptions?, object?>> private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonClassOptions?, object?>>;
{
}
private static readonly JTokenResolvers Resolvers = new() private static readonly JTokenResolvers Resolvers = new()
{ {
@@ -180,7 +178,7 @@ internal static class JObjectExtensions
private static IEnumerable ConvertToTypedArray(IEnumerable<object?> src, Type newType) private static IEnumerable ConvertToTypedArray(IEnumerable<object?> src, Type newType)
{ {
var method = ConvertToTypedArrayGenericMethod.MakeGenericMethod(newType); var method = ConvertToTypedArrayGenericMethod.MakeGenericMethod(newType);
return (IEnumerable)method.Invoke(null, new object[] { src })!; return (IEnumerable)method.Invoke(null, [src])!;
} }
private static readonly MethodInfo ConvertToTypedArrayGenericMethod = typeof(JObjectExtensions).GetMethod(nameof(ConvertToTypedArrayGeneric), BindingFlags.NonPublic | BindingFlags.Static)!; private static readonly MethodInfo ConvertToTypedArrayGenericMethod = typeof(JObjectExtensions).GetMethod(nameof(ConvertToTypedArrayGeneric), BindingFlags.NonPublic | BindingFlags.Static)!;
@@ -193,7 +191,7 @@ internal static class JObjectExtensions
public static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue, bool createParameterCtor = true) public static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue, bool createParameterCtor = true)
{ {
var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray(), createParameterCtor); var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray(), createParameterCtor);
var dynamicClass = (DynamicClass)Activator.CreateInstance(type); var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!;
foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null)) foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
{ {
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!); dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);

View File

@@ -152,7 +152,7 @@ namespace WireMock.Owin.Mappers
#if PROTOBUF #if PROTOBUF
case BodyType.ProtoBuf: case BodyType.ProtoBuf:
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts; var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, responseMessage.BodyData.ProtoBufMessageType, responseMessage.BodyData.BodyAsJson).ConfigureAwait(false); return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
#endif #endif
case BodyType.Bytes: case BodyType.Bytes:

View File

@@ -1,7 +1,6 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request; using WireMock.Matchers.Request;
using WireMock.Models; using WireMock.Models;

View File

@@ -242,7 +242,7 @@ public partial class Response
Guard.NotNull(value); Guard.NotNull(value);
#if !PROTOBUF #if !PROTOBUF
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else #else
ResponseMessage.BodyDestination = null; ResponseMessage.BodyDestination = null;
ResponseMessage.BodyData = new BodyData ResponseMessage.BodyData = new BodyData
@@ -252,8 +252,9 @@ public partial class Response
ProtoDefinition = () => new IdOrTexts(null, protoDefinitions), ProtoDefinition = () => new IdOrTexts(null, protoDefinitions),
ProtoBufMessageType = messageType ProtoBufMessageType = messageType
}; };
#endif
return this; return this;
#endif
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -268,7 +269,7 @@ public partial class Response
Guard.NotNull(value); Guard.NotNull(value);
#if !PROTOBUF #if !PROTOBUF
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else #else
ResponseMessage.BodyDestination = null; ResponseMessage.BodyDestination = null;
ResponseMessage.BodyData = new BodyData ResponseMessage.BodyData = new BodyData
@@ -278,7 +279,8 @@ public partial class Response
ProtoDefinition = () => Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."), ProtoDefinition = () => Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."),
ProtoBufMessageType = messageType ProtoBufMessageType = messageType
}; };
#endif
return this; return this;
#endif
} }
} }

View File

@@ -26,7 +26,7 @@ internal static class ProtoBufUtils
} }
var resolver = new WireMockProtoFileResolver(protoDefinitions); var resolver = new WireMockProtoFileResolver(protoDefinitions);
var request = new ConvertToProtoBufRequest(protoDefinitions[0], messageType, value, true) var request = new ConvertToProtoBufRequest(protoDefinitions[0], messageType!, value, true)
.WithProtoFileResolver(resolver); .WithProtoFileResolver(resolver);
return await SingletonFactory<Converter> return await SingletonFactory<Converter>

View File

@@ -146,7 +146,7 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.3' and '$(TargetFramework)' != 'net451' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net46' and '$(TargetFramework)' != 'net461'"> <ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.3' and '$(TargetFramework)' != 'net451' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net46' and '$(TargetFramework)' != 'net461'">
<PackageReference Include="GraphQL.NewtonsoftJson" Version="8.2.1" /> <PackageReference Include="GraphQL.NewtonsoftJson" Version="8.2.1" />
<PackageReference Include="MimeKitLite" Version="4.1.0.1" /> <PackageReference Include="MimeKitLite" Version="4.1.0.1" />
<PackageReference Include="ProtoBufJsonConverter" Version="0.5.0" /> <PackageReference Include="ProtoBufJsonConverter" Version="0.7.0" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' "> <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
@@ -154,6 +154,7 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,10 +1,12 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
using System; using System;
using System.Diagnostics.CodeAnalysis;
using Xunit; using Xunit;
namespace WireMock.Net.Tests.Facts; namespace WireMock.Net.Tests.Facts;
[ExcludeFromCodeCoverage]
public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute
{ {
private const string SkipReason = "Ignore when run via CI/CD"; private const string SkipReason = "Ignore when run via CI/CD";

View File

@@ -1,11 +1,13 @@
// Copyright © WireMock.Net // Copyright © WireMock.Net
#if NET6_0_OR_GREATER #if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using WireMock.Net.Testcontainers.Utils; using WireMock.Net.Testcontainers.Utils;
using Xunit; using Xunit;
namespace WireMock.Net.Tests.Facts; namespace WireMock.Net.Tests.Facts;
[ExcludeFromCodeCoverage]
public sealed class RunOnDockerPlatformFact : FactAttribute public sealed class RunOnDockerPlatformFact : FactAttribute
{ {
public RunOnDockerPlatformFact(string platform) public RunOnDockerPlatformFact(string platform)

View File

@@ -7,8 +7,10 @@ using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading.Tasks; using System.Threading.Tasks;
using FluentAssertions; using FluentAssertions;
using Google.Protobuf.WellKnownTypes;
using Greet; using Greet;
using Grpc.Net.Client; using Grpc.Net.Client;
using NarrowIntegrationTest.Lookup;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
@@ -17,6 +19,7 @@ using Xunit;
// ReSharper disable once CheckNamespace // ReSharper disable once CheckNamespace
namespace WireMock.Net.Tests; namespace WireMock.Net.Tests;
public partial class WireMockServerTests public partial class WireMockServerTests
{ {
private const string ProtoDefinition = @" private const string ProtoDefinition = @"
@@ -34,6 +37,12 @@ message HelloRequest {
message HelloReply { message HelloReply {
string message = 1; string message = 1;
enum PhoneType {
none = 0;
mobile = 1;
home = 2;
}
PhoneType phoneType = 2;
} }
"; ";
@@ -48,6 +57,8 @@ import ""google/protobuf/duration.proto"";
service Greeter { service Greeter {
rpc SayNothing (google.protobuf.Empty) returns (google.protobuf.Empty); rpc SayNothing (google.protobuf.Empty) returns (google.protobuf.Empty);
rpc SayTimestamp (MyMessageTimestamp) returns (MyMessageTimestamp);
rpc SayDuration (MyMessageDuration) returns (MyMessageDuration);
} }
message MyMessageTimestamp { message MyMessageTimestamp {
@@ -128,8 +139,6 @@ message Other {
// Assert // Assert
response.StatusCode.Should().Be(HttpStatusCode.OK); response.StatusCode.Should().Be(HttpStatusCode.OK);
server.Stop();
} }
[Theory] [Theory]
@@ -171,23 +180,18 @@ message Other {
var responseBytes = await response.Content.ReadAsByteArrayAsync(); var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv"); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");
server.Stop();
} }
[Fact] [Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes() public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Empty()
{ {
// Arrange // Arrange
var bytes = Convert.FromBase64String("CgRzdGVm");
using var server = WireMockServer.Start(); using var server = WireMockServer.Start();
server server
.Given(Request.Create() .Given(Request.Create()
.UsingPost() .UsingPost()
.WithPath("/grpc/Greeter/SayNothing") .WithPath("/grpc/Greeter/SayNothing")
.WithBody(new NotNullOrEmptyMatcher())
) )
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithBodyAsProtoBuf(ProtoDefinitionWithWellKnownTypes, "google.protobuf.Empty", .WithBodyAsProtoBuf(ProtoDefinitionWithWellKnownTypes, "google.protobuf.Empty",
@@ -198,6 +202,7 @@ message Other {
); );
// Act // Act
var bytes = Convert.FromBase64String("CgRzdGVm");
var protoBuf = new ByteArrayContent(bytes); var protoBuf = new ByteArrayContent(bytes);
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web"); protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
@@ -208,9 +213,91 @@ message Other {
response.StatusCode.Should().Be(HttpStatusCode.OK); response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseBytes = await response.Content.ReadAsByteArrayAsync(); var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be(""); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAA=");
}
server.Stop(); [Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Timestamp()
{
// Arrange
using var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingPost()
.WithPath("/grpc/Greeter/SayTimestamp")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithBodyAsProtoBuf(ProtoDefinitionWithWellKnownTypes, "communication.api.v1.MyMessageTimestamp",
new
{
ts = new
{
Seconds = 1722301323,
Nanos = 12300
}
}
)
.WithTrailingHeader("grpc-status", "0")
.WithTransformer()
);
// Act
var bytes = Convert.FromBase64String("CgkIi/egtQYQuWA=");
var protoBuf = new ByteArrayContent(bytes);
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
var client = server.CreateClient();
var response = await client.PostAsync("/grpc/Greeter/SayTimestamp", protoBuf);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAsKCQiL96C1BhCMYA==");
}
[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Duration()
{
// Arrange
using var server = WireMockServer.Start();
server
.Given(Request.Create()
.UsingPost()
.WithPath("/grpc/Greeter/SayDuration")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithBodyAsProtoBuf(ProtoDefinitionWithWellKnownTypes, "communication.api.v1.MyMessageDuration",
new
{
du = new
{
Seconds = 1722301323,
Nanos = 12300
}
}
)
.WithTrailingHeader("grpc-status", "0")
.WithTransformer()
);
// Act
var bytes = Convert.FromBase64String("CgkIi/egtQYQuWA=");
var protoBuf = new ByteArrayContent(bytes);
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
var client = server.CreateClient();
var response = await client.PostAsync("/grpc/Greeter/SayDuration", protoBuf);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAsKCQiL96C1BhCMYA==");
} }
[Fact] [Fact]
@@ -228,7 +315,6 @@ message Other {
.Given(Request.Create() .Given(Request.Create()
.UsingPost() .UsingPost()
.WithPath("/grpc/Greeter/SayNothing") .WithPath("/grpc/Greeter/SayNothing")
.WithBody(new NotNullOrEmptyMatcher())
) )
.WithProtoDefinition(id) .WithProtoDefinition(id)
.RespondWith(Response.Create() .RespondWith(Response.Create()
@@ -250,9 +336,7 @@ message Other {
response.StatusCode.Should().Be(HttpStatusCode.OK); response.StatusCode.Should().Be(HttpStatusCode.OK);
var responseBytes = await response.Content.ReadAsByteArrayAsync(); var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be(""); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAA=");
server.Stop();
} }
[Fact] [Fact]
@@ -294,8 +378,6 @@ message Other {
var responseBytes = await response.Content.ReadAsByteArrayAsync(); var responseBytes = await response.Content.ReadAsByteArrayAsync();
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv"); Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");
server.Stop();
} }
[Fact] [Fact]
@@ -326,15 +408,12 @@ message Other {
// Act // Act
var channel = GrpcChannel.ForAddress(server.Url!); var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel); var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" }); var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" });
// Assert // Assert
reply.Message.Should().Be("hello stef POST"); reply.Message.Should().Be("hello stef POST");
server.Stop();
} }
[Fact] [Fact]
@@ -367,15 +446,12 @@ message Other {
// Act // Act
var channel = GrpcChannel.ForAddress(server.Url!); var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel); var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" }); var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" });
// Assert // Assert
reply.Message.Should().Be("hello stef POST"); reply.Message.Should().Be("hello stef POST");
server.Stop();
} }
[Fact] [Fact]
@@ -411,15 +487,214 @@ message Other {
// Act // Act
var channel = GrpcChannel.ForAddress(server.Url!); var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel); var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" }); var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" });
// Assert // Assert
reply.Message.Should().Be("hello stef POST"); reply.Message.Should().Be("hello stef POST");
}
server.Stop(); [Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Empty_UsingGrpcGeneratedClient()
{
// Arrange
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
using var server = WireMockServer.Start(useHttp2: true);
server
.Given(Request.Create()
.UsingPost()
.WithPath("/greet.Greeter/SayNothing")
)
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/grpc")
.WithTrailingHeader("grpc-status", "0")
.WithBodyAsProtoBuf(definition, "google.protobuf.Empty",
new { }
)
);
// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayNothingAsync(new Empty());
// Assert
reply.Should().Be(new Empty());
}
[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Timestamp_UsingGrpcGeneratedClient()
{
// Arrange
const int seconds = 1722301323;
const int nanos = 12300;
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
using var server = WireMockServer.Start(useHttp2: true);
server
.Given(Request.Create()
.UsingPost()
.WithPath("/greet.Greeter/SayTimestamp")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/grpc")
.WithTrailingHeader("grpc-status", "0")
.WithBodyAsProtoBuf(definition, "greet.MyMessageTimestamp",
new MyMessageTimestamp
{
Ts = new Timestamp
{
Seconds = seconds,
Nanos = nanos
}
}
)
);
// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayTimestampAsync(new MyMessageTimestamp { Ts = Timestamp.FromDateTime(DateTime.UtcNow) });
// Assert
reply.Ts.Should().Be(new Timestamp { Seconds = seconds, Nanos = nanos });
}
[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Duration_UsingGrpcGeneratedClient()
{
// Arrange
const int seconds = 1722301323;
const int nanos = 12300;
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
using var server = WireMockServer.Start(useHttp2: true);
server
.Given(Request.Create()
.UsingPost()
.WithPath("/greet.Greeter/SayDuration")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/grpc")
.WithTrailingHeader("grpc-status", "0")
.WithBodyAsProtoBuf(definition, "greet.MyMessageDuration",
new MyMessageDuration
{
Du = new Duration
{
Seconds = seconds,
Nanos = nanos
}
}
)
);
// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayDurationAsync(new MyMessageDuration { Du = Duration.FromTimeSpan(TimeSpan.MinValue) });
// Assert
reply.Du.Should().Be(new Duration { Seconds = seconds, Nanos = nanos });
}
[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_Enum_UsingGrpcGeneratedClient()
{
// Arrange
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
using var server = WireMockServer.Start(useHttp2: true);
server
.Given(Request.Create()
.UsingPost()
.WithPath("/greet.Greeter/SayHello")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/grpc")
.WithTrailingHeader("grpc-status", "0")
.WithBodyAsProtoBuf(definition, "greet.HelloReply",
new HelloReply
{
Message = "hello",
PhoneType = HelloReply.Types.PhoneType.Home
}
)
);
// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(new HelloRequest());
// Assert
reply.Message.Should().Be("hello");
reply.PhoneType.Should().Be(HelloReply.Types.PhoneType.Home);
}
[Fact]
public async Task WireMockServer_WithBodyAsProtoBuf_Enum_UsingPolicyGrpcGeneratedClient()
{
// Arrange
const int seconds = 1722301323;
const int nanos = 12300;
const string version = "test";
const string correlationId = "correlation";
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/policy.proto");
using var server = WireMockServer.Start(useHttp2: true);
server
.Given(Request.Create()
.UsingPost()
.WithPath("/Policy.PolicyService/GetVersion")
.WithBody(new NotNullOrEmptyMatcher())
)
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/grpc")
.WithTrailingHeader("grpc-status", "0")
.WithBodyAsProtoBuf(definition, "NarrowIntegrationTest.Lookup.GetVersionResponse",
new GetVersionResponse
{
Version = version,
DateHired = new Timestamp
{
Seconds = seconds,
Nanos = nanos
},
Client = new NarrowIntegrationTest.Lookup.Client
{
ClientName = NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter,
CorrelationId = correlationId
}
}
)
);
// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var client = new PolicyService.PolicyServiceClient(channel);
var reply = await client.GetVersionAsync(new GetVersionRequest());
// Assert
reply.Version.Should().Be(version);
reply.DateHired.Should().Be(new Timestamp { Seconds = seconds, Nanos = nanos });
reply.Client.ClientName.Should().Be(NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter);
reply.Client.CorrelationId.Should().Be(correlationId);
} }
} }
#endif #endif

View File

@@ -1,33 +1,41 @@
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3"; syntax = "proto3";
package greet; package greet;
// The greeting service definition. import "google/protobuf/empty.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";
service Greeter { service Greeter {
// Sends a greeting rpc SayNothing (google.protobuf.Empty) returns (google.protobuf.Empty);
rpc SayHello (HelloRequest) returns (HelloReply); rpc SayHello (HelloRequest) returns (HelloReply);
rpc SayEmpty (MyMessageEmpty) returns (MyMessageEmpty);
rpc SayTimestamp (MyMessageTimestamp) returns (MyMessageTimestamp);
rpc SayDuration (MyMessageDuration) returns (MyMessageDuration);
} }
// The request message containing the user's name.
message HelloRequest { message HelloRequest {
string name = 1; string name = 1;
} }
// The response message containing the greetings
message HelloReply { message HelloReply {
string message = 1; string message = 1;
enum PhoneType {
none = 0;
mobile = 1;
home = 2;
}
PhoneType phoneType = 2;
}
message MyMessageTimestamp {
google.protobuf.Timestamp ts = 1;
}
message MyMessageDuration {
google.protobuf.Duration du = 1;
}
message MyMessageEmpty {
google.protobuf.Empty e = 1;
} }

View File

@@ -0,0 +1,40 @@
syntax = "proto3";
option csharp_namespace = "NarrowIntegrationTest.Lookup";
import "google/protobuf/timestamp.proto";
package Policy;
service PolicyService {
rpc GetVersion (GetVersionRequest) returns (GetVersionResponse);
}
message GetVersionRequest {
Client Client = 1;
}
message GetVersionResponse {
string Version = 1;
google.protobuf.Timestamp DateHired = 2;
Client Client = 3;
}
message Client {
string CorrelationId = 1;
enum Clients {
Unknown = 0;
QMS = 1;
BillingCenter = 2;
PAS = 3;
Payroll = 4;
Portal = 5;
SFO = 6;
QuoteAndBind = 7;
LegacyConversion = 8;
BindNow = 9;
PaymentPortal = 10 ;
PricingEngine = 11;
}
Clients ClientName = 2;
}

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<Authors>Stef Heyenrath</Authors> <Authors>Stef Heyenrath</Authors>
<TargetFrameworks>net452;net461;netcoreapp3.1;net6.0;net7.0;net8.0</TargetFrameworks> <TargetFrameworks>net452;net461;netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<DebugType>full</DebugType> <DebugType>full</DebugType>
@@ -113,6 +113,7 @@
</PackageReference> </PackageReference>
<Protobuf Include="Grpc\greet.proto" GrpcServices="Client" /> <Protobuf Include="Grpc\greet.proto" GrpcServices="Client" />
<Protobuf Include="Grpc\policy.proto" GrpcServices="Client" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'"> <ItemGroup Condition="'$(TargetFramework)' == 'net6.0' or '$(TargetFramework)' == 'net7.0' or '$(TargetFramework)' == 'net8.0'">
@@ -132,8 +133,13 @@
<None Update="cert.pem"> <None Update="cert.pem">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="Grpc\policy.proto">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<GrpcServices>Client</GrpcServices>
</None>
<None Update="Grpc\greet.proto"> <None Update="Grpc\greet.proto">
<GrpcServices>Client</GrpcServices> <GrpcServices>Client</GrpcServices>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
<None Update="responsebody.json"> <None Update="responsebody.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@@ -215,10 +215,10 @@ public partial class WireMockServerTests
{ {
// Arrange // Arrange
var port = PortUtils.FindFreeTcpPort(); var port = PortUtils.FindFreeTcpPort();
var IPv4 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetwork); var IPv4 = GetIPAddressesByFamily(AddressFamily.InterNetwork);
var settings = new WireMockServerSettings var settings = new WireMockServerSettings
{ {
Urls = ["http://0.0.0.0:" + port], Urls = ["http://0.0.0.0:" + port]
}; };
using var server = WireMockServer.Start(settings); using var server = WireMockServer.Start(settings);
@@ -234,15 +234,16 @@ public partial class WireMockServerTests
} }
} }
#if NET8_0_OR_GREATER
[IgnoreOnContinuousIntegrationFact] [IgnoreOnContinuousIntegrationFact]
public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6() public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv6()
{ {
// Arrange // Arrange
var port = PortUtils.FindFreeTcpPort(); var port = PortUtils.FindFreeTcpPort();
var IPv6 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetworkV6); var IPv6 = GetIPAddressesByFamily(AddressFamily.InterNetworkV6);
var settings = new WireMockServerSettings var settings = new WireMockServerSettings
{ {
Urls = ["http://0.0.0.0:" + port], Urls = ["http://0.0.0.0:" + port]
}; };
using var server = WireMockServer.Start(settings); using var server = WireMockServer.Start(settings);
@@ -257,6 +258,7 @@ public partial class WireMockServerTests
response.Should().Be("x"); response.Should().Be("x");
} }
} }
#endif
#endif #endif
[Fact] [Fact]