WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.42k stars 209 forks source link

Add gRPC mock requests and responses through the Administration API. #1162

Closed AllenXiao565 closed 2 months ago

AllenXiao565 commented 2 months ago

Based on the introduction and exercises from the blog post on how to use WireMock.NET with gRPC, I was able to successfully complete the first step. However, I got stuck when trying to move on to the next step. My expectation was to be able to use the Administration API to send requests and responses for simulating gRPC responses, but it seems like this is not currently supported. I was wondering if you have any other suggestions

AllenXiao565 commented 2 months ago

var grpcWireMockAdminApi = RestClient.For<IWireMockAdminApi>("http://localhost:49331");

await grpcWireMockAdminApi.PostMappingAsync(new MappingModel { Request = new RequestModel { Methods = ["POST"], Path = "/BonusTransaction/ProcessCasinoBonus", Body = new BodyModel { Matcher = new MatcherModel() { ProtoBufMessageType = "ProcessCasinoBonusRequest" } } }, Response = new ResponseModel() { StatusCode = HttpStatusCode.OK, ProtoDefinition = "GrpcBonusTransaction2", Headers = new Dictionary<string, object>() { { "Content-Type", "application/grpc"} }, TrailingHeaders = new Dictionary<string, object>() { { "grpc-status", "0" } }, ProtoBufMessageType = "CasinoProcessResponse", BodyDestination = null, BodyAsJson = new CasinoProcessResponse { WinLostDate = 1690816326000, Status = 1 }, UseTransformer = true, TransformerType = "Handlebars", TransformerReplaceNodeOptions = "EvaluateAndTryToConvert", UseTransformerForBodyAsFile = false } });

The code above is my current attempt, but when I run it, I get the following error: Unhandled exception. RestEase.ApiException: POST "http://localhost:XXX/__admin/mappings" failed because the response status code does not indicate success. I think this might be happening because the server does not support HTTP requests for mappings when UseHttp2 is enabled.

StefH commented 2 months ago

The admin-api can be used to manage your mappings (which can be mappings which use grpc). Once the mappings are defined, WireMock.Net will behave like a Grpc server. The admin admin cannot be used to send Grpc request to.

See this example project for some info: https://github.com/WireMock-Net/WireMock.Net/tree/master/examples/WireMock.Net.Console.GrpcClient

AllenXiao565 commented 2 months ago

Thank you for your response. It confirms that my assumptions were correct.