Skip to content

Commit

Permalink
[Fact(Skip = "#1233")]
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jan 12, 2025
1 parent 0d5fb07 commit 3e06b5d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 50 deletions.
6 changes: 4 additions & 2 deletions src/WireMock.Net.Abstractions/Server/IWireMockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,16 @@ public interface IWireMockServer : IDisposable
/// This can be used if you have 1 or more <see cref="MappingModel"/> defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// </summary>
/// <param name="mappings">The MappingModels</param>
/// <returns><see cref="IWireMockServer"/></returns>
IWireMockServer WithMapping(params MappingModel[] mappings);

/// <summary>
/// Register the mappings (via json string).
///
/// This can be used if you the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// This can be used if you've the mappings as json string defined and want to register these in WireMock.Net directly instead of using the fluent syntax.
/// </summary>
/// <param name="mappings">The mapping(s) as json string.</param>
/// <returns><see cref="IWireMockServer"/></returns>
IWireMockServer WithMapping(string mappings);

/// <summary>
Expand All @@ -238,5 +240,5 @@ public interface IWireMockServer : IDisposable
/// </summary>
/// <param name="converterType">The <see cref="MappingConverterType"/></param>
/// <returns>C# code</returns>
public string MappingsToCSharpCode(MappingConverterType converterType);
string MappingsToCSharpCode(MappingConverterType converterType);
}
6 changes: 3 additions & 3 deletions src/WireMock.Net/Server/RespondWithAProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public IRespondWithAProvider WithWebhook(
Guard.NotNull(url);
Guard.NotNull(method);

Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) };
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];

if (body != null)
{
Expand All @@ -323,7 +323,7 @@ public IRespondWithAProvider WithWebhook(
Guard.NotNull(url);
Guard.NotNull(method);

Webhooks = new[] { InitWebhook(url, method, headers, useTransformer, transformerType) };
Webhooks = [InitWebhook(url, method, headers, useTransformer, transformerType)];

if (body != null)
{
Expand Down Expand Up @@ -371,7 +371,7 @@ public IRespondWithAProvider WithProtoDefinition(params string[] protoDefinition
{
_protoDefinition = new(null, protoDefinitionOrId);
}


return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,18 @@

#if !(NET452 || NET461 || NETCOREAPP3_1)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using NFluent;
using RestEase;
using VerifyTests;
using VerifyXunit;
using WireMock.Admin.Mappings;
using WireMock.Admin.Settings;
using WireMock.Client;
using WireMock.Client.Extensions;
using WireMock.Constants;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
using WireMock.Models;
using WireMock.Net.Tests.VerifyExtensions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
using Xunit;

namespace WireMock.Net.Tests.AdminApi;
Expand Down Expand Up @@ -64,14 +45,9 @@ public async Task HttpClient_PostMappingsAsync_ForProtoBufMapping(string mapping
result.EnsureSuccessStatusCode();

// Assert
var mappings = server.Mappings
.Where(m => !m.IsAdminInterface)
.OrderBy(m => m.Title)
.ToArray();

var mapping = await httpClient.GetStringAsync($"/__admin/mappings/{guid}");

RemoveLineContainingUpdatedAt(mapping).Should().Be(mappingsJson);
mapping = RemoveLineContainingUpdatedAt(mapping);
mapping.Should().Be(mappingsJson);
}

[Fact]
Expand Down
81 changes: 63 additions & 18 deletions test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

#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 Greet;
using Grpc.Net.Client;
using WireMock.Constants;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using WireMock.Settings;
using Xunit;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -128,8 +132,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_JsonPartialWildcardMatcher()

// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);

server.Stop();
}

[Theory]
Expand Down Expand Up @@ -171,8 +173,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf(string data)
var responseBytes = await response.Content.ReadAsByteArrayAsync();

Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -209,8 +209,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes()
var responseBytes = await response.Content.ReadAsByteArrayAsync();

Convert.ToBase64String(responseBytes).Should().Be("");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -251,8 +249,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinition_WithWe
var responseBytes = await response.Content.ReadAsByteArrayAsync();

Convert.ToBase64String(responseBytes).Should().Be("");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -294,8 +290,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_MultipleFiles()
var responseBytes = await response.Content.ReadAsByteArrayAsync();

Convert.ToBase64String(responseBytes).Should().Be("AAAAAAcKBWhlbGxv");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -333,8 +327,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_InlineProtoDefinition_UsingG

// Assert
reply.Message.Should().Be("hello stef POST");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -374,8 +366,6 @@ public async Task WireMockServer_WithBodyAsProtoBuf_MappingProtoDefinition_Using

// Assert
reply.Message.Should().Be("hello stef POST");

server.Stop();
}

[Fact]
Expand Down Expand Up @@ -410,16 +400,71 @@ public async Task WireMockServer_WithBodyAsProtoBuf_ServerProtoDefinition_UsingG
);

// Act
var channel = GrpcChannel.ForAddress(server.Url!);
var reply = await When_GrpcClient_Calls_SayHelloAsync(server.Url!);

// Assert
Then_ReplyMessage_Should_BeCorrect(reply);
}

[Fact(Skip = "#1233")]
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.Url!);

Then_ReplyMessage_Should_BeCorrect(reply);
}

private static WireMockServer Given_When_ServerStartedUsingHttp2()
{
var settings = new WireMockServerSettings
{
UseHttp2 = false,
StartAdminInterface = true
};
return WireMockServer.Start(settings);
}

private static void Given_ProtoDefinition_IsAddedOnServerLevel(WireMockServer server)
{
server.AddProtoDefinition("my-greeter-351f0240-bba0-4bcb-93c6-1feba0fe0003", 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);

var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" });
return await client.SayHelloAsync(new HelloRequest { Name = "stef" });
}

// Assert
private static void Then_ReplyMessage_Should_BeCorrect(HelloReply reply)
{
reply.Message.Should().Be("hello stef POST");
}

server.Stop();
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
1 change: 1 addition & 0 deletions test/WireMock.Net.Tests/WireMock.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
</None>
<None Update="Grpc\greet.proto">
<GrpcServices>Client</GrpcServices>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="responsebody.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
},
"ProtoBufMessageType": "greet.HelloReply"
},
"ProtoDefinition": "my-greeter"
"ProtoDefinition": "my-greeter-351f0240-bba0-4bcb-93c6-1feba0fe0003"
}

0 comments on commit 3e06b5d

Please sign in to comment.