// Copyright © WireMock.Net using System; using System.Collections.Generic; using WireMock.Matchers; using WireMock.Matchers.Request; using WireMock.Models; namespace WireMock.RequestBuilders; /// /// IRequestBuilderExtensions extensions for ProtoBuf. /// // ReSharper disable once InconsistentNaming public static class IRequestBuilderExtensions { /// /// WithBodyAsProtoBuf /// /// The . /// The proto definition as text. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, matchBehaviour); } /// /// WithBodyAsProtoBuf /// /// The . /// The proto definition as text. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The matcher to use to match the ProtoBuf as (json) object. /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, matcher, matchBehaviour); } /// /// WithBodyAsProtoBuf /// /// The . /// The proto definitions as text. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, IReadOnlyList protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType)); } /// /// WithBodyAsProtoBuf /// /// The . /// The proto definitions as text. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The matcher to use to match the ProtoBuf as (json) object. /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, IReadOnlyList protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => new IdOrTexts(null, protoDefinitions), messageType, matcher)); } /// /// WithBodyAsProtoBuf /// /// The . /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(requestBuilder), messageType)); } /// /// WithBodyAsProtoBuf /// /// The . /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The matcher to use to match the ProtoBuf as (json) object. /// The match behaviour. (default = "AcceptOnMatch") /// The . public static IRequestBuilder WithBodyAsProtoBuf(this IRequestBuilder requestBuilder, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { return requestBuilder.Add(new RequestMessageProtoBufMatcher(matchBehaviour, ProtoDefinitionFunc(requestBuilder), messageType, matcher)); } private static Func ProtoDefinitionFunc(IRequestBuilder requestBuilder) { return () => { if (requestBuilder.Mapping.ProtoDefinition == null) { throw new InvalidOperationException($"No ProtoDefinition defined on mapping '{requestBuilder.Mapping.Guid}'. Please use the WireMockServerSettings to define ProtoDefinitions."); } return requestBuilder.Mapping.ProtoDefinition.Value; }; } }