// Copyright © WireMock.Net
using System.Collections.Generic;
using JsonConverter.Abstractions;
using Stef.Validation;
using WireMock.Exceptions;
using WireMock.Models;
using WireMock.Types;
using WireMock.Util;
namespace WireMock.ResponseBuilders;
///
/// Extensions for to implement WithBodyAsProtoBuf.
///
public static class IResponseBuilderExtensions
{
///
/// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value.
///
/// The response builder.
/// The proto definition as text.
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// The object to convert to protobuf byte[].
/// The [optional]. Default value is NewtonsoftJsonConverter.
/// The [optional].
/// A .
public static IResponseBuilder WithBodyAsProtoBuf(
this IResponseBuilder responseBuilder,
string protoDefinition,
string messageType,
object value,
IJsonConverter? jsonConverter = null,
JsonConverterOptions? options = null
)
{
return responseBuilder.WithBodyAsProtoBuf([protoDefinition], messageType, value, jsonConverter, options);
}
///
/// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on proto definitions, message type and the value.
///
/// The response builder.
/// The proto definition as text.
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// The object to convert to protobuf byte[].
/// The [optional]. Default value is NewtonsoftJsonConverter.
/// The [optional].
/// A .
public static IResponseBuilder WithBodyAsProtoBuf(
this IResponseBuilder responseBuilder,
IReadOnlyList protoDefinitions,
string messageType,
object value,
IJsonConverter? jsonConverter = null,
JsonConverterOptions? options = null
)
{
Guard.NotNullOrEmpty(protoDefinitions);
Guard.NotNullOrWhiteSpace(messageType);
Guard.NotNull(value);
responseBuilder.ResponseMessage.BodyDestination = null;
responseBuilder.ResponseMessage.BodyData = new BodyData
{
DetectedBodyType = BodyType.ProtoBuf,
BodyAsJson = value,
ProtoDefinition = () => new IdOrTexts(null, protoDefinitions),
ProtoBufMessageType = messageType
};
return responseBuilder;
}
///
/// WithBodyAsProtoBuf : Create a ProtoBuf byte[] response based on a proto definition, message type and the value.
///
/// The response builder.
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// The object to convert to protobuf byte[].
/// The [optional]. Default value is NewtonsoftJsonConverter.
/// The [optional].
/// A .
public static IResponseBuilder WithBodyAsProtoBuf(
this IResponseBuilder responseBuilder,
string messageType,
object value,
IJsonConverter? jsonConverter = null,
JsonConverterOptions? options = null
)
{
Guard.NotNullOrWhiteSpace(messageType);
Guard.NotNull(value);
responseBuilder.ResponseMessage.BodyDestination = null;
responseBuilder.ResponseMessage.BodyData = new BodyData
{
DetectedBodyType = BodyType.ProtoBuf,
BodyAsJson = value,
ProtoDefinition = () => responseBuilder.Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."),
ProtoBufMessageType = messageType
};
return responseBuilder;
}
}