mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 22:50:05 +02:00
Add Grpc ProtoBuf support (request-response) (#1047)
* ProtoBuf
* .
* x
* ---
* x
* fx
* ...
* sc
* ...
* .
* groen
* x
* fix tests
* ok!?
* fix tests
* fix tests
* !
* x
* 6
* .
* x
* ivaluematcher
* transformer
* .
* sc
* .
* mapping
* x
* tra
* com
* ...
* .
* .
* .
* AddProtoDefinition
* .
* set
* grpahj
* .
* .
* IdOrText
* ...
* async
* async2
* .
* t
* nuget
* <PackageReference Include="ProtoBufJsonConverter" Version="0.2.0-preview-04" />
* http version
* tests
* .WithHttpVersion("2")
* <PackageReference Include="ProtoBufJsonConverter" Version="0.2.0" />
* HttpVersionParser
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: post,
|
||||
HttpVersion: 1.1,
|
||||
BodyAsBytes: AA==,
|
||||
DetectedBodyType: Bytes
|
||||
},
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: get
|
||||
Method: get,
|
||||
HttpVersion: 1.1
|
||||
},
|
||||
Response: {
|
||||
BodyAsFile: test,
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: post,
|
||||
HttpVersion: 1.1,
|
||||
BodyAsBytes: AA==,
|
||||
DetectedBodyType: Bytes
|
||||
},
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
AbsolutePath: /,
|
||||
Url: http://localhost/,
|
||||
AbsoluteUrl: http://localhost/,
|
||||
Method: get
|
||||
Method: get,
|
||||
HttpVersion: 1.1
|
||||
},
|
||||
Response: {
|
||||
BodyAsFile: test,
|
||||
|
||||
@@ -102,7 +102,7 @@ public partial class MappingConverterTests
|
||||
return Verifier.Verify(code, VerifySettings);
|
||||
}
|
||||
|
||||
private Mapping CreateMapping()
|
||||
private IMapping CreateMapping()
|
||||
{
|
||||
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
|
||||
var request = Request.Create()
|
||||
@@ -119,7 +119,8 @@ public partial class MappingConverterTests
|
||||
.WithDelay(12345)
|
||||
.WithTransformer();
|
||||
|
||||
return new Mapping(
|
||||
return new Mapping
|
||||
(
|
||||
guid,
|
||||
_updatedAt,
|
||||
string.Empty,
|
||||
@@ -136,9 +137,8 @@ public partial class MappingConverterTests
|
||||
null,
|
||||
false,
|
||||
null,
|
||||
data: null,
|
||||
probability: 0.3
|
||||
);
|
||||
data: null
|
||||
).WithProbability(0.3);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,61 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 41,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: /grpc/greet.Greeter/SayHello,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: ProtoBufMatcher,
|
||||
ContentMatcher: {
|
||||
Name: JsonMatcher,
|
||||
Pattern: {
|
||||
name: stef
|
||||
},
|
||||
IgnoreCase: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello
|
||||
},
|
||||
TrailingHeaders: {
|
||||
grpc-status: 0
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloReply
|
||||
},
|
||||
UseWebhooksFireAndForget: false,
|
||||
ProtoDefinition:
|
||||
syntax = "proto3";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 41,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: /grpc/greet.Greeter/SayHello,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: ProtoBufMatcher,
|
||||
Pattern:
|
||||
syntax = "proto3";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
,
|
||||
ContentMatcher: {
|
||||
Name: JsonMatcher,
|
||||
Pattern: {
|
||||
name: stef
|
||||
},
|
||||
IgnoreCase: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Title: ,
|
||||
Description: ,
|
||||
Priority: 43,
|
||||
Request: {},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello
|
||||
},
|
||||
TrailingHeaders: {
|
||||
grpc-status: 0
|
||||
},
|
||||
ProtoDefinition:
|
||||
syntax = "proto3";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
,
|
||||
ProtoBufMessageType: greet.HelloReply
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Request: {},
|
||||
Response: {
|
||||
Headers: {
|
||||
w[]: [
|
||||
x,
|
||||
y
|
||||
]
|
||||
}
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Request: {},
|
||||
Response: {
|
||||
Headers: {
|
||||
w[]: [
|
||||
x,
|
||||
y
|
||||
]
|
||||
}
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Request: {},
|
||||
Response: {
|
||||
TrailingHeaders: {
|
||||
x1: y,
|
||||
x2: [
|
||||
y,
|
||||
z
|
||||
]
|
||||
}
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTime_1,
|
||||
Request: {},
|
||||
Response: {
|
||||
TrailingHeaders: {
|
||||
w[]: [
|
||||
x,
|
||||
y
|
||||
]
|
||||
}
|
||||
},
|
||||
UseWebhooksFireAndForget: false
|
||||
}
|
||||
@@ -22,6 +22,23 @@ public partial class MappingConverterTests
|
||||
private readonly Guid _guid = new("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
|
||||
private readonly DateTime _updatedAt = new(2022, 12, 4, 11, 12, 13);
|
||||
private readonly WireMockServerSettings _settings = new();
|
||||
private const string ProtoDefinition = @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
";
|
||||
|
||||
private readonly MappingConverter _sut;
|
||||
|
||||
@@ -58,7 +75,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -131,7 +148,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
}
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -169,7 +186,7 @@ public partial class MappingConverterTests
|
||||
var description = "my-description";
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -189,7 +206,7 @@ public partial class MappingConverterTests
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -218,7 +235,7 @@ public partial class MappingConverterTests
|
||||
End = end,
|
||||
TTL = ttl
|
||||
};
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -249,7 +266,7 @@ public partial class MappingConverterTests
|
||||
{
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(test.Delay);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -267,7 +284,7 @@ public partial class MappingConverterTests
|
||||
var delay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithDelay(delay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -287,7 +304,7 @@ public partial class MappingConverterTests
|
||||
int minimumDelay = 1000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -310,7 +327,7 @@ public partial class MappingConverterTests
|
||||
int maximumDelay = 2000;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -332,7 +349,8 @@ public partial class MappingConverterTests
|
||||
double probability = 0.4;
|
||||
var request = Request.Create();
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: probability);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null)
|
||||
.WithProbability(probability);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -351,7 +369,7 @@ public partial class MappingConverterTests
|
||||
// Arrange
|
||||
var request = Request.Create().WithClientIP("1.2.3.4");
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -364,7 +382,7 @@ public partial class MappingConverterTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_WithHeader_And_Cookie_ReturnsCorrectModel()
|
||||
public Task ToMappingModel_Request_WithHeader_And_Cookie_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create()
|
||||
@@ -380,8 +398,10 @@ public partial class MappingConverterTests
|
||||
.WithCookie("IgnoreCase_true", "cv-4")
|
||||
.WithCookie("ExactMatcher", new ExactMatcher("c-exact"))
|
||||
;
|
||||
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -393,6 +413,98 @@ public partial class MappingConverterTests
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_Response_WithHeader_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
|
||||
var response = Response.Create()
|
||||
.WithHeader("x1", "y")
|
||||
.WithHeader("x2", "y", "z")
|
||||
.WithHeaders(new Dictionary<string, string> { { "d", "test" } })
|
||||
.WithHeaders(new Dictionary<string, string[]> { { "d[]", new[] { "v1", "v2" } } })
|
||||
.WithHeaders(new Dictionary<string, WireMockList<string>> { { "w", new WireMockList<string>("x") } })
|
||||
.WithHeaders(new Dictionary<string, WireMockList<string>> { { "w[]", new WireMockList<string>("x", "y") } });
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_Response_WithHeaders_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
|
||||
var response = Response.Create()
|
||||
.WithHeaders(new Dictionary<string, WireMockList<string>> { { "w[]", new WireMockList<string>("x", "y") } });
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
#if TRAILINGHEADERS
|
||||
[Fact]
|
||||
public Task ToMappingModel_Response_WithTrailingHeader_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
|
||||
var response = Response.Create()
|
||||
.WithTrailingHeader("x1", "y")
|
||||
.WithTrailingHeader("x2", "y", "z");
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_Response_WithTrailingHeaders_ReturnsCorrectModel()
|
||||
{
|
||||
// Assign
|
||||
var request = Request.Create();
|
||||
|
||||
var response = Response.Create()
|
||||
.WithTrailingHeaders(new Dictionary<string, WireMockList<string>> { { "w[]", new WireMockList<string>("x", "y") } });
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_WithParam_ReturnsCorrectModel()
|
||||
{
|
||||
@@ -405,7 +517,7 @@ public partial class MappingConverterTests
|
||||
.WithParam("ExactMatcher", new ExactMatcher("exact"))
|
||||
;
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
@@ -422,7 +534,7 @@ public partial class MappingConverterTests
|
||||
public Task ToMappingModel_Request_WithBodyAsGraphQLSchema_ReturnsCorrectModel()
|
||||
{
|
||||
// Arrange
|
||||
var schema = @"
|
||||
const string schema = @"
|
||||
type Query {
|
||||
greeting:String
|
||||
students:[Student]
|
||||
@@ -437,7 +549,96 @@ public partial class MappingConverterTests
|
||||
}";
|
||||
var request = Request.Create().WithBodyAsGraphQLSchema(schema);
|
||||
var response = Response.Create();
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null, null);
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
[Fact]
|
||||
public Task ToMappingModel_Request_WithBodyAsProtoBuf_ReturnsCorrectModel()
|
||||
{
|
||||
// Arrange
|
||||
var jsonMatcher = new JsonMatcher(new { name = "stef" });
|
||||
|
||||
var request = Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/grpc/greet.Greeter/SayHello")
|
||||
.WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloRequest", jsonMatcher);
|
||||
|
||||
var response = Response.Create();
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 41, null, null, null, null, null, false, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_Response_WithBodyAsProtoBuf_ReturnsCorrectModel()
|
||||
{
|
||||
// Arrange
|
||||
var protobufResponse = new
|
||||
{
|
||||
message = "hello"
|
||||
};
|
||||
|
||||
var request = Request.Create();
|
||||
|
||||
var response = Response.Create()
|
||||
.WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloReply", protobufResponse)
|
||||
.WithTrailingHeader("grpc-status", "0");
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 43, null, null, null, null, null, false, null, null);
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
// Assert
|
||||
model.Should().NotBeNull();
|
||||
|
||||
// Verify
|
||||
return Verifier.Verify(model);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ToMappingModel_Mapping_WithBodyAsProtoBuf_ReturnsCorrectModel()
|
||||
{
|
||||
// Arrange
|
||||
var jsonMatcher = new JsonMatcher(new { name = "stef" });
|
||||
var protobufResponse = new
|
||||
{
|
||||
message = "hello"
|
||||
};
|
||||
|
||||
var request = Request.Create()
|
||||
.UsingPost()
|
||||
.WithPath("/grpc/greet.Greeter/SayHello")
|
||||
.WithBodyAsProtoBuf("greet.HelloRequest", jsonMatcher);
|
||||
|
||||
var response = Response.Create()
|
||||
.WithBodyAsProtoBuf("greet.HelloReply", protobufResponse)
|
||||
.WithTrailingHeader("grpc-status", "0");
|
||||
|
||||
var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 41, null, null, null, null, null, false, null, null)
|
||||
.WithProtoDefinition(new (null, ProtoDefinition));
|
||||
|
||||
((Request)request).Mapping = mapping;
|
||||
((Response)response).Mapping = mapping;
|
||||
|
||||
// Act
|
||||
var model = _sut.ToMappingModel(mapping);
|
||||
|
||||
@@ -27,7 +27,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IMatcher_Null()
|
||||
public void MatcherMapper_Map_Matcher_IMatcher_Null()
|
||||
{
|
||||
// Act
|
||||
var model = _sut.Map((IMatcher?)null);
|
||||
@@ -37,7 +37,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IMatchers_Null()
|
||||
public void MatcherMapper_Map_Matcher_IMatchers_Null()
|
||||
{
|
||||
// Act
|
||||
var model = _sut.Map((IMatcher[]?)null);
|
||||
@@ -47,7 +47,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IMatchers()
|
||||
public void MatcherMapper_Map_Matcher_IMatchers()
|
||||
{
|
||||
// Assign
|
||||
var matcherMock1 = new Mock<IStringMatcher>();
|
||||
@@ -62,7 +62,7 @@ public class MatcherMapperTests
|
||||
|
||||
#if MIMEKIT
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MimePartMatcher()
|
||||
public void MatcherMapper_Map_Matcher_MimePartMatcher()
|
||||
{
|
||||
// Arrange
|
||||
var bytes = Convert.FromBase64String("c3RlZg==");
|
||||
@@ -95,7 +95,7 @@ public class MatcherMapperTests
|
||||
#endif
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IStringMatcher()
|
||||
public void MatcherMapper_Map_Matcher_IStringMatcher()
|
||||
{
|
||||
// Assign
|
||||
var matcherMock = new Mock<IStringMatcher>();
|
||||
@@ -115,7 +115,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IStringMatcher_With_PatternAsFile()
|
||||
public void MatcherMapper_Map_Matcher_IStringMatcher_With_PatternAsFile()
|
||||
{
|
||||
// Arrange
|
||||
var pattern = new StringPattern { Pattern = "p", PatternAsFile = "pf" };
|
||||
@@ -136,7 +136,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_IIgnoreCaseMatcher()
|
||||
public void MatcherMapper_Map_Matcher_IIgnoreCaseMatcher()
|
||||
{
|
||||
// Assign
|
||||
var matcherMock = new Mock<IIgnoreCaseMatcher>();
|
||||
@@ -150,7 +150,7 @@ public class MatcherMapperTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_XPathMatcher()
|
||||
public void MatcherMapper_Map_Matcher_XPathMatcher()
|
||||
{
|
||||
// Assign
|
||||
var xmlNamespaceMap = new[]
|
||||
@@ -171,7 +171,7 @@ public class MatcherMapperTests
|
||||
|
||||
#if GRAPHQL
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_GraphQLMatcher()
|
||||
public void MatcherMapper_Map_Matcher_GraphQLMatcher()
|
||||
{
|
||||
// Assign
|
||||
const string testSchema = @"
|
||||
@@ -199,6 +199,87 @@ public class MatcherMapperTests
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_Matcher_ProtoBufMatcher()
|
||||
{
|
||||
// Arrange
|
||||
IdOrText protoDefinition = new(null, @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
");
|
||||
const string messageType = "greet.HelloRequest";
|
||||
|
||||
var jsonPattern = new { name = "stef" };
|
||||
var jsonMatcher = new JsonMatcher(jsonPattern);
|
||||
|
||||
var matcher = new ProtoBufMatcher(() => protoDefinition, messageType, matcher: jsonMatcher);
|
||||
|
||||
// Act
|
||||
var model = _sut.Map(matcher)!;
|
||||
|
||||
// Assert
|
||||
model.Name.Should().Be(nameof(ProtoBufMatcher));
|
||||
model.Pattern.Should().Be(protoDefinition.Text);
|
||||
model.ProtoBufMessageType.Should().Be(messageType);
|
||||
model.ContentMatcher?.Name.Should().Be("JsonMatcher");
|
||||
model.ContentMatcher?.Pattern.Should().Be(jsonPattern);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_Matcher_ProtoBufMatcher_WithId()
|
||||
{
|
||||
// Arrange
|
||||
string id = "abc123";
|
||||
IdOrText protoDefinition = new(id, @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
");
|
||||
const string messageType = "greet.HelloRequest";
|
||||
|
||||
var jsonPattern = new { name = "stef" };
|
||||
var jsonMatcher = new JsonMatcher(jsonPattern);
|
||||
|
||||
var matcher = new ProtoBufMatcher(() => protoDefinition, messageType, matcher: jsonMatcher);
|
||||
|
||||
// Act
|
||||
var model = _sut.Map(matcher)!;
|
||||
|
||||
// Assert
|
||||
model.Name.Should().Be(nameof(ProtoBufMatcher));
|
||||
model.Pattern.Should().Be(id);
|
||||
model.ProtoBufMessageType.Should().Be(messageType);
|
||||
model.ContentMatcher?.Name.Should().Be("JsonMatcher");
|
||||
model.ContentMatcher?.Pattern.Should().Be(jsonPattern);
|
||||
}
|
||||
#endif
|
||||
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_Null()
|
||||
{
|
||||
@@ -736,7 +817,7 @@ public class MatcherMapperTests
|
||||
var matcher = (ExactObjectMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
Check.That(matcher.ValueAsBytes).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
||||
Check.That((byte[])matcher.Value).ContainsExactly(new byte[] { 115, 116, 101, 102 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -1001,4 +1082,53 @@ public class MatcherMapperTests
|
||||
matcher.CustomScalars.Should().BeEquivalentTo(customScalars);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PROTOBUF
|
||||
[Fact]
|
||||
public void MatcherMapper_Map_MatcherModel_ProtoBufMatcher()
|
||||
{
|
||||
// Arrange
|
||||
const string protoDefinition = @"
|
||||
syntax = ""proto3"";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
";
|
||||
const string messageType = "greet.HelloRequest";
|
||||
|
||||
var jsonMatcherPattern = new { name = "stef" };
|
||||
|
||||
var model = new MatcherModel
|
||||
{
|
||||
Name = nameof(ProtoBufMatcher),
|
||||
Pattern = protoDefinition,
|
||||
ProtoBufMessageType = messageType,
|
||||
ContentMatcher = new MatcherModel
|
||||
{
|
||||
Name = nameof(JsonMatcher),
|
||||
Pattern = jsonMatcherPattern
|
||||
}
|
||||
};
|
||||
|
||||
// Act
|
||||
var matcher = (ProtoBufMatcher)_sut.Map(model)!;
|
||||
|
||||
// Assert
|
||||
matcher.ProtoDefinition().Text.Should().Be(protoDefinition);
|
||||
matcher.Name.Should().Be(nameof(ProtoBufMatcher));
|
||||
matcher.MessageType.Should().Be(messageType);
|
||||
matcher.Matcher?.Value.Should().Be(jsonMatcherPattern);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user