mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 00:59:02 +01:00
Fix ProtoBuf mapping.json (#1236)
* Fix ProtoBuf Mappings * [Fact(Skip = "#1233")] * fix? * PortUtils
This commit is contained in:
@@ -37,7 +37,32 @@ message HelloReply {
|
||||
public async Task IWireMockAdminApi_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
|
||||
{
|
||||
// Arrange
|
||||
using var server = WireMockServer.StartWithAdminInterface();
|
||||
using var server = Given_WithBodyAsProtoBuf_AddedToServer();
|
||||
|
||||
// Act
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Url);
|
||||
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);
|
||||
|
||||
await Verifier.Verify(getMappingsResult, VerifySettings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HttpClient_GetMappingsAsync_WithBodyAsProtoBuf_ShouldReturnCorrectMappingModels()
|
||||
{
|
||||
// Arrange
|
||||
using var server = Given_WithBodyAsProtoBuf_AddedToServer();
|
||||
|
||||
// Act
|
||||
var client = server.CreateClient();
|
||||
var getMappingsResult = await client.GetStringAsync("/__admin/mappings").ConfigureAwait(false);
|
||||
|
||||
await Verifier.VerifyJson(getMappingsResult, VerifySettings);
|
||||
}
|
||||
|
||||
public WireMockServer Given_WithBodyAsProtoBuf_AddedToServer()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
|
||||
var protoBufJsonMatcher = new JsonPartialWildcardMatcher(new { name = "*" });
|
||||
|
||||
@@ -122,13 +147,7 @@ message HelloReply {
|
||||
.WithTransformer()
|
||||
);
|
||||
|
||||
// Act
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Url);
|
||||
var getMappingsResult = await api.GetMappingsAsync().ConfigureAwait(false);
|
||||
|
||||
await Verifier.Verify(getMappingsResult, VerifySettings);
|
||||
|
||||
server.Stop();
|
||||
return server;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,235 @@
|
||||
[
|
||||
{
|
||||
Guid: Guid_1,
|
||||
UpdatedAt: DateTimeOffset_1,
|
||||
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: JsonPartialWildcardMatcher,
|
||||
Pattern: {
|
||||
name: *
|
||||
},
|
||||
IgnoreCase: false,
|
||||
Regex: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello {{request.BodyAsJson.name}}
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerType: Handlebars,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert,
|
||||
Headers: {
|
||||
Content-Type: application/grpc
|
||||
},
|
||||
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
|
||||
}
|
||||
},
|
||||
{
|
||||
Guid: Guid_2,
|
||||
UpdatedAt: DateTimeOffset_2,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: /grpc2/greet.Greeter/SayHello,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: ProtoBufMatcher,
|
||||
ContentMatcher: {
|
||||
Name: JsonPartialWildcardMatcher,
|
||||
Pattern: {
|
||||
name: *
|
||||
},
|
||||
IgnoreCase: false,
|
||||
Regex: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello {{request.BodyAsJson.name}}
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerType: Handlebars,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert,
|
||||
Headers: {
|
||||
Content-Type: application/grpc
|
||||
},
|
||||
TrailingHeaders: {
|
||||
grpc-status: 0
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloReply
|
||||
},
|
||||
ProtoDefinition:
|
||||
syntax = "proto3";
|
||||
|
||||
package greet;
|
||||
|
||||
service Greeter {
|
||||
rpc SayHello (HelloRequest) returns (HelloReply);
|
||||
}
|
||||
|
||||
message HelloRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message HelloReply {
|
||||
string message = 1;
|
||||
}
|
||||
|
||||
},
|
||||
{
|
||||
Guid: Guid_3,
|
||||
UpdatedAt: DateTimeOffset_3,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: /grpc3/greet.Greeter/SayHello,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: ProtoBufMatcher,
|
||||
ContentMatcher: {
|
||||
Name: JsonPartialWildcardMatcher,
|
||||
Pattern: {
|
||||
name: *
|
||||
},
|
||||
IgnoreCase: false,
|
||||
Regex: false
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello {{request.BodyAsJson.name}}
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerType: Handlebars,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert,
|
||||
Headers: {
|
||||
Content-Type: application/grpc
|
||||
},
|
||||
TrailingHeaders: {
|
||||
grpc-status: 0
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloReply
|
||||
},
|
||||
ProtoDefinition: my-greeter
|
||||
},
|
||||
{
|
||||
Guid: Guid_4,
|
||||
UpdatedAt: DateTimeOffset_4,
|
||||
Request: {
|
||||
Path: {
|
||||
Matchers: [
|
||||
{
|
||||
Name: WildcardMatcher,
|
||||
Pattern: /grpc4/greet.Greeter/SayHello,
|
||||
IgnoreCase: false
|
||||
}
|
||||
]
|
||||
},
|
||||
Methods: [
|
||||
POST
|
||||
],
|
||||
Body: {
|
||||
Matcher: {
|
||||
Name: ProtoBufMatcher,
|
||||
ProtoBufMessageType: greet.HelloRequest
|
||||
}
|
||||
}
|
||||
},
|
||||
Response: {
|
||||
BodyAsJson: {
|
||||
message: hello {{request.BodyAsJson.name}}
|
||||
},
|
||||
UseTransformer: true,
|
||||
TransformerType: Handlebars,
|
||||
TransformerReplaceNodeOptions: EvaluateAndTryToConvert,
|
||||
Headers: {
|
||||
Content-Type: application/grpc
|
||||
},
|
||||
TrailingHeaders: {
|
||||
grpc-status: 0
|
||||
},
|
||||
ProtoBufMessageType: greet.HelloReply
|
||||
},
|
||||
ProtoDefinition: my-greeter
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,172 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
#if !(NET452 || NET461 || NETCOREAPP3_1)
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using NFluent;
|
||||
using RestEase;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Client;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Models;
|
||||
using WireMock.Server;
|
||||
using Xunit;
|
||||
|
||||
namespace WireMock.Net.Tests.AdminApi;
|
||||
|
||||
public partial class WireMockAdminApiTests
|
||||
{
|
||||
public static string RemoveLineContainingUpdatedAt(string text)
|
||||
{
|
||||
var lines = text.Split([Environment.NewLine], StringSplitOptions.None);
|
||||
var filteredLines = lines.Where(line => !line.Contains("\"UpdatedAt\": "));
|
||||
return string.Join(Environment.NewLine, filteredLines);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("protobuf-mapping-1.json", "351f0240-bba0-4bcb-93c6-1feba0fe0001")]
|
||||
[InlineData("protobuf-mapping-2.json", "351f0240-bba0-4bcb-93c6-1feba0fe0002")]
|
||||
[InlineData("protobuf-mapping-3.json", "351f0240-bba0-4bcb-93c6-1feba0fe0003")]
|
||||
[InlineData("protobuf-mapping-4.json", "351f0240-bba0-4bcb-93c6-1feba0fe0004")]
|
||||
public async Task HttpClient_PostMappingsAsync_ForProtoBufMapping(string mappingFile, string guid)
|
||||
{
|
||||
// Arrange
|
||||
var mappingsJson = ReadMappingFile(mappingFile);
|
||||
|
||||
using var server = WireMockServer.StartWithAdminInterface();
|
||||
var httpClient = server.CreateClient();
|
||||
|
||||
// Act
|
||||
var result = await httpClient.PostAsync("/__admin/mappings", new StringContent(mappingsJson, Encoding.UTF8, WireMockConstants.ContentTypeJson));
|
||||
result.EnsureSuccessStatusCode();
|
||||
|
||||
// Assert
|
||||
var mapping = await httpClient.GetStringAsync($"/__admin/mappings/{guid}");
|
||||
mapping = RemoveLineContainingUpdatedAt(mapping);
|
||||
mapping.Should().Be(mappingsJson);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_PostMappingsAsync()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model1 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt 1" },
|
||||
Title = "test 1"
|
||||
};
|
||||
var model2 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/2" },
|
||||
Response = new ResponseModel { Body = "txt 2" },
|
||||
Title = "test 2"
|
||||
};
|
||||
var result = await api.PostMappingsAsync(new[] { model1, model2 }).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNull();
|
||||
Check.That(server.Mappings.Where(m => !m.IsAdminInterface)).HasSize(2);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData(-1, -1)]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(200, 200)]
|
||||
[InlineData("200", "200")]
|
||||
public async Task IWireMockAdminApi_PostMappingAsync_WithStatusCode(object statusCode, object expectedStatusCode)
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt", StatusCode = statusCode },
|
||||
Priority = 500,
|
||||
Title = "test"
|
||||
};
|
||||
var result = await api.PostMappingAsync(model).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNotNull();
|
||||
|
||||
var mapping = server.Mappings.Single(m => m.Priority == 500);
|
||||
Check.That(mapping).IsNotNull();
|
||||
Check.That(mapping.Title).Equals("test");
|
||||
|
||||
var response = await mapping.ProvideResponseAsync(new RequestMessage(new UrlDetails("http://localhost/1"), "GET", "")).ConfigureAwait(false);
|
||||
Check.That(response.Message.StatusCode).Equals(expectedStatusCode);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_PostMappingsAsync_WithDuplicateGuids_Should_Return_400()
|
||||
{
|
||||
// Arrange
|
||||
var guid = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model1WithGuid = new MappingModel
|
||||
{
|
||||
Guid = guid,
|
||||
Request = new RequestModel { Path = "/1g" },
|
||||
Response = new ResponseModel { Body = "txt 1g" },
|
||||
Title = "test 1g"
|
||||
};
|
||||
var model2WithGuid = new MappingModel
|
||||
{
|
||||
Guid = guid,
|
||||
Request = new RequestModel { Path = "/2g" },
|
||||
Response = new ResponseModel { Body = "txt 2g" },
|
||||
Title = "test 2g"
|
||||
};
|
||||
var model1 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt 1" },
|
||||
Title = "test 1"
|
||||
};
|
||||
var model2 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/2" },
|
||||
Response = new ResponseModel { Body = "txt 2" },
|
||||
Title = "test 2"
|
||||
};
|
||||
|
||||
var models = new[]
|
||||
{
|
||||
model1WithGuid,
|
||||
model2WithGuid,
|
||||
model1,
|
||||
model2
|
||||
};
|
||||
|
||||
var sutMethod = async () => await api.PostMappingsAsync(models);
|
||||
var exceptionAssertions = await sutMethod.Should().ThrowAsync<ApiException>();
|
||||
exceptionAssertions.Which.Content.Should().Be(@"{""Status"":""The following Guids are duplicate : '1b731398-4a5b-457f-a6e3-d65e541c428f' (Parameter 'mappingModels')""}");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -184,124 +184,7 @@ public partial class WireMockAdminApiTests
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, null)]
|
||||
[InlineData(-1, -1)]
|
||||
[InlineData(0, 0)]
|
||||
[InlineData(200, 200)]
|
||||
[InlineData("200", "200")]
|
||||
public async Task IWireMockAdminApi_PostMappingAsync_WithStatusCode(object statusCode, object expectedStatusCode)
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt", StatusCode = statusCode },
|
||||
Priority = 500,
|
||||
Title = "test"
|
||||
};
|
||||
var result = await api.PostMappingAsync(model).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNotNull();
|
||||
|
||||
var mapping = server.Mappings.Single(m => m.Priority == 500);
|
||||
Check.That(mapping).IsNotNull();
|
||||
Check.That(mapping.Title).Equals("test");
|
||||
|
||||
var response = await mapping.ProvideResponseAsync(new RequestMessage(new UrlDetails("http://localhost/1"), "GET", "")).ConfigureAwait(false);
|
||||
Check.That(response.Message.StatusCode).Equals(expectedStatusCode);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_PostMappingsAsync()
|
||||
{
|
||||
// Arrange
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model1 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt 1" },
|
||||
Title = "test 1"
|
||||
};
|
||||
var model2 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/2" },
|
||||
Response = new ResponseModel { Body = "txt 2" },
|
||||
Title = "test 2"
|
||||
};
|
||||
var result = await api.PostMappingsAsync(new[] { model1, model2 }).ConfigureAwait(false);
|
||||
|
||||
// Assert
|
||||
Check.That(result).IsNotNull();
|
||||
Check.That(result.Status).IsNotNull();
|
||||
Check.That(result.Guid).IsNull();
|
||||
Check.That(server.Mappings.Where(m => !m.IsAdminInterface)).HasSize(2);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_PostMappingsAsync_WithDuplicateGuids_Should_Return_400()
|
||||
{
|
||||
// Arrange
|
||||
var guid = Guid.Parse("1b731398-4a5b-457f-a6e3-d65e541c428f");
|
||||
var server = WireMockServer.StartWithAdminInterface();
|
||||
var api = RestClient.For<IWireMockAdminApi>(server.Urls[0]);
|
||||
|
||||
// Act
|
||||
var model1WithGuid = new MappingModel
|
||||
{
|
||||
Guid = guid,
|
||||
Request = new RequestModel { Path = "/1g" },
|
||||
Response = new ResponseModel { Body = "txt 1g" },
|
||||
Title = "test 1g"
|
||||
};
|
||||
var model2WithGuid = new MappingModel
|
||||
{
|
||||
Guid = guid,
|
||||
Request = new RequestModel { Path = "/2g" },
|
||||
Response = new ResponseModel { Body = "txt 2g" },
|
||||
Title = "test 2g"
|
||||
};
|
||||
var model1 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/1" },
|
||||
Response = new ResponseModel { Body = "txt 1" },
|
||||
Title = "test 1"
|
||||
};
|
||||
var model2 = new MappingModel
|
||||
{
|
||||
Request = new RequestModel { Path = "/2" },
|
||||
Response = new ResponseModel { Body = "txt 2" },
|
||||
Title = "test 2"
|
||||
};
|
||||
|
||||
var models = new[]
|
||||
{
|
||||
model1WithGuid,
|
||||
model2WithGuid,
|
||||
model1,
|
||||
model2
|
||||
};
|
||||
|
||||
var sutMethod = async () => await api.PostMappingsAsync(models);
|
||||
var exceptionAssertions = await sutMethod.Should().ThrowAsync<ApiException>();
|
||||
exceptionAssertions.Which.Content.Should().Be(@"{""Status"":""The following Guids are duplicate : '1b731398-4a5b-457f-a6e3-d65e541c428f' (Parameter 'mappingModels')""}");
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task IWireMockAdminApi_FindRequestsAsync()
|
||||
@@ -1140,5 +1023,10 @@ text
|
||||
// Assert
|
||||
status.Status.Should().Be("Static Mappings reloaded");
|
||||
}
|
||||
|
||||
private static string ReadMappingFile(string filename)
|
||||
{
|
||||
return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings", filename));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -2,19 +2,24 @@
|
||||
|
||||
#if PROTOBUF
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using Google.Protobuf.WellKnownTypes;
|
||||
using Greet;
|
||||
using Grpc.Net.Client;
|
||||
using NarrowIntegrationTest.Lookup;
|
||||
using WireMock.Constants;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
using WireMock.Util;
|
||||
using Xunit;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
@@ -486,20 +491,17 @@ message Other {
|
||||
);
|
||||
|
||||
// Act
|
||||
var channel = GrpcChannel.ForAddress(server.Url!);
|
||||
var client = new Greeter.GreeterClient(channel);
|
||||
|
||||
var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" });
|
||||
var reply = await When_GrpcClient_Calls_SayHelloAsync(server.Url!);
|
||||
|
||||
// Assert
|
||||
reply.Message.Should().Be("hello stef POST");
|
||||
Then_ReplyMessage_Should_BeCorrect(reply);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes_Empty_UsingGrpcGeneratedClient()
|
||||
{
|
||||
// Arrange
|
||||
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
var definition = await File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
|
||||
using var server = WireMockServer.Start(useHttp2: true);
|
||||
|
||||
@@ -532,7 +534,7 @@ message Other {
|
||||
// Arrange
|
||||
const int seconds = 1722301323;
|
||||
const int nanos = 12300;
|
||||
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
var definition = await File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
|
||||
using var server = WireMockServer.Start(useHttp2: true);
|
||||
|
||||
@@ -573,7 +575,7 @@ message Other {
|
||||
// Arrange
|
||||
const int seconds = 1722301323;
|
||||
const int nanos = 12300;
|
||||
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
var definition = await File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
|
||||
using var server = WireMockServer.Start(useHttp2: true);
|
||||
|
||||
@@ -612,7 +614,7 @@ message Other {
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_Enum_UsingGrpcGeneratedClient()
|
||||
{
|
||||
// Arrange
|
||||
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
var definition = await File.ReadAllTextAsync("./Grpc/greet.proto");
|
||||
|
||||
using var server = WireMockServer.Start(useHttp2: true);
|
||||
|
||||
@@ -653,7 +655,7 @@ message Other {
|
||||
const int nanos = 12300;
|
||||
const string version = "test";
|
||||
const string correlationId = "correlation";
|
||||
var definition = await System.IO.File.ReadAllTextAsync("./Grpc/policy.proto");
|
||||
var definition = await File.ReadAllTextAsync("./Grpc/policy.proto");
|
||||
|
||||
using var server = WireMockServer.Start(useHttp2: true);
|
||||
|
||||
@@ -696,5 +698,68 @@ message Other {
|
||||
reply.Client.ClientName.Should().Be(NarrowIntegrationTest.Lookup.Client.Types.Clients.BillingCenter);
|
||||
reply.Client.CorrelationId.Should().Be(correlationId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinitionFromJson_UsingGrpcGeneratedClient()
|
||||
{
|
||||
var server = Given_When_ServerStartedUsingHttp2();
|
||||
Given_ProtoDefinition_IsAddedOnServerLevel(server);
|
||||
await Given_When_ProtoBufMappingIsAddedViaAdminInterfaceAsync(server);
|
||||
|
||||
var reply = await When_GrpcClient_Calls_SayHelloAsync(server.Urls[1]);
|
||||
|
||||
Then_ReplyMessage_Should_BeCorrect(reply);
|
||||
}
|
||||
|
||||
private static WireMockServer Given_When_ServerStartedUsingHttp2()
|
||||
{
|
||||
var ports = PortUtils.FindFreeTcpPorts(2);
|
||||
|
||||
var settings = new WireMockServerSettings
|
||||
{
|
||||
Urls = [$"http://*:{ports[0]}/", $"grpc://*:{ports[1]}/"],
|
||||
StartAdminInterface = true
|
||||
};
|
||||
return WireMockServer.Start(settings);
|
||||
}
|
||||
|
||||
private static void Given_ProtoDefinition_IsAddedOnServerLevel(WireMockServer server)
|
||||
{
|
||||
server.AddProtoDefinition("my-greeter", ReadProtoFile("greet.proto"));
|
||||
}
|
||||
|
||||
private static async Task Given_When_ProtoBufMappingIsAddedViaAdminInterfaceAsync(WireMockServer server)
|
||||
{
|
||||
var mappingsJson = ReadMappingFile("protobuf-mapping-3.json");
|
||||
|
||||
using var httpClient = server.CreateClient();
|
||||
|
||||
var result = await httpClient.PostAsync("/__admin/mappings", new StringContent(mappingsJson, Encoding.UTF8, WireMockConstants.ContentTypeJson));
|
||||
result.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private static async Task<HelloReply> When_GrpcClient_Calls_SayHelloAsync(string address)
|
||||
{
|
||||
var channel = GrpcChannel.ForAddress(address);
|
||||
|
||||
var client = new Greeter.GreeterClient(channel);
|
||||
|
||||
return await client.SayHelloAsync(new HelloRequest { Name = "stef" });
|
||||
}
|
||||
|
||||
private static void Then_ReplyMessage_Should_BeCorrect(HelloReply reply)
|
||||
{
|
||||
reply.Message.Should().Be("hello stef POST");
|
||||
}
|
||||
|
||||
private static string ReadMappingFile(string filename)
|
||||
{
|
||||
return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "__admin", "mappings", filename));
|
||||
}
|
||||
|
||||
private static string ReadProtoFile(string filename)
|
||||
{
|
||||
return File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Grpc", filename));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -15,7 +15,6 @@ using RestEase;
|
||||
using WireMock.Client;
|
||||
using WireMock.Handlers;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -27,7 +26,8 @@ namespace WireMock.Net.Tests;
|
||||
|
||||
public class WireMockServerAdminTests
|
||||
{
|
||||
// For for AppVeyor + OpenCover
|
||||
private const int NumStaticMappings = 10;
|
||||
|
||||
private static string GetCurrentFolder()
|
||||
{
|
||||
return Directory.GetCurrentDirectory();
|
||||
@@ -40,8 +40,8 @@ public class WireMockServerAdminTests
|
||||
string folder = Path.Combine(GetCurrentFolder(), "__admin", "mappings");
|
||||
server.ReadStaticMappings(folder);
|
||||
|
||||
Check.That(server.Mappings).HasSize(6);
|
||||
Check.That(server.MappingModels).HasSize(6);
|
||||
Check.That(server.Mappings).HasSize(NumStaticMappings);
|
||||
Check.That(server.MappingModels).HasSize(NumStaticMappings);
|
||||
|
||||
// Act
|
||||
server.ResetMappings();
|
||||
@@ -220,7 +220,7 @@ public class WireMockServerAdminTests
|
||||
server.ReadStaticMappings(folder);
|
||||
|
||||
var mappings = server.Mappings.ToArray();
|
||||
Check.That(mappings).HasSize(6);
|
||||
Check.That(mappings).HasSize(NumStaticMappings);
|
||||
|
||||
server.Stop();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"Guid": "351f0240-bba0-4bcb-93c6-1feba0fe0001",
|
||||
"Title": "ProtoBuf Mapping 1",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/greet.Greeter/SayHello",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "ProtoBufMatcher",
|
||||
"Pattern": "\r\nsyntax = \"proto3\";\r\n\r\npackage greet;\r\n\r\nservice Greeter {\r\n rpc SayHello (HelloRequest) returns (HelloReply);\r\n}\r\n\r\nmessage HelloRequest {\r\n string name = 1;\r\n}\r\n\r\nmessage HelloReply {\r\n string message = 1;\r\n}\r\n",
|
||||
"ContentMatcher": {
|
||||
"Name": "JsonPartialWildcardMatcher",
|
||||
"Pattern": {
|
||||
"name": "*"
|
||||
},
|
||||
"IgnoreCase": false,
|
||||
"Regex": false
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
"TransformerReplaceNodeOptions": "EvaluateAndTryToConvert",
|
||||
"Headers": {
|
||||
"Content-Type": "application/grpc"
|
||||
},
|
||||
"TrailingHeaders": {
|
||||
"grpc-status": "0"
|
||||
},
|
||||
"ProtoDefinition": "\r\nsyntax = \"proto3\";\r\n\r\npackage greet;\r\n\r\nservice Greeter {\r\n rpc SayHello (HelloRequest) returns (HelloReply);\r\n}\r\n\r\nmessage HelloRequest {\r\n string name = 1;\r\n}\r\n\r\nmessage HelloReply {\r\n string message = 1;\r\n}\r\n",
|
||||
"ProtoBufMessageType": "greet.HelloReply"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"Guid": "351f0240-bba0-4bcb-93c6-1feba0fe0002",
|
||||
"Title": "ProtoBuf Mapping 2",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/greet.Greeter/SayHello",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "ProtoBufMatcher",
|
||||
"ContentMatcher": {
|
||||
"Name": "JsonPartialWildcardMatcher",
|
||||
"Pattern": {
|
||||
"name": "*"
|
||||
},
|
||||
"IgnoreCase": false,
|
||||
"Regex": false
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
"TransformerReplaceNodeOptions": "EvaluateAndTryToConvert",
|
||||
"Headers": {
|
||||
"Content-Type": "application/grpc"
|
||||
},
|
||||
"TrailingHeaders": {
|
||||
"grpc-status": "0"
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloReply"
|
||||
},
|
||||
"ProtoDefinition": "\r\nsyntax = \"proto3\";\r\n\r\npackage greet;\r\n\r\nservice Greeter {\r\n rpc SayHello (HelloRequest) returns (HelloReply);\r\n}\r\n\r\nmessage HelloRequest {\r\n string name = 1;\r\n}\r\n\r\nmessage HelloReply {\r\n string message = 1;\r\n}\r\n"
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"Guid": "351f0240-bba0-4bcb-93c6-1feba0fe0003",
|
||||
"Title": "ProtoBuf Mapping 3",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/greet.Greeter/SayHello",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "ProtoBufMatcher",
|
||||
"ContentMatcher": {
|
||||
"Name": "JsonPartialWildcardMatcher",
|
||||
"Pattern": {
|
||||
"name": "*"
|
||||
},
|
||||
"IgnoreCase": true,
|
||||
"Regex": false
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}} {{request.method}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
"TransformerReplaceNodeOptions": "EvaluateAndTryToConvert",
|
||||
"Headers": {
|
||||
"Content-Type": "application/grpc"
|
||||
},
|
||||
"TrailingHeaders": {
|
||||
"grpc-status": "0"
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloReply"
|
||||
},
|
||||
"ProtoDefinition": "my-greeter"
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"Guid": "351f0240-bba0-4bcb-93c6-1feba0fe0004",
|
||||
"Title": "ProtoBuf Mapping 4",
|
||||
"Request": {
|
||||
"Path": {
|
||||
"Matchers": [
|
||||
{
|
||||
"Name": "WildcardMatcher",
|
||||
"Pattern": "/greet.Greeter/SayHello",
|
||||
"IgnoreCase": false
|
||||
}
|
||||
]
|
||||
},
|
||||
"Methods": [
|
||||
"POST"
|
||||
],
|
||||
"Body": {
|
||||
"Matcher": {
|
||||
"Name": "ProtoBufMatcher",
|
||||
"ProtoBufMessageType": "greet.HelloRequest"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Response": {
|
||||
"BodyAsJson": {
|
||||
"message": "hello {{request.BodyAsJson.name}}"
|
||||
},
|
||||
"UseTransformer": true,
|
||||
"TransformerType": "Handlebars",
|
||||
"TransformerReplaceNodeOptions": "EvaluateAndTryToConvert",
|
||||
"Headers": {
|
||||
"Content-Type": "application/grpc"
|
||||
},
|
||||
"TrailingHeaders": {
|
||||
"grpc-status": "0"
|
||||
},
|
||||
"ProtoBufMessageType": "greet.HelloReply"
|
||||
},
|
||||
"ProtoDefinition": "my-greeter"
|
||||
}
|
||||
Reference in New Issue
Block a user