mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 07:51:41 +02:00
Use latest ProtoBufJsonConverter to support WellKnownTypes (#1161)
* Use latest ProtoBufJsonConverter to support WellKnownTypes * Fix * 02 * WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes * . * extra test * 0.4.0-preview-06 * 7 * <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0-preview-08" /> * Update README.md * <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0-preview-09" /> * <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0" /> * Update README.md
This commit is contained in:
@@ -35,6 +35,69 @@ message HelloRequest {
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
";
|
||||
|
||||
private const string ProtoDefinitionWithWellKnownTypes = @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package communication.api.v1;
|
||||
|
||||
import ""google/protobuf/empty.proto"";
|
||||
import ""google/protobuf/timestamp.proto"";
|
||||
import ""google/protobuf/duration.proto"";
|
||||
|
||||
service Greeter {
|
||||
rpc SayNothing (google.protobuf.Empty) returns (google.protobuf.Empty);
|
||||
}
|
||||
|
||||
message MyMessageTimestamp {
|
||||
google.protobuf.Timestamp ts = 1;
|
||||
}
|
||||
|
||||
message MyMessageDuration {
|
||||
google.protobuf.Duration du = 1;
|
||||
}
|
||||
";
|
||||
|
||||
private const string ProtoDefinitionMain = @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
import ""other.proto"";
|
||||
import ""google/protobuf/empty.proto"";
|
||||
|
||||
service Greeter {
|
||||
rpc Nothing (google.protobuf.Empty) returns (google.protobuf.Empty);
|
||||
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
|
||||
rpc SayOther (Other) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
message Person {
|
||||
string name = 1;
|
||||
int32 id = 2;
|
||||
string email = 3;
|
||||
}
|
||||
";
|
||||
|
||||
private const string ProtoDefinitionOther = @"// other.proto
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
message Other {
|
||||
string name = 1;
|
||||
}
|
||||
";
|
||||
|
||||
[Theory]
|
||||
@@ -80,6 +143,129 @@ message HelloReply {
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Convert.FromBase64String("CgRzdGVm");
|
||||
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/grpc/Greeter/SayNothing")
|
||||
.WithBody(new NotNullOrEmptyMatcher())
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithBodyAsProtoBuf(ProtoDefinitionWithWellKnownTypes, "google.protobuf.Empty",
|
||||
new { }
|
||||
)
|
||||
.WithTrailingHeader("grpc-status", "0")
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
// Act
|
||||
var protoBuf = new ByteArrayContent(bytes);
|
||||
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
|
||||
|
||||
var client = server.CreateClient();
|
||||
var response = await client.PostAsync("/grpc/Greeter/SayNothing", protoBuf);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var responseBytes = await response.Content.ReadAsByteArrayAsync();
|
||||
|
||||
Convert.ToBase64String(responseBytes).Should().Be("");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinition_WithWellKnownTypes()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Convert.FromBase64String("CgRzdGVm");
|
||||
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
var id = $"proto-{Guid.NewGuid()}";
|
||||
|
||||
server
|
||||
.AddProtoDefinition(id, ProtoDefinitionWithWellKnownTypes)
|
||||
.Given(Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/grpc/Greeter/SayNothing")
|
||||
.WithBody(new NotNullOrEmptyMatcher())
|
||||
)
|
||||
.WithProtoDefinition(id)
|
||||
.RespondWith(Response.Create()
|
||||
.WithBodyAsProtoBuf("google.protobuf.Empty",
|
||||
new { }
|
||||
)
|
||||
.WithTrailingHeader("grpc-status", "0")
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
// Act
|
||||
var protoBuf = new ByteArrayContent(bytes);
|
||||
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
|
||||
|
||||
var client = server.CreateClient();
|
||||
var response = await client.PostAsync("/grpc/Greeter/SayNothing", protoBuf);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var responseBytes = await response.Content.ReadAsByteArrayAsync();
|
||||
|
||||
Convert.ToBase64String(responseBytes).Should().Be("");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_MultipleFiles()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Convert.FromBase64String("CgRzdGVm");
|
||||
var jsonMatcher = new JsonMatcher(new { name = "stef" });
|
||||
|
||||
using var server = WireMockServer.Start();
|
||||
|
||||
var protoFiles = new [] { ProtoDefinitionMain, ProtoDefinitionOther };
|
||||
|
||||
server
|
||||
.Given(Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/grpc/greet.Greeter/SayOther")
|
||||
.WithBodyAsProtoBuf(protoFiles, "greet.Other", jsonMatcher)
|
||||
)
|
||||
.RespondWith(Response.Create()
|
||||
.WithBodyAsProtoBuf(protoFiles, "greet.HelloReply",
|
||||
new
|
||||
{
|
||||
message = "hello"
|
||||
}
|
||||
)
|
||||
.WithTrailingHeader("grpc-status", "0")
|
||||
);
|
||||
|
||||
// Act
|
||||
var protoBuf = new ByteArrayContent(bytes);
|
||||
protoBuf.Headers.ContentType = new MediaTypeHeaderValue("application/grpc-web");
|
||||
|
||||
var client = server.CreateClient();
|
||||
var response = await client.PostAsync("/grpc/greet.Greeter/SayOther", protoBuf);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var responseBytes = await response.Content.ReadAsByteArrayAsync();
|
||||
|
||||
Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_InlineProtoDefinition_UsingGrpcGeneratedClient()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user